diff --git a/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/EcoCraftEnchantmentManager.java b/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/EcoCraftEnchantmentManager.java new file mode 100644 index 00000000..95536cb6 --- /dev/null +++ b/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/EcoCraftEnchantmentManager.java @@ -0,0 +1,25 @@ +package com.willfp.ecoenchants.proxy.v1_16_R1; + +import com.willfp.ecoenchants.enchantments.support.vanilla.VanillaEnchantmentMetadata; +import com.willfp.ecoenchants.enchantments.support.vanilla.VanillaEnchantments; +import com.willfp.ecoenchants.proxy.proxies.EcoCraftEnchantmentManagerProxy; +import com.willfp.ecoenchants.proxy.v1_16_R1.vanilla.EcoCraftEnchantment; +import net.minecraft.server.v1_16_R1.Enchantment; +import net.minecraft.server.v1_16_R1.IRegistry; +import org.bukkit.NamespacedKey; +import org.bukkit.craftbukkit.v1_16_R1.util.CraftNamespacedKey; + +import java.util.Map; + +public final class EcoCraftEnchantmentManager implements EcoCraftEnchantmentManagerProxy { + @Override + public void registerNewCraftEnchantments() { + Map metadataMap = VanillaEnchantments.getMetadataMap(); + + for (Enchantment enchantment : IRegistry.ENCHANTMENT) { + NamespacedKey key = CraftNamespacedKey.fromMinecraft(IRegistry.ENCHANTMENT.getKey(enchantment)); + VanillaEnchantmentMetadata metadata = metadataMap.get(org.bukkit.enchantments.Enchantment.getByKey(key)); + new EcoCraftEnchantment(enchantment, metadata).register(); + } + } +} diff --git a/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/vanilla/EcoCraftEnchantment.java b/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/vanilla/EcoCraftEnchantment.java new file mode 100644 index 00000000..13b55300 --- /dev/null +++ b/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/vanilla/EcoCraftEnchantment.java @@ -0,0 +1,56 @@ +package com.willfp.ecoenchants.proxy.v1_16_R1.vanilla; + +import com.willfp.ecoenchants.enchantments.support.vanilla.EcoCraftEnchantmentWrapper; +import com.willfp.ecoenchants.enchantments.support.vanilla.VanillaEnchantmentMetadata; +import net.minecraft.server.v1_16_R1.Enchantment; +import org.bukkit.NamespacedKey; +import org.bukkit.craftbukkit.v1_16_R1.enchantments.CraftEnchantment; +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + +@SuppressWarnings("unchecked") +public class EcoCraftEnchantment extends CraftEnchantment implements EcoCraftEnchantmentWrapper { + private final VanillaEnchantmentMetadata metadata; + + public EcoCraftEnchantment(@NotNull final Enchantment target, + @NotNull final VanillaEnchantmentMetadata metadata) { + super(target); + this.metadata = metadata; + } + + @Override + public int getMaxLevel() { + return metadata.getMaxLevel() == null ? this.getHandle().getMaxLevel() : metadata.getMaxLevel(); + } + + public void register() { + try { + Field byIdField = org.bukkit.enchantments.Enchantment.class.getDeclaredField("byKey"); + Field byNameField = org.bukkit.enchantments.Enchantment.class.getDeclaredField("byName"); + byIdField.setAccessible(true); + byNameField.setAccessible(true); + Map byKey = (Map) byIdField.get(null); + Map byName = (Map) byNameField.get(null); + byKey.remove(this.getKey()); + byName.remove(this.getName()); + + Map byNameClone = new HashMap<>(byName); + for (Map.Entry entry : byNameClone.entrySet()) { + if (entry.getValue().getKey().equals(this.getKey())) { + byName.remove(entry.getKey()); + } + } + + Field f = org.bukkit.enchantments.Enchantment.class.getDeclaredField("acceptingNew"); + f.setAccessible(true); + f.set(null, true); + f.setAccessible(false); + + org.bukkit.enchantments.Enchantment.registerEnchantment(this); + } catch (NoSuchFieldException | IllegalAccessException ignored) { + } + } +} diff --git a/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/EcoCraftEnchantmentManager.java b/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/EcoCraftEnchantmentManager.java new file mode 100644 index 00000000..42f19e06 --- /dev/null +++ b/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/EcoCraftEnchantmentManager.java @@ -0,0 +1,25 @@ +package com.willfp.ecoenchants.proxy.v1_16_R2; + +import com.willfp.ecoenchants.enchantments.support.vanilla.VanillaEnchantmentMetadata; +import com.willfp.ecoenchants.enchantments.support.vanilla.VanillaEnchantments; +import com.willfp.ecoenchants.proxy.proxies.EcoCraftEnchantmentManagerProxy; +import com.willfp.ecoenchants.proxy.v1_16_R2.vanilla.EcoCraftEnchantment; +import net.minecraft.server.v1_16_R2.Enchantment; +import net.minecraft.server.v1_16_R2.IRegistry; +import org.bukkit.NamespacedKey; +import org.bukkit.craftbukkit.v1_16_R2.util.CraftNamespacedKey; + +import java.util.Map; + +public final class EcoCraftEnchantmentManager implements EcoCraftEnchantmentManagerProxy { + @Override + public void registerNewCraftEnchantments() { + Map metadataMap = VanillaEnchantments.getMetadataMap(); + + for (Enchantment enchantment : IRegistry.ENCHANTMENT) { + NamespacedKey key = CraftNamespacedKey.fromMinecraft(IRegistry.ENCHANTMENT.getKey(enchantment)); + VanillaEnchantmentMetadata metadata = metadataMap.get(org.bukkit.enchantments.Enchantment.getByKey(key)); + new EcoCraftEnchantment(enchantment, metadata).register(); + } + } +} diff --git a/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/vanilla/EcoCraftEnchantment.java b/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/vanilla/EcoCraftEnchantment.java new file mode 100644 index 00000000..337d0336 --- /dev/null +++ b/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/vanilla/EcoCraftEnchantment.java @@ -0,0 +1,56 @@ +package com.willfp.ecoenchants.proxy.v1_16_R2.vanilla; + +import com.willfp.ecoenchants.enchantments.support.vanilla.EcoCraftEnchantmentWrapper; +import com.willfp.ecoenchants.enchantments.support.vanilla.VanillaEnchantmentMetadata; +import net.minecraft.server.v1_16_R2.Enchantment; +import org.bukkit.NamespacedKey; +import org.bukkit.craftbukkit.v1_16_R2.enchantments.CraftEnchantment; +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + +@SuppressWarnings("unchecked") +public class EcoCraftEnchantment extends CraftEnchantment implements EcoCraftEnchantmentWrapper { + private final VanillaEnchantmentMetadata metadata; + + public EcoCraftEnchantment(@NotNull final Enchantment target, + @NotNull final VanillaEnchantmentMetadata metadata) { + super(target); + this.metadata = metadata; + } + + @Override + public int getMaxLevel() { + return metadata.getMaxLevel() == null ? this.getHandle().getMaxLevel() : metadata.getMaxLevel(); + } + + public void register() { + try { + Field byIdField = org.bukkit.enchantments.Enchantment.class.getDeclaredField("byKey"); + Field byNameField = org.bukkit.enchantments.Enchantment.class.getDeclaredField("byName"); + byIdField.setAccessible(true); + byNameField.setAccessible(true); + Map byKey = (Map) byIdField.get(null); + Map byName = (Map) byNameField.get(null); + byKey.remove(this.getKey()); + byName.remove(this.getName()); + + Map byNameClone = new HashMap<>(byName); + for (Map.Entry entry : byNameClone.entrySet()) { + if (entry.getValue().getKey().equals(this.getKey())) { + byName.remove(entry.getKey()); + } + } + + Field f = org.bukkit.enchantments.Enchantment.class.getDeclaredField("acceptingNew"); + f.setAccessible(true); + f.set(null, true); + f.setAccessible(false); + + org.bukkit.enchantments.Enchantment.registerEnchantment(this); + } catch (NoSuchFieldException | IllegalAccessException ignored) { + } + } +}