From f2bb73e2b7f2fa86ece451f042fd7450b889c7b6 Mon Sep 17 00:00:00 2001 From: Indyuce Date: Mon, 20 Dec 2021 01:02:53 +0100 Subject: [PATCH] fixed lore updates --- pom.xml | 4 +- .../net/Indyuce/mmoitems/api/UpdaterData.java | 104 ---------- .../mmoitems/api/interaction/Consumable.java | 26 +-- .../api/item/build/ItemStackBuilder.java | 2 +- .../mmoitems/api/item/build/LoreBuilder.java | 4 +- .../Indyuce/mmoitems/listener/ItemUse.java | 4 +- .../mmoitems/manager/UpdaterManager.java | 187 ------------------ 7 files changed, 12 insertions(+), 319 deletions(-) delete mode 100644 src/main/java/net/Indyuce/mmoitems/api/UpdaterData.java delete mode 100644 src/main/java/net/Indyuce/mmoitems/manager/UpdaterManager.java diff --git a/pom.xml b/pom.xml index 7c8d5fd2..87499b63 100644 --- a/pom.xml +++ b/pom.xml @@ -140,8 +140,8 @@ io.lumine - MythicLib - 1.1.6 + MythicLib-dist + 1.2 provided diff --git a/src/main/java/net/Indyuce/mmoitems/api/UpdaterData.java b/src/main/java/net/Indyuce/mmoitems/api/UpdaterData.java deleted file mode 100644 index c2063a4d..00000000 --- a/src/main/java/net/Indyuce/mmoitems/api/UpdaterData.java +++ /dev/null @@ -1,104 +0,0 @@ -package net.Indyuce.mmoitems.api; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; -import java.util.UUID; - -import org.apache.commons.lang.Validate; -import org.bukkit.configuration.ConfigurationSection; - -import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate; -import net.Indyuce.mmoitems.manager.UpdaterManager.KeepOption; -import io.lumine.mythic.lib.api.item.NBTItem; - -public class UpdaterData { - - /** - * The item reference - */ - private final Type type; - private final String id; - - /** - * Two UUIDs can be found: one on the itemStack in the NBTTags, and one in - * the UpdaterData instance. If the UUIDs match, the item is up to date. If - * they don't match, the item needs to be updated. UUID not final because it - * is updated everytime the item is edited using the editor GUI - */ - private UUID uuid; - - private final Set options = new HashSet<>(); - - public UpdaterData(MMOItemTemplate template, ConfigurationSection config) { - this(template, UUID.fromString(config.getString("uuid"))); - - for (KeepOption option : KeepOption.values()) - if (config.getBoolean(option.getPath())) - options.add(option); - } - - public UpdaterData(MMOItemTemplate template, UUID uuid, KeepOption... options) { - this.id = template.getId(); - this.type = template.getType(); - this.uuid = uuid; - this.options.addAll(Arrays.asList(options)); - } - - public UpdaterData(MMOItemTemplate template, UUID uuid, boolean enableAllOptions) { - this(template, uuid); - - if (enableAllOptions) - options.addAll(Arrays.asList(KeepOption.values())); - } - - public void save(ConfigurationSection config) { - for (KeepOption option : KeepOption.values()) - if (options.contains(option)) - config.set(option.getPath(), true); - config.set("uuid", uuid.toString()); - } - - public String getPath() { - return type.getId() + "." + id; - } - - public Type getType() { - return type; - } - - public String getId() { - return id; - } - - public UUID getUniqueId() { - return uuid; - } - - /* - * used everytime a change is applied to one item. the database uuid is - * randomized so that any item with different UUIDs have to be dynamically - * updated - */ - public void setUniqueId(UUID uuid) { - Validate.notNull(uuid, "UUID cannot be null"); - - this.uuid = uuid; - } - - public boolean matches(NBTItem item) { - return uuid.toString().equals(item.getString("MMOITEMS_ITEM_UUID")); - } - - public boolean hasOption(KeepOption option) { - return options.contains(option); - } - - public void addOption(KeepOption option) { - options.add(option); - } - - public void removeOption(KeepOption option) { - options.remove(option); - } -} \ No newline at end of file diff --git a/src/main/java/net/Indyuce/mmoitems/api/interaction/Consumable.java b/src/main/java/net/Indyuce/mmoitems/api/interaction/Consumable.java index 0d8ab67c..0578f1dc 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/interaction/Consumable.java +++ b/src/main/java/net/Indyuce/mmoitems/api/interaction/Consumable.java @@ -16,6 +16,7 @@ import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; @@ -50,18 +51,10 @@ public class Consumable extends UseItem { return false; } - /** - * @deprecated Use {@link Consumable#useOnPlayer()} - */ - @Deprecated - public boolean useWithoutItem() { - return useOnPlayer() == ConsumableConsumeResult.CONSUME; - } - /** * @return If the item should be consumed */ - public ConsumableConsumeResult useOnPlayer() { + public ConsumableConsumeResult useOnPlayer(EquipmentSlot handUsed) { NBTItem nbtItem = getNBTItem(); // Inedible stat cancels this operation from the beginning @@ -104,24 +97,15 @@ public class Consumable extends UseItem { ItemStack oldItem = nbtItem.getItem(); if (oldItem.getAmount() > 1) { newItem.setAmount(1); - - if (player.getInventory().getItemInMainHand().equals(oldItem)) - player.getInventory().setItemInMainHand(newItem); - else if (player.getInventory().getItemInOffHand().equals(oldItem)) - player.getInventory().setItemInOffHand(newItem); - + player.getInventory().setItem(handUsed, newItem); oldItem.setAmount(oldItem.getAmount() - 1); new SmartGive(player).give(oldItem); /** * Player just holding one item */ - } else { - if (player.getInventory().getItemInMainHand().equals(oldItem)) - player.getInventory().setItemInMainHand(newItem); - else if (player.getInventory().getItemInOffHand().equals(oldItem)) - player.getInventory().setItemInOffHand(newItem); - } + } else + player.getInventory().setItem(handUsed, newItem); return ConsumableConsumeResult.NOT_CONSUME; } diff --git a/src/main/java/net/Indyuce/mmoitems/api/item/build/ItemStackBuilder.java b/src/main/java/net/Indyuce/mmoitems/api/item/build/ItemStackBuilder.java index 2d21bbe2..11881891 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/item/build/ItemStackBuilder.java +++ b/src/main/java/net/Indyuce/mmoitems/api/item/build/ItemStackBuilder.java @@ -254,7 +254,7 @@ public class ItemStackBuilder { if (meta.hasLore()) { List componentLore = new LinkedList<>(); - meta.getLore().forEach(line -> componentLore.add(LegacyComponent.parse(line))); + meta.getLore().forEach(line -> componentLore.add(LegacyComponent.simpleParse(line))); nbtItem.setLoreComponents(componentLore); } diff --git a/src/main/java/net/Indyuce/mmoitems/api/item/build/LoreBuilder.java b/src/main/java/net/Indyuce/mmoitems/api/item/build/LoreBuilder.java index 1abcabb3..828f0a42 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/item/build/LoreBuilder.java +++ b/src/main/java/net/Indyuce/mmoitems/api/item/build/LoreBuilder.java @@ -179,8 +179,8 @@ public class LoreBuilder { */ for (int j = 0; j < lore.size(); ) { - // Apply color codes and replace bar prefixes - String str = MythicLib.plugin.parseColors(lore.get(j).replace("{bar}", "").replace("{sbar}", "")); + // Replace bar prefixes + String str = lore.get(j).replace("{bar}", "").replace("{sbar}", ""); // Need to break down the line into multiple if (str.contains("\\n")) { diff --git a/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java b/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java index 39acf66c..6fe61dcb 100644 --- a/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java +++ b/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java @@ -85,7 +85,7 @@ public class ItemUse implements Listener { event.setCancelled(true); useItem.getPlayerData().getMMOPlayerData().triggerSkills(player.isSneaking() ? TriggerType.SHIFT_RIGHT_CLICK : TriggerType.RIGHT_CLICK, null); - Consumable.ConsumableConsumeResult result = ((Consumable) useItem).useOnPlayer(); + Consumable.ConsumableConsumeResult result = ((Consumable) useItem).useOnPlayer(event.getHand()); if (result == Consumable.ConsumableConsumeResult.CANCEL) return; @@ -323,7 +323,7 @@ public class ItemUse implements Listener { return; } - Consumable.ConsumableConsumeResult result = ((Consumable) useItem).useOnPlayer(); + Consumable.ConsumableConsumeResult result = ((Consumable) useItem).useOnPlayer(event.getItem().equals(player.getInventory().getItemInMainHand()) ? org.bukkit.inventory.EquipmentSlot.HAND : org.bukkit.inventory.EquipmentSlot.OFF_HAND); // No effects are applied and not consumed if (result == Consumable.ConsumableConsumeResult.CANCEL) { diff --git a/src/main/java/net/Indyuce/mmoitems/manager/UpdaterManager.java b/src/main/java/net/Indyuce/mmoitems/manager/UpdaterManager.java deleted file mode 100644 index 23d55ed7..00000000 --- a/src/main/java/net/Indyuce/mmoitems/manager/UpdaterManager.java +++ /dev/null @@ -1,187 +0,0 @@ -package net.Indyuce.mmoitems.manager; - -import io.lumine.mythic.lib.MythicLib; -import io.lumine.mythic.lib.api.item.NBTItem; -import io.lumine.mythic.lib.api.util.LegacyComponent; -import io.lumine.mythic.utils.adventure.text.Component; -import io.lumine.mythic.utils.adventure.text.format.NamedTextColor; -import net.Indyuce.mmoitems.MMOItems; -import net.Indyuce.mmoitems.api.Type; -import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem; -import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate; -import net.Indyuce.mmoitems.api.player.PlayerData; -import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.Damageable; -import org.bukkit.inventory.meta.ItemMeta; - -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; - -public class UpdaterManager implements Listener { - /*public UpdaterManager() { - FileConfiguration config = new ConfigFile("/dynamic", "updater").getConfig(); - for (String typeFormat : config.getKeys(false)) - try { - Type type = MMOItems.plugin.getTypes().getOrThrow(typeFormat); - for (String id : config.getConfigurationSection(typeFormat).getKeys(false)) { - MMOItemTemplate template = MMOItems.plugin.getTemplates().getTemplateOrThrow(type, id); - //enable(new UpdaterData(template, config.getConfigurationSection(typeFormat + "." + id))); - } - } catch (IllegalArgumentException exception) { - MMOItems.plugin.getLogger().log(Level.WARNING, - "An issue occurred while trying to load dynamic updater data: " + exception.getMessage()); - } - }*/ - - /** - * Updates inventory item when an item is clicked in a player's inventory - */ - @EventHandler - public void updateOnClick(InventoryClickEvent event) { - ItemStack item = event.getCurrentItem(); - if (item == null || item.getType() == Material.AIR) return; - - ItemStack newItem = getUpdated(item, (Player) event.getWhoClicked()); - if (!newItem.equals(item)) event.setCurrentItem(newItem); - } - - /** - * Updates a player inventory when joining - */ - @EventHandler(priority = EventPriority.HIGH) - public void updateOnJoin(PlayerJoinEvent event) { - Player player = event.getPlayer(); - - player.getEquipment().setHelmet(getUpdated(player.getEquipment().getHelmet(), player)); - player.getEquipment().setChestplate(getUpdated(player.getEquipment().getChestplate(), player)); - player.getEquipment().setLeggings(getUpdated(player.getEquipment().getLeggings(), player)); - player.getEquipment().setBoots(getUpdated(player.getEquipment().getBoots(), player)); - - for (int j = 0; j < 9; j++) - player.getInventory().setItem(j, getUpdated(player.getInventory().getItem(j), player)); - player.getEquipment().setItemInOffHand(getUpdated(player.getEquipment().getItemInOffHand(), player)); - } - - public ItemStack getUpdated(ItemStack item, Player target) { - return getUpdated(MythicLib.plugin.getVersion().getWrapper().getNBTItem(item), target); - } - - public ItemStack getUpdated(NBTItem item, Player target) { - - /* - * If the item type is null, then it is not an mmoitem and it does not - * need to be updated - */ - Type type = Type.get(item.getType()); - if (type == null) return item.getItem(); - - /* - * check the internal UUID of the item, if it does not make the one - * stored in the item updater data then the item is outdated. - - UpdaterData did = data.getValue(type, item.getString("MMOITEMS_ITEM_ID")); - if (did.matches(item)) - return item.getItem(); - - */ - - MMOItemTemplate template = MMOItems.plugin.getTemplates().getTemplate(type, item.getString("MMOITEMS_ITEM_ID")); - MMOItem newMMOItem = template.newBuilder(PlayerData.get(target).getRPG()).build(); - - /* - * apply older gem stones, using a light MMOItem so the item does not - * calculate every stat data from the older item. - */ - //MMOItem volatileItem = new VolatileMMOItem(item); - /*if (did.hasOption(KeepOption.KEEP_GEMS) && volatileItem.hasData(ItemStats.GEM_SOCKETS)) - newMMOItem.replaceData(ItemStats.GEM_SOCKETS, volatileItem.getData(ItemStats.GEM_SOCKETS)); - - if (did.hasOption(KeepOption.KEEP_SOULBOUND) && volatileItem.hasData(ItemStats.SOULBOUND)) - newMMOItem.replaceData(ItemStats.SOULBOUND, volatileItem.getData(ItemStats.SOULBOUND));*/ - - // if (did.hasOption(KeepOption.KEEP_SKIN) && itemMMO.hasData(stat)) - - // apply amount - ItemStack newItem = newMMOItem.newBuilder().build(); - newItem.setAmount(item.getItem().getAmount()); - - ItemMeta newItemMeta = newItem.getItemMeta(); - NBTItem nbtItem = NBTItem.get(newItem); - List lore = new LinkedList<>(); - newItemMeta.getLore().forEach(line -> lore.add(LegacyComponent.parse(line))); - - - /* - * add old enchants to the item. warning - if enabled the item will - * remember of ANY enchant on the old item, even the enchants that were - * removed! - */ - //if (did.hasOption(KeepOption.KEEP_ENCHANTS)) - item.getItem().getItemMeta().getEnchants().forEach((enchant, level) -> newItemMeta.addEnchant(enchant, level, true)); - - /* - * keepLore is used to save enchants from custom enchants plugins that - * only use lore to save enchant data - */ - //if (did.hasOption(KeepOption.KEEP_LORE)) { - int n = 0; - for (Component component : nbtItem.getLoreComponents()) { - if (component.color() != NamedTextColor.GRAY) break; - lore.add(n++, component); - } - //} - - /* - * keep durability can be used for tools to save their durability so - * users do not get extra durability when the item is updated - */ - //if (did.hasOption(KeepOption.KEEP_DURABILITY) && item.getItem().getItemMeta() instanceof Damageable && newItemMeta instanceof Damageable) - ((Damageable) newItemMeta).setDamage(((Damageable) item.getItem().getItemMeta()).getDamage()); - - newItem.setItemMeta(newItemMeta); - /* - * keep name so players who renamed the item in the anvil does not have - * to rename it again - */ - //if (did.hasOption(KeepOption.KEEP_NAME) && item.getItem().getItemMeta().hasDisplayName()) - nbtItem.setDisplayNameComponent(LegacyComponent.parse(item.getItem().getItemMeta().getDisplayName())); - - nbtItem.setLoreComponents(lore); - - return nbtItem.toItem(); - } - - public enum KeepOption { - KEEP_LORE("Any lore line starting with '&7' will be", "kept when updating your item.", "", "This option is supposed to keep", "the item custom enchants.", ChatColor.RED + "May not support every enchant plugin."), - KEEP_ENCHANTS("The item keeps its old enchantments."), - KEEP_DURABILITY("The item keeps its durability.", "Don't use this option if you", "are using texture-by-durability!"), - KEEP_NAME("The item keeps its display name."), - KEEP_GEMS("The item keeps its empty gem", "sockets and applied gems."), - KEEP_SOULBOUND("The item keeps its soulbound data."), - // KEEP_SKIN("Keep the item applied skins."), - ; - - private final List lore; - - KeepOption(String... lore) { - this.lore = Arrays.asList(lore); - } - - public List getLore() { - return lore; - } - - public String getPath() { - return name().toLowerCase().replace("_", "-").substring(5); - } - } -}