From 88a683a01adeb994a8445bac01797d4554e9554c Mon Sep 17 00:00:00 2001 From: Auxilor Date: Tue, 27 Jul 2021 18:23:32 +0100 Subject: [PATCH] Added option to remove disabled enchantments server-side --- .../proxy/v1_16_R3/FastGetEnchants.java | 6 +- .../proxy/v1_17_R1/FastGetEnchants.java | 3 + .../util/ItemConversionOptions.java | 7 ++ .../enchantments/util/ItemConversions.java | 75 ++++++++++++++++++- .../core-plugin/src/main/resources/config.yml | 7 ++ .../proxy/proxies/FastGetEnchantsProxy.java | 4 +- 6 files changed, 98 insertions(+), 4 deletions(-) diff --git a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/FastGetEnchants.java b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/FastGetEnchants.java index bda58459..ab68b7c2 100644 --- a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/FastGetEnchants.java +++ b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/FastGetEnchants.java @@ -1,11 +1,13 @@ package com.willfp.ecoenchants.proxy.v1_16_R3; import com.willfp.ecoenchants.proxy.proxies.FastGetEnchantsProxy; -import net.minecraft.server.v1_16_R3.Items; import net.minecraft.server.v1_16_R3.ItemEnchantedBook; +import net.minecraft.server.v1_16_R3.Items; import net.minecraft.server.v1_16_R3.NBTBase; import net.minecraft.server.v1_16_R3.NBTTagCompound; import net.minecraft.server.v1_16_R3.NBTTagList; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_16_R3.util.CraftNamespacedKey; import org.bukkit.enchantments.Enchantment; @@ -13,7 +15,9 @@ import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; public final class FastGetEnchants implements FastGetEnchantsProxy { @Override diff --git a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_17_R1/FastGetEnchants.java b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_17_R1/FastGetEnchants.java index 56a02877..69b2c08e 100644 --- a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_17_R1/FastGetEnchants.java +++ b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_17_R1/FastGetEnchants.java @@ -6,6 +6,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.world.item.ItemEnchantedBook; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_17_R1.util.CraftNamespacedKey; import org.bukkit.enchantments.Enchantment; @@ -13,7 +14,9 @@ import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; public final class FastGetEnchants implements FastGetEnchantsProxy { @Override 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 e3d55203..10d6e9f2 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,6 +62,12 @@ public class ItemConversionOptions { @Getter private boolean deletingIllegal = false; + /** + * If disabled enchantments should be removed entirely. + */ + @Getter + private boolean removeDisabled = false; + public void reload(@NotNull final EcoPlugin plugin) { usingLoreGetter = plugin.getConfigYml().getBool("advanced.lore-getter.enabled"); usingAggressiveLoreGetter = plugin.getConfigYml().getBool("advanced.lore-getter.aggressive"); @@ -72,6 +78,7 @@ public class ItemConversionOptions { usingLevelClampDelete = plugin.getConfigYml().getBool("advanced.level-clamp.delete-item"); removingIllegal = plugin.getConfigYml().getBool("advanced.remove-illegal.enabled"); deletingIllegal = plugin.getConfigYml().getBool("advanced.remove-illegal.delete-item"); + removeDisabled = plugin.getConfigYml().getBool("advanced.remove-invalid.remove-disabled"); } 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 7e2ff0e6..c130850e 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 @@ -1,9 +1,12 @@ package com.willfp.ecoenchants.enchantments.util; +import com.willfp.eco.core.EcoPlugin; +import com.willfp.eco.core.PluginDependent; import com.willfp.eco.util.NumberUtils; import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget; +import com.willfp.ecoenchants.proxy.proxies.FastGetEnchantsProxy; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -28,7 +31,16 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -public class ItemConversions implements Listener { +public class ItemConversions extends PluginDependent implements Listener { + /** + * Pass an {@link EcoPlugin} in order to interface with it. + * + * @param plugin The plugin to manage. + */ + protected ItemConversions(@NotNull EcoPlugin plugin) { + super(plugin); + } + /** * On player hold item. *

@@ -342,4 +354,65 @@ public class ItemConversions implements Listener { Bukkit.getLogger().warning(player.getName() + " has/had an illegal item!"); } + + + /** + * On player hold item. + *

+ * Listener for conversion. + * + * @param event The event to listen for. + */ + @EventHandler + public void invalidRemover(@NotNull final PlayerItemHeldEvent event) { + if (!ItemConversionOptions.isRemoveDisabled()) { + return; + } + + ItemStack itemStack = event.getPlayer().getInventory().getItem(event.getNewSlot()); + + fixInvalid(itemStack); + } + + private void fixInvalid(@Nullable final ItemStack itemStack) { + if (itemStack == null) { + return; + } + + ItemMeta meta = itemStack.getItemMeta(); + if (meta == null) { + return; + } + + Map enchants = this.getPlugin().getProxy(FastGetEnchantsProxy.class) + .getEnchantmentsOnItem(itemStack, true); + + for (Enchantment enchantment : enchants.keySet()) { + if (enchantment instanceof EcoEnchant enchant) { + if (!enchant.isEnabled()) { + enchants.remove(enchantment); + } + } + } + + if (meta instanceof EnchantmentStorageMeta storageMeta) { + storageMeta.getStoredEnchants().forEach((enchantment, integer) -> { + storageMeta.removeStoredEnchant(enchantment); + }); + + enchants.forEach((enchantment, integer) -> { + storageMeta.addStoredEnchant(enchantment, integer, true); + }); + } else { + meta.getEnchants().forEach((enchantment, integer) -> { + meta.removeEnchant(enchantment); + }); + + enchants.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 7695f37c..4a263b9a 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -154,3 +154,10 @@ advanced: enabled: false # If the item should be deleted rather than have the enchantment removed. delete-item: false + remove-invalid: + # Remove invalid / disabled enchantments from items server-side + # 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 + # If disabled enchantments should be removed + remove-disabled: false diff --git a/eco-core/core-proxy/src/main/java/com/willfp/ecoenchants/proxy/proxies/FastGetEnchantsProxy.java b/eco-core/core-proxy/src/main/java/com/willfp/ecoenchants/proxy/proxies/FastGetEnchantsProxy.java index d398e18c..430c85d4 100644 --- a/eco-core/core-proxy/src/main/java/com/willfp/ecoenchants/proxy/proxies/FastGetEnchantsProxy.java +++ b/eco-core/core-proxy/src/main/java/com/willfp/ecoenchants/proxy/proxies/FastGetEnchantsProxy.java @@ -22,7 +22,7 @@ public interface FastGetEnchantsProxy extends AbstractProxy { /** * Get all enchantments on an {@link ItemStack}. * - * @param itemStack The item to query. + * @param itemStack The item to query. * @param checkStored Check stored enchantments in the enchanted book if true. * @return A map of all enchantments, where the value represents the level present. */ @@ -36,7 +36,7 @@ public interface FastGetEnchantsProxy extends AbstractProxy { * @return The level found, or 0 if not present. */ default int getLevelOnItem(@NotNull ItemStack itemStack, - @NotNull Enchantment enchantment) { + @NotNull Enchantment enchantment) { return getLevelOnItem(itemStack, enchantment, false); }