diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/hook/ProtocolHook.java b/Core/src/main/java/su/nightexpress/excellentenchants/hook/ProtocolHook.java index 54fbf43..80ab216 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/hook/ProtocolHook.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/hook/ProtocolHook.java @@ -10,12 +10,12 @@ import org.bukkit.GameMode; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.Nullable; -import su.nexmedia.engine.utils.ItemUtil; import su.nightexpress.excellentenchants.ExcellentEnchantsAPI; import su.nightexpress.excellentenchants.api.enchantment.ExcellentEnchant; import su.nightexpress.excellentenchants.config.Config; import su.nightexpress.excellentenchants.manager.EnchantManager; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -33,13 +33,10 @@ public class ProtocolHook { PacketContainer packet = event.getPacket(); ItemStack item = packet.getItemModifier().read(0); - boolean removeOld = event.getPlayer().getGameMode() == GameMode.CREATIVE; - if (removeOld) { - packet.getItemModifier().write(0, remove(item)); - } - else { - packet.getItemModifier().write(0, update(item)); - } + boolean isCreative = event.getPlayer().getGameMode() == GameMode.CREATIVE; + //else { + packet.getItemModifier().write(0, update(item, isCreative)); + //} } }); @@ -49,15 +46,11 @@ public class ProtocolHook { PacketContainer packet = event.getPacket(); List items = packet.getItemListModifier().readSafely(0); - boolean removeOld = event.getPlayer().getGameMode() == GameMode.CREATIVE; + boolean isCreative = event.getPlayer().getGameMode() == GameMode.CREATIVE; for (int index = 0; index < items.size(); index++) { - if (removeOld) { - items.set(index, remove(items.get(index))); - } - else { - items.set(index, update(items.get(index))); - } + ItemStack item = items.get(index); + items.set(index, update(item, isCreative)); } packet.getItemListModifier().write(0, items); } @@ -67,20 +60,23 @@ public class ProtocolHook { } @Nullable - private static ItemStack update(@Nullable ItemStack item) { + private static ItemStack update(@Nullable ItemStack item, boolean isCreative) { if (item == null || item.getType().isAir()) return item; ItemStack copy = new ItemStack(item); ItemMeta meta = copy.getItemMeta(); if (meta == null) return item; - List lore = ItemUtil.getLore(copy); - Map enchants = EnchantManager.getItemCustomEnchants(item); - //if (enchants.keySet().stream().anyMatch(enchant -> ItemUtil.getLoreTag(item, enchant.getId()) != null)) { - // return item; - //} + List lore = meta.getLore() == null ? new ArrayList<>() : meta.getLore(); + Map enchants = EnchantManager.getExcellentEnchantments(item); - if (Config.ENCHANTMENTS_DESCRIPTION_ENABLED) { + enchants.keySet().forEach(enchant -> lore.removeIf(line -> line.contains(enchant.getDisplayName()))); + if (isCreative) { + enchants.forEach((enchant, integer) -> { + lore.removeAll(Config.formatDescription(enchant.getDescription(integer))); + }); + } + if (Config.ENCHANTMENTS_DESCRIPTION_ENABLED && !isCreative) { enchants.forEach((enchant, integer) -> { lore.addAll(0, Config.formatDescription(enchant.getDescription(integer))); }); @@ -93,23 +89,4 @@ public class ProtocolHook { copy.setItemMeta(meta); return copy; } - - @Nullable - private static ItemStack remove(@Nullable ItemStack item) { - if (item == null || item.getType().isAir()) return item; - - List from = ItemUtil.getLore(item); - Map enchants = EnchantManager.getItemCustomEnchants(item); - enchants.forEach((enchant, integer) -> { - from.removeIf(line -> line.equalsIgnoreCase(enchant.getNameFormatted(integer))); - from.removeAll(Config.formatDescription(enchant.getDescription(integer))); - }); - - ItemMeta meta = item.getItemMeta(); - if (meta == null) return item; - - meta.setLore(from); - item.setItemMeta(meta); - return item; - } } diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/EnchantManager.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/EnchantManager.java index fc9b9e7..7513811 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/EnchantManager.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/EnchantManager.java @@ -14,6 +14,7 @@ import org.jetbrains.annotations.Nullable; import su.nexmedia.engine.api.manager.AbstractManager; import su.nexmedia.engine.utils.EntityUtil; import su.nexmedia.engine.utils.ItemUtil; +import su.nexmedia.engine.utils.Pair; import su.nexmedia.engine.utils.random.Rnd; import su.nightexpress.excellentenchants.ExcellentEnchants; import su.nightexpress.excellentenchants.ExcellentEnchantsAPI; @@ -162,7 +163,7 @@ public class EnchantManager extends AbstractManager { @Deprecated public static void updateItemLoreEnchants(@NotNull ItemStack item) { - EnchantRegister.ENCHANT_LIST.forEach(ench -> { + EnchantRegister.ENCHANT_REGISTRY.values().forEach(ench -> { //ItemUtil.delLore(item, ench.getId()); //ItemUtil.delLore(item, ench.getId() + "_info"); }); @@ -228,7 +229,9 @@ public class EnchantManager extends AbstractManager { item.setItemMeta(meta); } + // Too expensive @NotNull + @Deprecated public static Map getItemCustomEnchants(@NotNull ItemStack item) { return EnchantManager.getItemEnchants(item).entrySet().stream() .filter(entry -> entry.getKey() instanceof ExcellentEnchant) @@ -246,6 +249,7 @@ public class EnchantManager extends AbstractManager { .collect(Collectors.toMap(k -> (T) k.getKey(), Map.Entry::getValue, (old, nev) -> nev, LinkedHashMap::new)); } + @Deprecated public static int getItemCustomEnchantsAmount(@NotNull ItemStack item) { return EnchantManager.getItemCustomEnchants(item).size(); } @@ -256,6 +260,7 @@ public class EnchantManager extends AbstractManager { } @NotNull + @Deprecated public static Map getItemEnchants(@NotNull ItemStack item) { ItemMeta meta = item.getItemMeta(); if (meta == null) return Collections.emptyMap(); @@ -263,6 +268,7 @@ public class EnchantManager extends AbstractManager { return (meta instanceof EnchantmentStorageMeta meta2) ? meta2.getStoredEnchants() : meta.getEnchants(); } + @Deprecated public static int getItemEnchantsAmount(@NotNull ItemStack item) { return EnchantManager.getItemEnchants(item).size(); } @@ -278,6 +284,16 @@ public class EnchantManager extends AbstractManager { return meta.getEnchantLevel(enchant); } + @NotNull + public static Map getExcellentEnchantments(@NotNull ItemStack item) { + return EnchantManager.getItemEnchants(item).entrySet().stream() + .map(entry -> { + ExcellentEnchant ex = EnchantRegister.get(entry.getKey().getKey()); + return ex == null ? null : Pair.of(ex, entry.getValue()); + }).filter(Objects::nonNull) + .collect(Collectors.toMap(Pair::getFirst, Pair::getSecond, (old, nev) -> nev, LinkedHashMap::new)); + } + @Nullable public static ExcellentEnchant getEnchantmentByEffect(@NotNull LivingEntity entity, @NotNull PotionEffect effect) { Enchantment enchantment = ExcellentEnchantsAPI.PLUGIN.getEnchantNMS().getEnchantmentByEffect(entity, effect); diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/EnchantRegister.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/EnchantRegister.java index c95735e..2f18bbc 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/EnchantRegister.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/EnchantRegister.java @@ -16,14 +16,13 @@ import su.nightexpress.excellentenchants.manager.enchants.tool.*; import su.nightexpress.excellentenchants.manager.enchants.weapon.*; import java.lang.reflect.Field; -import java.util.HashSet; +import java.util.HashMap; import java.util.Map; -import java.util.Set; public class EnchantRegister { - private static final ExcellentEnchants PLUGIN; - public static final Set ENCHANT_LIST; + private static final ExcellentEnchants PLUGIN; + public static final Map ENCHANT_REGISTRY; public static final EnchantBlastMining BLAST_MINING; public static final EnchantCurseOfBreaking CURSE_OF_BREAKING; @@ -91,7 +90,7 @@ public class EnchantRegister { static { PLUGIN = ExcellentEnchants.getPlugin(ExcellentEnchants.class); PLUGIN.getConfigManager().extractResources("/enchants/"); - ENCHANT_LIST = new HashSet<>(); + ENCHANT_REGISTRY = new HashMap<>(); // Tool enchants BLAST_MINING = init(EnchantBlastMining.class, EnchantBlastMining.ID); @@ -165,7 +164,7 @@ public class EnchantRegister { public static void setup() { // Prevent to register enchantments during the runtime. if (ExcellentEnchants.isLoaded) { - ENCHANT_LIST.forEach(ExcellentEnchant::loadConfig); + ENCHANT_REGISTRY.values().forEach(ExcellentEnchant::loadConfig); return; } @@ -186,7 +185,7 @@ public class EnchantRegister { } Enchantment.stopAcceptingRegistrations(); - PLUGIN.info("Enchants Registered: " + ENCHANT_LIST.size()); + PLUGIN.info("Enchants Registered: " + ENCHANT_REGISTRY.size()); ExcellentEnchants.isLoaded = true; } @@ -199,7 +198,7 @@ public class EnchantRegister { if (byKey == null || byName == null) return; - for (ExcellentEnchant enchant : ENCHANT_LIST) { + for (ExcellentEnchant enchant : ENCHANT_REGISTRY.values()) { if (enchant instanceof ICleanable cleanable) { cleanable.clear(); } @@ -208,10 +207,15 @@ public class EnchantRegister { byName.remove(enchant.getName()); enchant.unregisterListeners(); } - ENCHANT_LIST.clear(); + ENCHANT_REGISTRY.clear(); PLUGIN.info("All enchants are unregistered."); } + @Nullable + public static ExcellentEnchant get(@NotNull NamespacedKey key) { + return ENCHANT_REGISTRY.get(key); + } + @Nullable private static T init(@NotNull Class clazz, @NotNull String id) { String enchantId = id.toLowerCase(); @@ -231,7 +235,7 @@ public class EnchantRegister { if (enchant == null) return; Enchantment.registerEnchantment(enchant); - ENCHANT_LIST.add(enchant); + ENCHANT_REGISTRY.put(enchant.getKey(), enchant); enchant.registerListeners(); PLUGIN.info("Registered enchantment: " + enchant.getId()); //IRegistry.a(IRegistry.ENCHANTMENT, enchant.getId(), CraftEnchantment.getRaw(enchant)); diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/object/EnchantListGUI.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/object/EnchantListGUI.java index 22a93b8..aa6c268 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/object/EnchantListGUI.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/object/EnchantListGUI.java @@ -79,7 +79,7 @@ public class EnchantListGUI extends AbstractMenu { public boolean onPrepare(@NotNull Player player, @NotNull Inventory inventory) { int page = this.getPage(player); int length = this.enchantSlots.length; - List list = new ArrayList<>(EnchantRegister.ENCHANT_LIST.stream(). + List list = new ArrayList<>(EnchantRegister.ENCHANT_REGISTRY.values().stream(). sorted(Comparator.comparing(ExcellentEnchant::getName)).toList()); List> split = CollectionsUtil.split(list, length);