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 644b9840..7addf40e 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 @@ -304,4 +304,71 @@ public class ItemConversions extends PluginDependent implements Liste this.getPlugin().getLogger().warning(player.getName() + " has/had an illegal item!"); } } + + /** + * On player hold item. + *

+ * Listener for hide fixer. + * + * @param event The event to listen for. + */ + @EventHandler + public void removeDisallowed(@NotNull final PlayerItemHeldEvent event) { + if (!this.getPlugin().getConfigYml().getBool("advanced.remove-illegal.enabled")) { + return; + } + + if (event.getPlayer().hasPermission("ecoenchants.allowillegal")) { + return; + } + + ItemStack itemStack = event.getPlayer().getInventory().getItem(event.getNewSlot()); + + clampItemLevels(itemStack, event.getPlayer()); + } + + private void removeDisallowed(@Nullable final ItemStack itemStack, + @NotNull final Player player) { + if (itemStack == null) { + return; + } + + ItemMeta meta = itemStack.getItemMeta(); + if (meta == null) { + return; + } + + if (meta instanceof EnchantmentStorageMeta) { + return; + } + + boolean illegal = false; + + EcoEnchant illegalEnchant = null; + + for (Enchantment enchantment : meta.getEnchants().keySet()) { + if (EcoEnchants.getFromEnchantment(enchantment) != null) { + EcoEnchant enchant = EcoEnchants.getFromEnchantment(enchantment); + + if (!enchant.getTargetMaterials().contains(itemStack.getType())) { + illegal = true; + illegalEnchant = enchant; + } + } + } + + if (!illegal) { + return; + } + + if (this.getPlugin().getConfigYml().getBool("advanced.remove-illegal.delete-item")) { + itemStack.setType(Material.AIR); + itemStack.setItemMeta(new ItemStack(Material.AIR).getItemMeta()); + itemStack.setItemMeta(meta); + } else { + meta.removeEnchant(illegalEnchant); + } + + this.getPlugin().getLogger().warning(player.getName() + " has/had an illegal item!"); + } } diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index 581a3ee1..f03a3ca8 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -143,4 +143,12 @@ advanced: # This may impact performance. enabled: false # If the item should be deleted rather than have the enchantment level capped. - delete-item: false \ No newline at end of file + delete-item: false + remove-illegal: + # Helps reverse players who have enchantments on illegal items. + # When holding an item, any enchantments present on the item that shouldn't be will be removed. + # Players with ecoenchants.allowillegal permission will be ignored. + # This may impact performance. + enabled: false + # If the item should be deleted rather than have the enchantment removed. + delete-item: false diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index 84521e74..eefce078 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -41,6 +41,7 @@ permissions: ecoenchants.bypasslevelclamp: true ecoenchants.anvil.*: true ecoenchants.command.*: true + ecoenchants.allowillegal: true ecoenchants.anvil.*: description: Allows all anvil permissions default: op @@ -98,6 +99,9 @@ permissions: ecoenchants.bypasslevelclamp: description: Allows bypassing the advanced level clamp option default: op + ecoenchants.allowillegal: + description: Don't remove illegal items when remove-illegal is enabled. + default: op # Perks ecoenchants.cooldowntime.half: