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 a1f4917d..35ca6163 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 @@ -241,4 +241,58 @@ public class ItemConversions extends PluginDependent implements Listener { itemStack.setItemMeta(meta); } + + /** + * On player hold item. + *

+ * Listener for hide fixer. + * + * @param event The event to listen for. + */ + @EventHandler + public void levelClamp(@NotNull final PlayerItemHeldEvent event) { + if (!this.getPlugin().getConfigYml().getBool("advanced.level-clamp.enabled")) { + return; + } + + if (event.getPlayer().hasPermission("ecoenchants.bypasslevelclamp")) { + return; + } + + ItemStack itemStack = event.getPlayer().getInventory().getItem(event.getNewSlot()); + + clampItemLevels(itemStack); + } + + private void clampItemLevels(@Nullable final ItemStack itemStack) { + if (itemStack == null) { + return; + } + + if (!EnchantmentTarget.ALL.getMaterials().contains(itemStack.getType())) { + return; + } + + ItemMeta meta = itemStack.getItemMeta(); + if (meta == null) { + return; + } + + if (meta instanceof EnchantmentStorageMeta) { + new HashMap<>(((EnchantmentStorageMeta) meta).getStoredEnchants()).forEach((enchantment, integer) -> { + if (integer > enchantment.getMaxLevel()) { + ((EnchantmentStorageMeta) meta).removeStoredEnchant(enchantment); + ((EnchantmentStorageMeta) meta).addStoredEnchant(enchantment, enchantment.getMaxLevel(), true); + } + }); + } else { + new HashMap<>(meta.getEnchants()).forEach((enchantment, integer) -> { + if (integer > enchantment.getMaxLevel()) { + meta.removeEnchant(enchantment); + } + }); + } + + 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 3bd446ce..a3f5eb91 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -135,4 +135,10 @@ advanced: # Fixes potential plugin conflicts/serialization issues when updating from a version before 7.3.0. # Registers byName both the internal enchantment name (new method) and the display name (old method). # There may be potential bugs with having duplicate enchantments, so enable this only if you need it. + enabled: true + level-clamp: + # Helps reverse players who have illegal above-max-level enchantments. + # Any enchantments above their max level will be set to the max level in order to prevent illegal items. + # Players with ecoenchants.bypasslevelclamp permission will be ignored. + # This may impact performance. enabled: true \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index f77ed52e..c80a8791 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -53,6 +53,7 @@ permissions: ecoenchants.randomenchant: true ecoenchants.randomenchant.bypasshardcap: true ecoenchants.randombook: true + ecoenchants.bypasslevelclamp: true ecoenchants.anvil.*: true ecoenchants.anvil.*: description: Allows all anvil permissions @@ -94,6 +95,9 @@ permissions: ecoenchants.anvil.color: description: Allows color from anvil renames default: op + ecoenchants.bypasslevelclamp: + description: Allows bypassing the advanced level clamp option + default: op # Perks ecoenchants.cooldowntime.half: