Added option to remove disabled enchantments server-side
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<EcoPlugin> 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.
|
||||
* <p>
|
||||
@@ -342,4 +354,65 @@ public class ItemConversions implements Listener {
|
||||
|
||||
Bukkit.getLogger().warning(player.getName() + " has/had an illegal item!");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* On player hold item.
|
||||
* <p>
|
||||
* 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<Enchantment, Integer> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user