From 4001d7ca46baef085a2c68289d3065f2a2f2522d Mon Sep 17 00:00:00 2001 From: Auxilor Date: Wed, 18 Aug 2021 17:32:16 +0100 Subject: [PATCH] Added hard cap clamp --- .../util/ItemConversionOptions.java | 12 +++ .../enchantments/util/ItemConversions.java | 88 +++++++++++++++++++ .../core-plugin/src/main/resources/config.yml | 5 ++ 3 files changed, 105 insertions(+) diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/ItemConversionOptions.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/ItemConversionOptions.java index 1019fe6e..727a72ea 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/ItemConversionOptions.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/ItemConversionOptions.java @@ -62,12 +62,23 @@ public class ItemConversionOptions { @Getter private boolean deletingIllegal = false; + /** + * If hard cap clamp is enabled. + */ + @Getter + private boolean usingHardCapClamp = false; + /** * If disabled enchantments should be removed entirely. */ @Getter private boolean removeDisabled = false; + /** + * Reload the options. + * + * @param plugin Instance of ecoenchants. + */ public void reload(@NotNull final EcoPlugin plugin) { usingLoreGetter = plugin.getConfigYml().getBool("advanced.lore-getter.enabled"); usingAggressiveLoreGetter = plugin.getConfigYml().getBool("advanced.lore-getter.aggressive"); @@ -79,6 +90,7 @@ public class ItemConversionOptions { removingIllegal = plugin.getConfigYml().getBool("advanced.remove-illegal.enabled"); deletingIllegal = plugin.getConfigYml().getBool("advanced.remove-illegal.delete-item"); removeDisabled = plugin.getConfigYml().getBool("advanced.remove-disabled.enabled"); + usingHardCapClamp = plugin.getConfigYml().getBool("advanced.hard-cap-clamp.enabled"); } static { diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/ItemConversions.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/ItemConversions.java index cee4f989..c27a4b36 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/ItemConversions.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/ItemConversions.java @@ -415,4 +415,92 @@ public class ItemConversions extends PluginDependent implements Liste itemStack.setItemMeta(meta); } + + /** + * On player hold item. + *

+ * Listener for conversion. + * + * @param event The event to listen for. + */ + @EventHandler + public void hardCapClamp(@NotNull final PlayerItemHeldEvent event) { + if (!ItemConversionOptions.isUsingHardCapClamp()) { + return; + } + + + if (!this.getPlugin().getConfigYml().getBool("anvil.hard-cap.enabled")) { + return; + } + + if (event.getPlayer().hasPermission("ecoenchants.anvil.bypasshardcap")) { + return; + } + + ItemStack itemStack = event.getPlayer().getInventory().getItem(event.getNewSlot()); + + clampHardCap(itemStack); + } + + private void clampHardCap(@Nullable final ItemStack itemStack) { + if (itemStack == null) { + return; + } + + ItemMeta meta = itemStack.getItemMeta(); + if (meta == null) { + return; + } + + Map enchants = FastItemStack.wrap(itemStack).getEnchantmentsOnItem(true); + Map replacement = new HashMap<>(); + + int i = 0; + for (Map.Entry entry : enchants.entrySet()) { + if (i >= this.getPlugin().getConfigYml().getInt("anvil.hard-cap.cap")) { + return; + } + + Enchantment enchantment = entry.getKey(); + if (enchantment instanceof EcoEnchant enchant) { + if (!enchant.hasFlag("hard-cap-ignore")) { + i++; + replacement.put(entry.getKey(), entry.getValue()); + } + } else { + i++; + replacement.put(entry.getKey(), entry.getValue()); + } + } + + for (Map.Entry entry : enchants.entrySet()) { + Enchantment enchantment = entry.getKey(); + if (enchantment instanceof EcoEnchant enchant) { + if (enchant.hasFlag("hard-cap-ignore")) { + replacement.put(entry.getKey(), entry.getValue()); + } + } + } + + if (meta instanceof EnchantmentStorageMeta storageMeta) { + storageMeta.getStoredEnchants().forEach((enchantment, integer) -> { + storageMeta.removeStoredEnchant(enchantment); + }); + + replacement.forEach((enchantment, integer) -> { + storageMeta.addStoredEnchant(enchantment, integer, true); + }); + } else { + meta.getEnchants().forEach((enchantment, integer) -> { + meta.removeEnchant(enchantment); + }); + + replacement.forEach((enchantment, integer) -> { + meta.addEnchant(enchantment, integer, true); + }); + } + + itemStack.setItemMeta(meta); + } } diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index 426ebf3e..66e29fcc 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -176,3 +176,8 @@ advanced: # Useful if you're disabling enchantments permanently and don't mind removing them from existing players # Also helps remove deleted enchantments or enchantments from old plugins enabled: false + hard-cap-clamp: + # Helps reverse players who have items with more enchants than the hard cap. + # Players with ecoenchants.anvil.bypasshardcap permission will be ignored. + # This may impact performance. + enabled: false