From 6ff5eb595abb977c02b155dec42c0df044bec8de Mon Sep 17 00:00:00 2001 From: NeumimTo Date: Sat, 18 Apr 2020 23:28:06 +0200 Subject: [PATCH] Some changes in sponge/spigot guis --- .../api/localization/LocalizationKeys.java | 1 + .../cz/neumimto/rpg/common/gui/GuiParser.java | 15 +- .../rpg/common/gui/StaticInventory.java | 24 +-- .../rpg/common/gui/TemplateInventory.java | 2 - .../persistance/dao/ClassDefinitionDao.java | 2 +- Common/src/main/resources/guis.conf | 36 +++- Implementations/Spigot/14/build.gradle | 2 +- .../neumimto/rpg/spigot/SpigotRpgPlugin.java | 2 +- .../spigot/commands/SpigotAdminCommands.java | 3 +- .../cz/neumimto/rpg/spigot/gui/SpigotGui.java | 23 --- .../rpg/spigot/gui/SpigotGuiHelper.java | 21 --- Implementations/Sponge/7/build.gradle | 8 +- .../neumimto/rpg/sponge/SpongeRpgPlugin.java | 13 +- .../rpg/sponge/bridges/HologramsListener.java | 134 ++++++++++++++ .../commands/SpongeCharacterCommands.java | 2 +- .../sponge/commands/SpongeCommandService.java | 3 - .../rpg/sponge/configuration/Settings.java | 28 --- .../entities/players/SpongeCharacter.java | 10 -- .../sponge/gui/ArmorAndWeaponMenuHelper.java | 166 ------------------ .../cz/neumimto/rpg/sponge/gui/GuiHelper.java | 147 ++++++++-------- .../rpg/sponge/gui/SpongeUIReader.java | 10 ++ .../rpg/sponge/gui/VanillaMessaging.java | 32 ++-- .../gui/vanilla/CharacterAttributesGui.java | 129 -------------- .../rpg/sponge/gui/vanilla/VanillaGui.java | 120 ------------- .../gui/vanilla/VanillaGuiListener.java | 8 - .../rpg/sponge/utils/math/VectorUtils.java | 11 ++ 26 files changed, 308 insertions(+), 644 deletions(-) create mode 100644 Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/bridges/HologramsListener.java delete mode 100644 Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/configuration/Settings.java delete mode 100644 Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/ArmorAndWeaponMenuHelper.java delete mode 100644 Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/vanilla/CharacterAttributesGui.java delete mode 100644 Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/vanilla/VanillaGui.java diff --git a/API/src/main/java/cz/neumimto/rpg/api/localization/LocalizationKeys.java b/API/src/main/java/cz/neumimto/rpg/api/localization/LocalizationKeys.java index c7fc32912..9b3079a56 100644 --- a/API/src/main/java/cz/neumimto/rpg/api/localization/LocalizationKeys.java +++ b/API/src/main/java/cz/neumimto/rpg/api/localization/LocalizationKeys.java @@ -70,6 +70,7 @@ public class LocalizationKeys { public static final String CONFIRM_SKILL_SELECTION_BUTTON = "gui.button.label.confirm.skill-selection"; public static final String WEAPONS_MENU_HELP = "gui.label.weapons.tooltip"; public static final String WEAPONS = "gui.label.weapons"; + public static final String ITEM_CLASS = "gui.label.item-class"; public static final String ARMOR = "gui.label.armor"; public static final String ARMOR_MENU_HELP = "gui.label.armor.tooltip"; public static final String PROPERTIES = "gui.label.generic.properties"; diff --git a/Common/src/main/java/cz/neumimto/rpg/common/gui/GuiParser.java b/Common/src/main/java/cz/neumimto/rpg/common/gui/GuiParser.java index 76845337d..e763af863 100644 --- a/Common/src/main/java/cz/neumimto/rpg/common/gui/GuiParser.java +++ b/Common/src/main/java/cz/neumimto/rpg/common/gui/GuiParser.java @@ -64,11 +64,13 @@ public Map initInventories(ClassLoader classLoader, String confN String type = s.getClassType(); Object[] context2 = new Object[]{ type, - (Supplier) () -> api.getClassService().getClassDefinitions() - .stream().filter(a -> a.getClassType().equals(type)) - .map(this::toItemStack) - .collect(Collectors.toList()) - .toArray(initArray(api.getClassService().getClassDefinitions().size())) + (Supplier) () -> { + List collect = api.getClassService().getClassDefinitions() + .stream().filter(a -> a.getClassType().equals(type)) + .map(this::toItemStack) + .collect(Collectors.toList()); + return collect.toArray(initArray(collect.size())); + } }; ConfigInventory c2 = createCachedMenu( sFactorz, guiName, gui, context2 @@ -227,7 +229,8 @@ private T parseItemsAndReturnBlank(Invocable i, Object context, Map processor) { @Override public void fill(I inventory) { - for (int i = 0; i < items.length; ) { - //does java automatically unroll loops? - - processor.setItem(items[i], inventory, i); - i++; - processor.setItem(items[i], inventory, i); - i++; - processor.setItem(items[i], inventory, i); - i++; - processor.setItem(items[i], inventory, i); - i++; - processor.setItem(items[i], inventory, i); - i++; - processor.setItem(items[i], inventory, i); - i++; - processor.setItem(items[i], inventory, i); - i++; - if (i == 54) { - return; - } - processor.setItem(items[i], inventory, i); - i++; + for (int i = 0; i < 54; i++) { processor.setItem(items[i], inventory, i); - i++; } } } \ No newline at end of file diff --git a/Common/src/main/java/cz/neumimto/rpg/common/gui/TemplateInventory.java b/Common/src/main/java/cz/neumimto/rpg/common/gui/TemplateInventory.java index 2f0fede17..90631482f 100644 --- a/Common/src/main/java/cz/neumimto/rpg/common/gui/TemplateInventory.java +++ b/Common/src/main/java/cz/neumimto/rpg/common/gui/TemplateInventory.java @@ -1,7 +1,5 @@ package cz.neumimto.rpg.common.gui; -import java.util.function.Function; - public class TemplateInventory extends DynamicInventory { public TemplateInventory(T[] items, T replaceToken, InventorySlotProcessor processor) { diff --git a/Common/src/main/java/cz/neumimto/rpg/common/persistance/dao/ClassDefinitionDao.java b/Common/src/main/java/cz/neumimto/rpg/common/persistance/dao/ClassDefinitionDao.java index cc80600ca..8faa1a545 100644 --- a/Common/src/main/java/cz/neumimto/rpg/common/persistance/dao/ClassDefinitionDao.java +++ b/Common/src/main/java/cz/neumimto/rpg/common/persistance/dao/ClassDefinitionDao.java @@ -114,7 +114,7 @@ private Map preloadClassDefs(Path path, Set set) } if (classTypeDefinition == null) { - error(" - Unknown ClassType; Allowed Class Types: " + String.join(", ", types.keySet())); + error(" - Unknown ClassType " + classType + "; Allowed Class Types: " + String.join(", ", types.keySet())); } else { classDefinition = new ClassDefinition(name, classType); map.put(name, p); diff --git a/Common/src/main/resources/guis.conf b/Common/src/main/resources/guis.conf index 178a771fd..8081f8847 100644 --- a/Common/src/main/resources/guis.conf +++ b/Common/src/main/resources/guis.conf @@ -51,17 +51,20 @@ gui: [ conditions: "" dynamic_space: "-" command: """ + if ("---".equals(command_)) { + return command_; + } return "ninfo classes " + context_.getName() """ } { type: classes_by_type inv: [ - " 0) { - List lore = new ArrayList<>(); - SpigotDamageService spigotDamageService = (SpigotDamageService) Rpg.get().getDamageService(); - String colorByDamage = spigotDamageService.getColorByDamage(damage); - lore.add(damageLabel + ":" + colorByDamage + damage); - itemMeta.setLore(lore); - } - itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES); - itemStack.setItemMeta(itemMeta); - - return unclickableIcon(itemStack); - } - - - public static Inventory createClassAttributesView(Player player, ClassDefinition cc) { String translate = Rpg.get().getLocalizationService().translate(LocalizationKeys.ATTRIBUTES); Map attrs = cc.getStartingAttributes(); diff --git a/Implementations/Sponge/7/build.gradle b/Implementations/Sponge/7/build.gradle index 3ab9acce8..27b7b5399 100644 --- a/Implementations/Sponge/7/build.gradle +++ b/Implementations/Sponge/7/build.gradle @@ -5,6 +5,12 @@ plugins { group = 'cz.neumimto.rpg' version = '2.1.0-SNAPSHOT-2' +repositories { + maven { + url 'https://jitpack.io' + } +} + compileJava { options.compilerArgs += ["-parameters"] options.fork = true @@ -26,7 +32,7 @@ dependencies { compileOnly 'me.lucko.luckperms:luckperms-api:4.2' compileOnly 'com.github.rojo8399:PlaceholderAPI:4.5.1' compileOnly 'it.unimi.dsi:fastutil:8.2.1' - + compileOnly 'com.github.randombyte-developer:holograms:v3.2.0' compile "co.aikar:acf-sponge:0.5.0-SNAPSHOT" testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.1' diff --git a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/SpongeRpgPlugin.java b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/SpongeRpgPlugin.java index f1acfd091..a81ed6d5d 100644 --- a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/SpongeRpgPlugin.java +++ b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/SpongeRpgPlugin.java @@ -22,7 +22,6 @@ import co.aikar.commands.CommandIssuer; import co.aikar.commands.InvalidCommandArgument; import co.aikar.commands.SpongeCommandManager; -import co.aikar.commands.sponge.contexts.OnlinePlayer; import com.google.inject.Inject; import com.google.inject.Injector; import cz.neumimto.rpg.api.Rpg; @@ -32,6 +31,7 @@ import cz.neumimto.rpg.api.utils.FileUtils; import cz.neumimto.rpg.common.commands.*; import cz.neumimto.rpg.persistence.flatfiles.FlatFilesModule; +import cz.neumimto.rpg.sponge.bridges.HologramsListener; import cz.neumimto.rpg.sponge.commands.*; import cz.neumimto.rpg.sponge.gui.GuiHelper; import cz.neumimto.rpg.sponge.gui.VanillaMessaging; @@ -361,12 +361,21 @@ public void initializeApi(GamePreInitializationEvent event) { if (Boolean.TRUE.equals(Rpg.get().getPluginConfig().ITEM_COOLDOWNS)) { Rpg.get().registerListeners(new SpongeItemCooldownListener()); } + + + try { + Class.forName("de.randombyte.holograms.api.class.HologramsService"); + injector.getInstance(HologramsListener.class).init(); + Log.info("Holograms inetgartions enabled"); + } catch (ClassNotFoundException e) { + + } + } ); Rpg.get().getSyncExecutor(); - info("NtRpg plugin successfully loaded in " + elapsedTime + " seconds"); } diff --git a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/bridges/HologramsListener.java b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/bridges/HologramsListener.java new file mode 100644 index 000000000..c9e7bf680 --- /dev/null +++ b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/bridges/HologramsListener.java @@ -0,0 +1,134 @@ +package cz.neumimto.rpg.sponge.bridges; + + +import com.flowpowered.math.vector.Vector2d; +import cz.neumimto.rpg.api.entity.IEntity; +import cz.neumimto.rpg.api.entity.players.IActiveCharacter; +import cz.neumimto.rpg.api.localization.LocalizationService; +import cz.neumimto.rpg.api.skills.ISkill; +import cz.neumimto.rpg.api.skills.PlayerSkillContext; +import cz.neumimto.rpg.api.skills.SkillService; +import cz.neumimto.rpg.api.utils.rng.XORShiftRnd; +import cz.neumimto.rpg.sponge.SpongeRpgPlugin; +import cz.neumimto.rpg.sponge.damage.SpongeDamageService; +import cz.neumimto.rpg.sponge.events.skill.SpongeSkillPostUsageEvent; +import cz.neumimto.rpg.sponge.skills.NDamageType; +import cz.neumimto.rpg.sponge.utils.math.VectorUtils; +import de.randombyte.holograms.api.HologramsService; +import de.randombyte.holograms.api.HologramsService.Hologram; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.entity.living.Living; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.cause.entity.damage.DamageType; +import org.spongepowered.api.event.cause.entity.damage.DamageTypes; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.format.TextColor; +import org.spongepowered.api.text.format.TextColors; +import org.spongepowered.api.world.Location; + +import javax.inject.Inject; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Optional; + +public class HologramsListener { + + @Inject + private SkillService skillService; + + @Inject + private SpongeDamageService damageService; + + @Inject + private LocalizationService localizationService; + + private static Map holograms = new HashMap<>(); + private static Map colors = new HashMap<>(); + + private HologramsService hologramsService; + + static { + put(NDamageType.FIRE, TextColors.RED); + put(DamageTypes.MAGMA, TextColors.RED); + + put(DamageTypes.SUFFOCATE, TextColors.BLUE); + put(DamageTypes.DROWN, TextColors.BLUE); + put(NDamageType.LIGHTNING, TextColors.YELLOW); + + put(DamageTypes.MAGIC, TextColors.DARK_AQUA); + + put(DamageTypes.VOID, TextColors.DARK_PURPLE); + put(DamageTypes.ATTACK, TextColors.GRAY); + put(DamageTypes.SWEEPING_ATTACK, TextColors.GRAY); + + displayLocs = VectorUtils.circle(new Vector2d[20], 2); + } + + public void init() { + + Sponge.getEventManager().registerListeners(SpongeRpgPlugin.getInstance(), this); + hologramsService = Sponge.getServiceManager().provide(HologramsService.class).get(); + + Sponge.getScheduler().createTaskBuilder() + .intervalTicks(30) + .execute(() -> { + Iterator> iterator = holograms.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry next = iterator.next(); + Hologram key = next.getKey(); + if (next.getValue() < System.currentTimeMillis()) { + key.remove(); + } + } + }) + .submit(SpongeRpgPlugin.getInstance()); + + } + + private static Vector2d[] displayLocs; + private static XORShiftRnd rnd = new XORShiftRnd(); + + + + private static void put(DamageType damageCause, TextColor tx) { + colors.put(damageCause, tx); + } + + private static Vector2d getLocation() { + int i = rnd.nextInt(displayLocs.length - 1); + return displayLocs[i]; + } + + + @Listener + public void onSkillCast(SpongeSkillPostUsageEvent event) { + ISkill skill = event.getSkill(); + String damageTypes = skill.getDamageType(); + IEntity caster = event.getCaster(); + Living entity = (Living) caster.getEntity(); + + Optional type = Sponge.getRegistry().getType(DamageType.class, damageTypes); + DamageType damageType = type.get(); + TextColor s = colors.get(damageType); + if (s == null) { + s = TextColors.WHITE; + } + + + IActiveCharacter c = (IActiveCharacter) caster; + PlayerSkillContext info = c.getSkillInfo(skill.getId()); + String skillName = info.getSkillData().getSkillName(); + //ran as nadmin skill we have no context to grab name from + if (skillName == null) { + skillName = info.getSkill().getId(); + } + + Vector2d vec = getLocation(); + Location location = entity.getLocation().add(vec.getX(), 2,vec.getY()); + Optional opt = hologramsService.createHologram(location, Text.of(s, skillName)); + opt.ifPresent(o -> holograms.put(o, System.currentTimeMillis() + 2500L)); + } + +} + diff --git a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/commands/SpongeCharacterCommands.java b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/commands/SpongeCharacterCommands.java index 31e996dc0..f822ffb23 100644 --- a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/commands/SpongeCharacterCommands.java +++ b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/commands/SpongeCharacterCommands.java @@ -55,7 +55,7 @@ public void deleteCharacter(Player executor, String name) { //TODO } - @Subcommand("set-class") + @Subcommand("choose class") public void chooseClassCommand(IActiveCharacter character, ClassDefinition classDefinition) { characterCommandFacade.commandChooseClass(character, classDefinition); } diff --git a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/commands/SpongeCommandService.java b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/commands/SpongeCommandService.java index 4038bcfe3..b73a991f4 100644 --- a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/commands/SpongeCommandService.java +++ b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/commands/SpongeCommandService.java @@ -32,7 +32,6 @@ import cz.neumimto.rpg.sponge.inventory.runewords.RWService; import cz.neumimto.rpg.sponge.utils.TextHelper; import org.spongepowered.api.Sponge; -import org.spongepowered.api.command.CommandCallable; import org.spongepowered.api.command.CommandResult; import org.spongepowered.api.command.args.GenericArguments; import org.spongepowered.api.command.spec.CommandSpec; @@ -40,8 +39,6 @@ import org.spongepowered.api.text.Text; import org.spongepowered.api.text.serializer.TextSerializers; -import java.lang.reflect.InvocationTargetException; -import java.util.List; import javax.inject.Inject; import javax.inject.Singleton; diff --git a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/configuration/Settings.java b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/configuration/Settings.java deleted file mode 100644 index 0fbce1c7d..000000000 --- a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/configuration/Settings.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2015, NeumimTo https://github.com/NeumimTo - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package cz.neumimto.rpg.sponge.configuration; - -/** - * Created by NeumimTo on 27.12.2014. - */ -public class Settings { - - public static boolean ENABLED_GUI = false; - -} diff --git a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/entities/players/SpongeCharacter.java b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/entities/players/SpongeCharacter.java index 5ec66472a..d1bd72839 100644 --- a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/entities/players/SpongeCharacter.java +++ b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/entities/players/SpongeCharacter.java @@ -1,10 +1,8 @@ package cz.neumimto.rpg.sponge.entities.players; -import cz.neumimto.rpg.api.entity.players.IActiveCharacter; import cz.neumimto.rpg.api.persistance.model.CharacterBase; import cz.neumimto.rpg.common.entity.players.ActiveCharacter; import cz.neumimto.rpg.sponge.entities.players.party.SpongeParty; -import cz.neumimto.rpg.sponge.gui.ArmorAndWeaponMenuHelper; import cz.neumimto.rpg.sponge.gui.SpongeSkillTreeViewModel; import cz.neumimto.rpg.sponge.utils.TextHelper; import org.spongepowered.api.entity.living.player.Player; @@ -53,12 +51,4 @@ public SpongeSkillTreeViewModel getLastTimeInvokedSkillTreeView() { return null; } - - @Override - public IActiveCharacter updateItemRestrictions() { - ArmorAndWeaponMenuHelper.reset(this); - return super.updateItemRestrictions(); - } - - } diff --git a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/ArmorAndWeaponMenuHelper.java b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/ArmorAndWeaponMenuHelper.java deleted file mode 100644 index 69a93c061..000000000 --- a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/ArmorAndWeaponMenuHelper.java +++ /dev/null @@ -1,166 +0,0 @@ -package cz.neumimto.rpg.sponge.gui; - -import com.google.common.collect.Lists; -import cz.neumimto.rpg.api.Rpg; -import cz.neumimto.rpg.api.entity.players.IActiveCharacter; -import cz.neumimto.rpg.api.localization.LocalizationKeys; -import cz.neumimto.rpg.common.items.RpgItemTypeImpl; -import cz.neumimto.rpg.sponge.SpongeRpgPlugin; -import cz.neumimto.rpg.sponge.damage.SpongeDamageService; -import cz.neumimto.rpg.sponge.items.SpongeRpgItemType; -import cz.neumimto.rpg.sponge.utils.TextHelper; -import org.spongepowered.api.data.key.Keys; -import org.spongepowered.api.data.type.DyeColors; -import org.spongepowered.api.item.ItemTypes; -import org.spongepowered.api.item.inventory.Inventory; -import org.spongepowered.api.item.inventory.InventoryArchetypes; -import org.spongepowered.api.item.inventory.ItemStack; -import org.spongepowered.api.item.inventory.property.InventoryTitle; -import org.spongepowered.api.item.inventory.property.SlotPos; -import org.spongepowered.api.item.inventory.query.QueryOperationTypes; -import org.spongepowered.api.text.Text; -import org.spongepowered.api.text.format.TextColor; -import org.spongepowered.api.text.format.TextStyles; - -import java.util.*; -import java.util.stream.Collectors; - -public class ArmorAndWeaponMenuHelper { - - private static Map> armor = new HashMap<>(); - private static Map> weapon = new HashMap<>(); - - - public static Inventory listArmor(IActiveCharacter character, int idx) { - List inventories = armor.get(character.getUUID()); - if (inventories == null) { - inventories = buildArmorViewFor(character); - armor.put(character.getUUID(), inventories); - } - return inventories.get(idx); - } - - public static Inventory listWeapons(IActiveCharacter character, int idx) { - List inventories = weapon.get(character.getUUID()); - if (inventories == null) { - inventories = buildWeaponViewFor(character); - weapon.put(character.getUUID(), inventories); - } - return inventories.get(idx); - } - - private static List buildArmorViewFor(IActiveCharacter character) { - List i = new ArrayList<>(); - - Set allowedArmor = character.getAllowedArmor(); - List list = allowedArmor.stream().sorted( - Comparator.comparingDouble(RpgItemTypeImpl::getArmor) - .thenComparing(RpgItemTypeImpl::getId)) - .collect(Collectors.toList()); - final int max = list.size(); - int q = 0; - Inventory inventory; - List> partition = Lists.partition(list, 21); - - for (List inner : partition) { - inventory = prepareArmorInventory(q, max, "Armor", "armor"); - fillInventoryWithItems(inventory, inner); - i.add(inventory); - q++; - } - - return i; - } - - - private static List buildWeaponViewFor(IActiveCharacter character) { - List i = new ArrayList<>(); - - Map allowedWeapons = character.getAllowedWeapons(); - List> list = new ArrayList<>(allowedWeapons.entrySet().stream().sorted(Map.Entry.comparingByValue()) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, - LinkedHashMap::new)).entrySet()); - final int max = list.size(); - int q = 0; - Inventory inventory; - List>> partition = Lists.partition(list, 21); - - for (List> inner : partition) { - inventory = prepareArmorInventory(q, max, "Weapons", "weapon"); - fillInventoryWithWeaponItems(inventory, inner); - i.add(inventory); - q++; - } - - return i; - } - - private static void fillInventoryWithWeaponItems(Inventory inventory, List> inner) { - int row = 2; - int column = 1; - String iDmgLabel = Rpg.get().getLocalizationService().translate(LocalizationKeys.ITEM_DAMAGE); - for (Map.Entry entry : inner) { - SpongeRpgItemType spongeRpgItemType = entry.getKey(); - ItemStack itemStack = GuiHelper.itemStack(spongeRpgItemType.getItemType()); - TextColor colorByDamage = ((SpongeDamageService)Rpg.get().getDamageService()).getColorByDamage(entry.getValue()); - Text t = Text.builder(iDmgLabel + ": " + entry.getValue()).color(colorByDamage).style(TextStyles.BOLD).build(); - itemStack.offer(Keys.ITEM_LORE, Collections.singletonList(t)); - inventory.query(QueryOperationTypes.INVENTORY_PROPERTY.of(SlotPos.of(column,row))).offer(itemStack); - if ((column) % 7 == 0) { - column = 1; - row++; - } else { - column++; - } - } - } - - private static void fillInventoryWithItems(Inventory inventory, List inner) { - int row = 2; - int column = 1; - for (SpongeRpgItemType spongeRpgItemType : inner) { - ItemStack itemStack = GuiHelper.itemStack(spongeRpgItemType.getItemType()); - inventory.query(QueryOperationTypes.INVENTORY_PROPERTY.of(SlotPos.of(column,row))).offer(itemStack); - if ((column) % 7 == 0) { - column = 1; - row++; - } else { - column++; - } - } - } - - private static Inventory prepareArmorInventory(int idx, int max, String title, String command) { - Inventory i = Inventory.builder() - .of(InventoryArchetypes.DOUBLE_CHEST) - .property(InventoryTitle.of(Text.of(title))) - .build(SpongeRpgPlugin.getInstance()); - GuiHelper.makeBorder(i, DyeColors.ORANGE); - String translate = Rpg.get().getLocalizationService().translate(LocalizationKeys.BACK); - - ItemStack back = GuiHelper.back("char", TextHelper.parse(translate)); - i.offer(back); - - if (idx * 21 < max) { - translate = Rpg.get().getLocalizationService().translate(LocalizationKeys.NEXT); - ItemStack c = GuiHelper.command("char " + command + " " + (idx + 1), TextHelper.parse(translate), ItemTypes.GLOWSTONE_DUST); - i.offer(c); - } - if (idx > 0) { - translate = Rpg.get().getLocalizationService().translate(LocalizationKeys.BACK); - ItemStack c = GuiHelper.command("char " + command + " " + (idx - 1), TextHelper.parse(translate), ItemTypes.REDSTONE); - i.offer(c); - } - return i; - } - - public static void reset(IActiveCharacter character) { - armor.remove(character.getUUID()); - weapon.remove(character.getUUID()); - } - - public static void resetAll() { - armor.clear(); - weapon.clear(); - } -} diff --git a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/GuiHelper.java b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/GuiHelper.java index e75aff383..a5e7bbfa7 100644 --- a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/GuiHelper.java +++ b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/GuiHelper.java @@ -6,8 +6,9 @@ import cz.neumimto.rpg.api.entity.players.IActiveCharacter; import cz.neumimto.rpg.api.entity.players.classes.ClassDefinition; import cz.neumimto.rpg.api.entity.players.classes.PlayerClassData; -import cz.neumimto.rpg.api.items.ClassItem; +import cz.neumimto.rpg.api.items.RpgItemType; import cz.neumimto.rpg.api.localization.Arg; +import cz.neumimto.rpg.api.localization.Localization; import cz.neumimto.rpg.api.localization.LocalizationKeys; import cz.neumimto.rpg.api.localization.LocalizationService; import cz.neumimto.rpg.api.logging.Log; @@ -19,20 +20,16 @@ import cz.neumimto.rpg.common.gui.DynamicInventory; import cz.neumimto.rpg.common.gui.TemplateInventory; import cz.neumimto.rpg.sponge.SpongeRpgPlugin; -import cz.neumimto.rpg.sponge.damage.SpongeDamageService; import cz.neumimto.rpg.sponge.entities.players.ISpongeCharacter; import cz.neumimto.rpg.sponge.inventory.data.InventoryCommandItemMenuData; import cz.neumimto.rpg.sponge.inventory.data.MenuInventoryData; import cz.neumimto.rpg.sponge.inventory.data.SkillTreeInventoryViewControllsData; -import cz.neumimto.rpg.sponge.items.SpongeRpgItemType; import cz.neumimto.rpg.sponge.listeners.SkillTreeInventoryListener; import cz.neumimto.rpg.sponge.skills.NDamageType; import cz.neumimto.rpg.sponge.utils.TextHelper; import cz.neumimto.rpg.sponge.utils.Utils; import org.spongepowered.api.Sponge; import org.spongepowered.api.block.BlockTypes; -import org.spongepowered.api.data.DataContainer; -import org.spongepowered.api.data.DataQuery; import org.spongepowered.api.data.key.Keys; import org.spongepowered.api.data.type.DyeColor; import org.spongepowered.api.data.type.DyeColors; @@ -68,9 +65,9 @@ public class GuiHelper { public static Map damageTypeToItemStack = new HashMap<>(); public static Text JOINT = Text.of(TextColors.DARK_GRAY, "[", TextColors.DARK_RED, "+", TextColors.DARK_GRAY, "]"); - public static Text HEADER_START = Text.of(TextColors.DARK_GRAY,"════════ [ "); - public static Text HEADER_END = Text.of(TextColors.DARK_GRAY," ] ════════"); - public static Text VERTICAL_LINE = Text.of(TextColors.DARK_GRAY, "║ " , TextColors.GRAY); + public static Text HEADER_START = Text.of(TextColors.DARK_GRAY, "════════ [ "); + public static Text HEADER_END = Text.of(TextColors.DARK_GRAY, " ] ════════"); + public static Text VERTICAL_LINE = Text.of(TextColors.DARK_GRAY, "║ ", TextColors.GRAY); public static Set SKILL_SETTINGS_DURATION_NODES = new HashSet<>(); public static Map CACHED_MENUS = new HashMap<>(); @@ -120,7 +117,7 @@ public static Text header(String header) { } public static Text header(String header, TextColor textColor) { - return Text.of(JOINT, HEADER_START, textColor, header , HEADER_END); + return Text.of(JOINT, HEADER_START, textColor, header, HEADER_END); } public static Text line(String line) { @@ -128,7 +125,7 @@ public static Text line(String line) { } public static Text node(String key, String value) { - return Text.of(VERTICAL_LINE, TextColors.GRAY, Rpg.get().getLocalizationService().translate(key),TextColors.DARK_GRAY, ": " + value); + return Text.of(VERTICAL_LINE, TextColors.GRAY, Rpg.get().getLocalizationService().translate(key), TextColors.DARK_GRAY, ": " + value); } @@ -140,20 +137,6 @@ public static ItemStack itemStack(ItemType type) { return is; } - public static ItemStack isItemStackUnclickable(ItemStack i) { - DataQuery unsafeDataQ = DataQuery.of("UnsafeData", "my.nbt.path"); - DataContainer container = i.toContainer(); - container.set(unsafeDataQ, true); - i = ItemStack.builder().fromContainer(container).build(); - return i; - } - - public static void makeItemStackUnclickable(ItemStack i) { - DataQuery.of("UnsafeData", "ntrpg:menu_inventory"); - } - - - private static ItemStack damageTypeToItemStack(String type) { if (type == null) { return itemStack(ItemTypes.STONE); @@ -169,51 +152,15 @@ private static ItemStack damageTypeToItemStack(String type) { return is; } - static Inventory createMenuInventoryClassTypeView(String type) { - ClassTypeDefinition classTypeDefinition = Rpg.get().getPluginConfig().CLASS_TYPES.get(type); - Inventory i = Inventory.builder().of(InventoryArchetypes.DOUBLE_CHEST) - .property(InventoryTitle.of( - Text.builder("[ ").color(toTextColor(classTypeDefinition.getSecondaryColor())) - .append(Text.builder(type).color(toTextColor(classTypeDefinition.getPrimaryColor())).style(TextStyles.BOLD).build()) - .append(Text.builder(" ]").color(toTextColor(classTypeDefinition.getSecondaryColor())).build()) - .build())) - .build(SpongeRpgPlugin.getInstance()); - GuiHelper.makeBorder(i, toDyeColor(classTypeDefinition.getDyeColor())); - - Rpg.get().getClassService().getClassDefinitions().stream() - .filter(a -> a.getClassType().equalsIgnoreCase(type)) - .forEach(a -> i.offer(GuiHelper.toItemStack(a))); - - i.query(QueryOperationTypes.INVENTORY_PROPERTY.of(SlotPos.of(7, 4))).offer(back("ninfo classes", translate(LocalizationKeys.BACK))); - - return i; - } - public static Inventory createMenuInventoryClassTypesView() { - Inventory i = Inventory.builder().of(InventoryArchetypes.DOUBLE_CHEST) - .property(InventoryTitle.of(translate(LocalizationKeys.CLASS_TYPES))) - .build(SpongeRpgPlugin.getInstance()); - - makeBorder(i, DyeColors.WHITE); - - for (String type : Rpg.get().getPluginConfig().CLASS_TYPES.keySet()) { - i.offer(createClassTypeDefinitionCommand(type)); - } - - return i; + return CACHED_MENUS.get("class_types"); } - public static ItemStack createClassTypeDefinitionCommand(String type) { - ItemStack i = itemStack(ItemTypes.CRAFTING_TABLE); - i.offer(new MenuInventoryData(true)); - i.offer(Keys.DISPLAY_NAME, - Text.builder(type) - .color(toTextColor(Rpg.get().getPluginConfig().CLASS_TYPES.get(type).getPrimaryColor())) - .build()); - i.offer(new InventoryCommandItemMenuData("ninfo classes " + type)); - return i; + public static Inventory createMenuInventoryClassesByTypeView(String classType) { + return CACHED_MENUS.get("classes_by_type" + classType); } + public static ItemStack propertyToItemStack(int id, float value) { ItemStack i = itemStack(ItemTypes.BOOK); String nameById = Rpg.get().getPropertyService().getNameById(id); @@ -339,9 +286,9 @@ public static List toLore(ISpongeCharacter character, SkillData skillData, lore.add(node(locService.translate(LocalizationKeys.SKILL_EXECUTION_TYPE), locService.translate(skill.getSkillExecutionType().toString().toLowerCase()))); PlayerSkillContext psc = character.getSkillInfo(skill); - String level = psc == null ? " -- " : psc.getLevel() + (psc.getLevel() != psc.getTotalLevel() ? " ("+psc.getTotalLevel()+")" :""); + String level = psc == null ? " -- " : psc.getLevel() + (psc.getLevel() != psc.getTotalLevel() ? " (" + psc.getTotalLevel() + ")" : ""); lore.add(node(locService.translate(LocalizationKeys.LEVEL), level)); - lore.add(node(locService.translate(LocalizationKeys.SKILL_MAX_LEVEL), ""+ skillData.getMaxSkillLevel())); + lore.add(node(locService.translate(LocalizationKeys.SKILL_MAX_LEVEL), "" + skillData.getMaxSkillLevel())); if (skillData.getMinPlayerLevel() > 0) { lore.add(node(locService.translate(LocalizationKeys.SKILL_MIN_CLASS_LEVEL), "" + skillData.getMinPlayerLevel())); } @@ -421,7 +368,7 @@ public static List toLore(ISpongeCharacter character, SkillData skillData, while (iterator.hasNext()) { i++; ISkillType next = iterator.next(); - String translate = locService.translate(next.toString())+" "; + String translate = locService.translate(next.toString()) + " "; builder.append(translate); if (i % 4 == 0) { if (firstLine) { @@ -441,8 +388,6 @@ public static List toLore(ISpongeCharacter character, SkillData skillData, } - - @SuppressWarnings("unchecked") private static TextColor getSkillTextColor(IActiveCharacter character, ISkill skill, SkillData skillData, SkillTree skillTree) { if (character.hasSkill(skillData.getSkillId())) { @@ -740,4 +685,68 @@ public static Inventory createInventoryTemplate(String title) { .build(SpongeRpgPlugin.getInstance()); } + public static Inventory getCharacterAllowedArmor(ISpongeCharacter character, int page) { + String name = "char_allowed_items_armor" + character.getName(); + Inventory inventory = CACHED_MENUS.get(name); + if (inventory == null) { + TemplateInventory dView = (TemplateInventory) CACHED_MENU_TEMPLATES.get("char_view"); + Map allowedWeapons = character.getAllowedWeapons(); + List content = new ArrayList<>(); + for (Map.Entry ent : allowedWeapons.entrySet()) { + RpgItemType key = ent.getKey(); + Double value = ent.getValue(); + ItemStack is = toItemStack(key, value); + content.add(is); + } + DynamicInventory inv = dView.setActualContent(content.toArray(new ItemStack[content.size() == 0 ? 0 : content.size() - 1])); + String translate = Rpg.get().getLocalizationService().translate(LocalizationKeys.CHARACTER_ARMOR); + inventory = createInventoryTemplate(translate); + inv.fill(inventory); + CACHED_MENUS.put(name, inventory); + } + return inventory; + } + + public static Inventory getCharacterAllowedWeapons(ISpongeCharacter character, int page) { + String name = "char_allowed_items_weapons" + character.getName(); + Inventory inventory = CACHED_MENUS.get(name); + if (inventory == null) { + TemplateInventory dView = (TemplateInventory) CACHED_MENU_TEMPLATES.get("char_view"); + Set allowedWeapons = character.getAllowedArmor(); + List content = new ArrayList<>(); + for (RpgItemType ent : allowedWeapons) { + ItemStack is = toItemStack(ent, 0); + content.add(is); + } + DynamicInventory inv = dView.setActualContent(content.toArray(new ItemStack[content.size() == 0 ? 0 : content.size() - 1])); + String translate = Rpg.get().getLocalizationService().translate(LocalizationKeys.CHARACTER_ARMOR); + inventory = createInventoryTemplate(translate); + inv.fill(inventory); + CACHED_MENUS.put(name, inventory); + } + return inventory; + } + + private static ItemStack toItemStack(RpgItemType key, double damage) { + String id = key.getId(); + Optional type = Sponge.getRegistry().getType(ItemType.class, id); + List lore = new ArrayList<>(); + ItemStack is = ItemStack.of(type.get()); + + LocalizationService localizationService = Rpg.get().getLocalizationService(); + String translate = localizationService.translate(LocalizationKeys.ITEM_CLASS); + lore.add(Text.of(TextColors.GRAY, translate, ": ", TextColors.GREEN, key.getItemClass().getName())); + if (damage > 0) { + translate = localizationService.translate(LocalizationKeys.ITEM_DAMAGE); + lore.add(Text.of(TextColors.GRAY, translate, ": ", TextColors.RED, damage)); + } + if (Rpg.get().getPluginConfig().DEBUG.isBalance()) { + //tood 1.15 + } + + is.offer(new MenuInventoryData(true)); + is.offer(Keys.ITEM_LORE, lore); + return is; + } + } diff --git a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/SpongeUIReader.java b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/SpongeUIReader.java index 2bee2913a..1ad271c08 100644 --- a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/SpongeUIReader.java +++ b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/SpongeUIReader.java @@ -13,6 +13,8 @@ import cz.neumimto.rpg.sponge.utils.TextHelper; import org.spongepowered.api.Sponge; import org.spongepowered.api.data.key.Keys; +import org.spongepowered.api.data.type.DyeColor; +import org.spongepowered.api.data.type.DyeColors; import org.spongepowered.api.item.ItemType; import org.spongepowered.api.item.ItemTypes; import org.spongepowered.api.item.inventory.Inventory; @@ -61,17 +63,21 @@ protected ItemStack toItemStack(ClassDefinition a) { @Override protected ItemStack itemStringToItemStack(String[] split, Supplier command) { String s = split[2]; + DyeColor dye = null; if (s.equals("minecraft:oak_sapling")) { s = "minecraft:sapling"; } if (s.equals("minecraft:gray_stained_glass_pane")) { s = "minecraft:stained_glass_pane"; + dye = DyeColors.GRAY; } if (s.equals("minecraft:red_stained_glass_pane")) { s = "minecraft:stained_glass_pane"; + dye = DyeColors.RED; } if (s.equals("minecraft:white_stained_glass_pane")) { s = "minecraft:stained_glass_pane"; + dye = DyeColors.WHITE; } Optional type = Sponge.getRegistry().getType(ItemType.class, s); ItemType itemType = type.get(); @@ -79,6 +85,10 @@ protected ItemStack itemStringToItemStack(String[] split, Supplier comma itemStack.offer(Keys.HIDE_ATTRIBUTES, true); itemStack.offer(Keys.HIDE_ENCHANTMENTS, true); + if (dye != null) { + itemStack.offer(Keys.DYE_COLOR, dye); + } + int i1 = Integer.parseInt(split[3]); if (i1 > 0) { //todo 1.15 diff --git a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/VanillaMessaging.java b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/VanillaMessaging.java index bfe70bbc0..faaec6930 100644 --- a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/VanillaMessaging.java +++ b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/VanillaMessaging.java @@ -31,7 +31,6 @@ import cz.neumimto.rpg.api.entity.players.classes.PlayerClassData; import cz.neumimto.rpg.api.gui.IPlayerMessage; import cz.neumimto.rpg.api.inventory.CannotUseItemReason; -import cz.neumimto.rpg.api.items.ClassItem; import cz.neumimto.rpg.api.localization.Arg; import cz.neumimto.rpg.api.localization.LocalizationKeys; import cz.neumimto.rpg.api.localization.LocalizationService; @@ -40,8 +39,6 @@ import cz.neumimto.rpg.api.skills.SkillData; import cz.neumimto.rpg.api.skills.tree.SkillTree; import cz.neumimto.rpg.common.effects.InternalEffectSourceProvider; -import cz.neumimto.rpg.common.gui.DynamicInventory; -import cz.neumimto.rpg.common.gui.TemplateInventory; import cz.neumimto.rpg.common.inventory.crafting.runewords.ItemUpgrade; import cz.neumimto.rpg.common.inventory.crafting.runewords.Rune; import cz.neumimto.rpg.common.inventory.runewords.RuneWord; @@ -54,17 +51,13 @@ import cz.neumimto.rpg.sponge.effects.common.def.ManaBarNotifier; import cz.neumimto.rpg.sponge.entities.players.ISpongeCharacter; import cz.neumimto.rpg.sponge.entities.players.SpongeCharacterService; -import cz.neumimto.rpg.sponge.gui.vanilla.CharacterAttributesGui; import cz.neumimto.rpg.sponge.inventory.data.InventoryCommandItemMenuData; import cz.neumimto.rpg.sponge.inventory.data.MenuInventoryData; import cz.neumimto.rpg.sponge.inventory.data.SkillTreeInventoryViewControllsData; import cz.neumimto.rpg.sponge.inventory.data.manipulators.SkillTreeNode; import cz.neumimto.rpg.sponge.inventory.runewords.RWService; -import cz.neumimto.rpg.sponge.items.SpongeRpgItemType; import cz.neumimto.rpg.sponge.skills.SpongeSkillService; -import cz.neumimto.rpg.sponge.utils.ItemStackUtils; import cz.neumimto.rpg.sponge.utils.TextHelper; -import cz.neumimto.rpg.sponge.utils.Utils; import org.spongepowered.api.Game; import org.spongepowered.api.Sponge; import org.spongepowered.api.data.key.Keys; @@ -94,10 +87,10 @@ import javax.inject.Inject; import javax.inject.Singleton; import java.util.*; -import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; -import static cz.neumimto.rpg.sponge.gui.GuiHelper.*; +import static cz.neumimto.rpg.sponge.gui.GuiHelper.back; +import static cz.neumimto.rpg.sponge.gui.GuiHelper.getItemLore; /** * Created by NeumimTo on 6.8.2015. @@ -673,16 +666,16 @@ private void createSkillTreeView(ISpongeCharacter character, Inventory skillTree @Override public void sendClassesByType(ISpongeCharacter character, String type) { - Inventory i = GuiHelper.createMenuInventoryClassTypeView(type); + Inventory i = GuiHelper.createMenuInventoryClassesByTypeView(type); character.getPlayer().openInventory(i); } @Override public void sendClassTypes(ISpongeCharacter character) { - Inventory i = GuiHelper.createMenuInventoryClassTypesView(); - - character.getPlayer().openInventory(i); + Player player = character.getPlayer(); + Inventory inventory = GuiHelper.createMenuInventoryClassTypesView(); + player.openInventory(inventory); } @Override @@ -695,7 +688,6 @@ public void displayCharacterMenu(ISpongeCharacter cc) { @Override public void displayCharacterAttributes(ISpongeCharacter character) { character.setAttributesTransaction(new HashMap<>()); - character.getPlayer().openInventory(new CharacterAttributesGui(character).getInventory()); } @Override @@ -709,18 +701,14 @@ public void displayCurrentClicks(ISpongeCharacter character, String combo) { @Override public void displayCharacterArmor(ISpongeCharacter character, int page) { - Inventory inventory = ArmorAndWeaponMenuHelper.listArmor(character, page); - if (inventory != null) { - character.getPlayer().openInventory(inventory); - } + Inventory i = GuiHelper.getCharacterAllowedArmor(character, page); + character.getPlayer().openInventory(i); } @Override public void displayCharacterWeapons(ISpongeCharacter character, int page) { - Inventory inventory = ArmorAndWeaponMenuHelper.listWeapons(character, page); - if (inventory != null) { - character.getPlayer().openInventory(inventory); - } + Inventory i = GuiHelper.getCharacterAllowedWeapons(character, page); + character.getPlayer().openInventory(i); } private Text translate(String key) { diff --git a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/vanilla/CharacterAttributesGui.java b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/vanilla/CharacterAttributesGui.java deleted file mode 100644 index e08d3ee2b..000000000 --- a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/vanilla/CharacterAttributesGui.java +++ /dev/null @@ -1,129 +0,0 @@ -package cz.neumimto.rpg.sponge.gui.vanilla; - -import cz.neumimto.rpg.api.Rpg; -import cz.neumimto.rpg.api.configuration.AttributeConfig; -import cz.neumimto.rpg.api.configuration.PluginConfig; -import cz.neumimto.rpg.api.gui.Gui; -import cz.neumimto.rpg.api.localization.LocalizationKeys; -import cz.neumimto.rpg.sponge.entities.players.ISpongeCharacter; -import cz.neumimto.rpg.sponge.utils.TextHelper; -import org.spongepowered.api.Sponge; -import org.spongepowered.api.data.key.Keys; -import org.spongepowered.api.data.type.DyeColors; -import org.spongepowered.api.item.ItemTypes; -import org.spongepowered.api.item.inventory.ItemStack; -import org.spongepowered.api.item.inventory.property.SlotPos; -import org.spongepowered.api.text.Text; -import org.spongepowered.api.text.format.TextColors; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.stream.Collectors; - -public class CharacterAttributesGui extends VanillaGui { - - private ISpongeCharacter character; - - public CharacterAttributesGui(ISpongeCharacter character) { - super(character.getPlayer().getUniqueId()); - - this.character = character; - this.inventory = characterEmptyInventory(character).build(plugin); - makeBorder(DyeColors.ORANGE); - - createAttributeRow(); - createCommitAttributeTxButton(); - - setItem(SlotPos.of(1, 1), itemAction(ItemTypes.PAPER, translate(LocalizationKeys.BACK), Gui::displayCharacterMenu)); - - PluginConfig pluginConfig = Rpg.get().getPluginConfig(); - if (pluginConfig.RESPEC_ATTRIBUTES) { - ItemStack btnRespec = itemCommand(ItemTypes.BARRIER, translate(LocalizationKeys.RESPEC_ATTRIBUTES), "char attributes-respec"); - setItem(SlotPos.of(7, 5), btnRespec); - } - } - - private void createAttributeRow() { - int attributePoints = character.getAttributePoints(); - for (Integer value : character.getAttributesTransaction().values()) { - attributePoints -= value; - } - - Collection attribs = Rpg.get().getPropertyService().getAttributes().values(); - List allOf = attribs.stream().filter(a -> !a.isHidden()).collect(Collectors.toList()); - int q = 1; - for (AttributeConfig attribute : allOf) { - - List lore = TextHelper.splitStringByDelimiter(attribute.getDescription()); - - int totalAmount = 0; - - Integer amount = character.getCharacterBase().getAttributes().get(attribute.getId()); - amount = amount == null ? 0 : amount; - totalAmount += amount; - - Integer transientAmount = character.getTransientAttributes().get(attribute.getId()); - transientAmount = transientAmount == null ? 0 : totalAmount; - totalAmount += transientAmount; - - Integer transacted = character.getAttributesTransaction().get(attribute.getId()); - transacted = transacted == null ? 0 : transacted; - - String attrString = attribute.getName() + " (" + totalAmount + ")"; - String transactedString = transacted != 0 ? " (+" + transacted + ")" : ""; - lore.add(Text.builder(amount + transactedString + " / " + attribute.getMaxValue()).build()); - - ItemStack itemStack = getItem(SlotPos.of(q, 3)); - if (!itemStack.isEmpty()) { - itemStack.offer(Keys.DISPLAY_NAME, Text.of(attrString)); - itemStack.offer(Keys.ITEM_LORE, lore); - } else { - itemStack = itemStack(itemType(attribute.getItemType()), Text.of(attrString), lore); - setItem(SlotPos.of(q, 3), itemStack); - } - - if (attributePoints > 0) { - String command = "char attribute " + attribute.getId() + " 1"; - ItemStack btn = getItem(SlotPos.of(q, 2)); - if (btn.isEmpty()) { - btn = itemAction(ItemTypes.SUGAR, Text.of(TextColors.GREEN, "+"), (character) -> { - Sponge.getCommandManager().process(character.getPlayer(), command); - createAttributeRow(); - createCommitAttributeTxButton(); - }); - setItem(SlotPos.of(q, 2), btn); - } - } else { - setItem(SlotPos.of(q, 2), ItemStack.empty()); - } - - q++; - } - } - - private void createCommitAttributeTxButton() { - int attributePoints = character.getAttributePoints(); - for (Integer value : character.getAttributesTransaction().values()) { - attributePoints -= value; - } - - Text name = Text.builder("Commit").color(TextColors.GREEN).build(); - List lore = new ArrayList<>(); - lore.add(Text.builder("Remaining Attribute Points: " + attributePoints).color(attributePoints == 0 ? TextColors.RED : TextColors.GREEN).build()); - - ItemStack commit = getItem(SlotPos.of(7, 1)); - if (commit.isEmpty()) { - commit = itemAction(ItemTypes.DIAMOND, name, c -> { - Sponge.getCommandManager().process(character.getPlayer(), "char tx-attribute-commit"); - Gui.displayCharacterMenu(c); - }); - setItem(SlotPos.of(7, 1), commit); - } - commit.offer(Keys.ITEM_LORE, lore); - } - - protected Text translate(String id) { - return TextHelper.parse(Rpg.get().getLocalizationService().translate(id)); - } -} diff --git a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/vanilla/VanillaGui.java b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/vanilla/VanillaGui.java deleted file mode 100644 index e5880550c..000000000 --- a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/vanilla/VanillaGui.java +++ /dev/null @@ -1,120 +0,0 @@ -package cz.neumimto.rpg.sponge.gui.vanilla; - -import cz.neumimto.rpg.sponge.SpongeRpgPlugin; -import cz.neumimto.rpg.sponge.entities.players.ISpongeCharacter; -import cz.neumimto.rpg.sponge.inventory.data.InventoryActionItemMenuData; -import cz.neumimto.rpg.sponge.inventory.data.MenuInventoryData; -import org.spongepowered.api.Sponge; -import org.spongepowered.api.data.key.Keys; -import org.spongepowered.api.data.type.DyeColor; -import org.spongepowered.api.item.ItemType; -import org.spongepowered.api.item.ItemTypes; -import org.spongepowered.api.item.inventory.Inventory; -import org.spongepowered.api.item.inventory.InventoryArchetypes; -import org.spongepowered.api.item.inventory.ItemStack; -import org.spongepowered.api.item.inventory.property.InventoryTitle; -import org.spongepowered.api.item.inventory.property.SlotPos; -import org.spongepowered.api.item.inventory.query.QueryOperationTypes; -import org.spongepowered.api.text.Text; -import org.spongepowered.api.text.format.TextStyles; - -import java.util.List; -import java.util.Optional; -import java.util.UUID; -import java.util.function.Consumer; - -public class VanillaGui { - - protected static SpongeRpgPlugin plugin = SpongeRpgPlugin.getInstance(); - protected UUID owner; - protected Inventory inventory; - - protected VanillaGui(UUID owner) { - VanillaGuiListener.clearActions(owner); - this.inventory = emptyInventory().build(plugin); - this.owner = owner; - } - - public Inventory getInventory() { - return inventory; - } - - protected Inventory.Builder emptyInventory() { - return Inventory.builder() - .of(InventoryArchetypes.DOUBLE_CHEST); - } - - protected Inventory.Builder characterEmptyInventory(ISpongeCharacter character) { - return Inventory.builder() - .of(InventoryArchetypes.DOUBLE_CHEST) - .property(InventoryTitle.of(Text.of(character.getCharacterBase().getName(), TextStyles.BOLD))); - } - - protected void setItem(SlotPos pos, ItemStack item) { - inventory.query(QueryOperationTypes.INVENTORY_PROPERTY.of(pos)).set(item.copy()); - } - - protected ItemStack getItem(SlotPos pos) { - Optional stack = inventory.query(QueryOperationTypes.INVENTORY_PROPERTY.of(pos)).peek(); - return stack.orElseGet(ItemStack::empty); - } - - protected void makeBorder(DyeColor dyeColor) { - if (inventory.getArchetype() == InventoryArchetypes.DOUBLE_CHEST) { - ItemStack item = itemPanel(dyeColor); - for (int j = 0; j < 9; j++) { - setItem(SlotPos.of(j, 0), item); - setItem(SlotPos.of(j, 5), item); - } - - for (int j = 1; j < 5; j++) { - setItem(SlotPos.of(0, j), item); - setItem(SlotPos.of(8, j), item); - } - } - } - - protected ItemType itemType(String itemType) { - if (itemType == null) { - return ItemTypes.STONE; - } - return Sponge.getRegistry().getType(ItemType.class, itemType).orElse(ItemTypes.STONE); - } - - protected ItemStack itemStack(ItemType type) { - ItemStack is = ItemStack.of(type, 1); - is.offer(new MenuInventoryData(true)); - is.offer(Keys.HIDE_ATTRIBUTES, true); - is.offer(Keys.HIDE_MISCELLANEOUS, true); - return is; - } - - protected ItemStack itemStack(ItemType type, Text name) { - ItemStack is = itemStack(type); - is.offer(Keys.DISPLAY_NAME, name); - return is; - } - - protected ItemStack itemStack(ItemType type, Text name, List lore) { - ItemStack is = itemStack(type, name); - if (lore.size() > 0) is.offer(Keys.ITEM_LORE, lore); - return is; - } - - protected ItemStack itemPanel(DyeColor dyeColor) { - ItemStack of = itemStack(ItemTypes.STAINED_GLASS_PANE, Text.EMPTY); - of.offer(Keys.DYE_COLOR, dyeColor); - return of; - } - - protected ItemStack itemAction(ItemType type, Text displayName, Consumer action) { - ItemStack of = itemStack(type, displayName); - int actionId = VanillaGuiListener.putAction(owner, action); - of.offer(new InventoryActionItemMenuData(actionId)); - return of; - } - - protected ItemStack itemCommand(ItemType type, Text displayName, String command) { - return itemAction(type, displayName, (c) -> Sponge.getCommandManager().process(c.getPlayer(), command)); - } -} diff --git a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/vanilla/VanillaGuiListener.java b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/vanilla/VanillaGuiListener.java index 83cd87140..2dc41696d 100644 --- a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/vanilla/VanillaGuiListener.java +++ b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/gui/vanilla/VanillaGuiListener.java @@ -57,14 +57,6 @@ public void onClick(ClickInventoryEvent event, @Root Player player) { Optional menu = transaction.getOriginal().get(NKeys.MENU_INVENTORY); menu.ifPresent(m -> event.setCancelled(true)); - Optional i = transaction.getOriginal().get(NKeys.MENU_ACTION); - i.ifPresent(action -> { - if (consumerMap.containsKey(player.getUniqueId()) && consumerMap.get(player.getUniqueId()).containsKey(action)) { - Rpg.get().scheduleSyncLater(() -> - consumerMap.get(player.getUniqueId()).get(action).accept(characterService.getCharacter(player))); - } - }); - Optional s = transaction.getOriginal().get(NKeys.MENU_COMMAND); s.ifPresent(command -> Rpg.get().scheduleSyncLater(() -> Sponge.getCommandManager().process(player, command))); } diff --git a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/utils/math/VectorUtils.java b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/utils/math/VectorUtils.java index aff1c0189..fef4a89d3 100644 --- a/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/utils/math/VectorUtils.java +++ b/Implementations/Sponge/7/src/main/java/cz/neumimto/rpg/sponge/utils/math/VectorUtils.java @@ -2,6 +2,7 @@ import com.flowpowered.math.TrigMath; import com.flowpowered.math.imaginary.Quaterniond; +import com.flowpowered.math.vector.Vector2d; import com.flowpowered.math.vector.Vector3d; import java.util.concurrent.ThreadLocalRandom; @@ -105,5 +106,15 @@ public static Vector3d getRandomPointBetween(Vector3d start, Vector3d end) { .mul(ThreadLocalRandom.current().nextDouble(start.distance(end))); } + public static Vector2d[] circle(Vector2d[] d, double radius) { + double increment = (2 * Math.PI) / d.length; + for (int i = 0; i < d.length; i++) { + double angle = i * increment; + double x = radius * Math.cos(angle); + double z = radius * Math.sin(angle); + d[i] = new Vector2d(x, z); + } + return d; + } }