From ca8169d46b6b546f6ca84482d8f54dc713c3edca Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sat, 12 Jun 2021 10:37:22 +0100 Subject: [PATCH] Cleaned up registration code --- .../enchants/EcoCraftEnchantment.java | 33 +----------- .../enchants/EcoCraftEnchantment.java | 33 +----------- .../enchants/EcoCraftEnchantment.java | 33 +----------- .../enchants/EcoCraftEnchantment.java | 33 +----------- .../ecoenchants/enchantments/EcoEnchant.java | 36 +------------ .../enchantments/util/EnchantmentUtils.java | 51 +++++++++++++++++++ 6 files changed, 61 insertions(+), 158 deletions(-) diff --git a/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/enchants/EcoCraftEnchantment.java b/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/enchants/EcoCraftEnchantment.java index ae0a2ad9..7b7486d3 100644 --- a/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/enchants/EcoCraftEnchantment.java +++ b/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/enchants/EcoCraftEnchantment.java @@ -2,16 +2,11 @@ package com.willfp.ecoenchants.proxy.v1_16_R1.enchants; import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.support.vanilla.VanillaEnchantmentMetadata; +import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils; 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 { private final VanillaEnchantmentMetadata metadata; @@ -32,30 +27,6 @@ public class EcoCraftEnchantment extends CraftEnchantment { } 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) { - } + EnchantmentUtils.register(this); } } diff --git a/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/enchants/EcoCraftEnchantment.java b/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/enchants/EcoCraftEnchantment.java index 781c48a2..1ec08a45 100644 --- a/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/enchants/EcoCraftEnchantment.java +++ b/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/enchants/EcoCraftEnchantment.java @@ -2,16 +2,11 @@ package com.willfp.ecoenchants.proxy.v1_16_R2.enchants; import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.support.vanilla.VanillaEnchantmentMetadata; +import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils; 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 { private final VanillaEnchantmentMetadata metadata; @@ -32,30 +27,6 @@ public class EcoCraftEnchantment extends CraftEnchantment { } 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) { - } + EnchantmentUtils.register(this); } } diff --git a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/enchants/EcoCraftEnchantment.java b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/enchants/EcoCraftEnchantment.java index a445720e..ec2f188f 100644 --- a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/enchants/EcoCraftEnchantment.java +++ b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/enchants/EcoCraftEnchantment.java @@ -2,16 +2,11 @@ package com.willfp.ecoenchants.proxy.v1_16_R3.enchants; import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.support.vanilla.VanillaEnchantmentMetadata; +import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils; import net.minecraft.server.v1_16_R3.Enchantment; -import org.bukkit.NamespacedKey; import org.bukkit.craftbukkit.v1_16_R3.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 { private final VanillaEnchantmentMetadata metadata; @@ -32,30 +27,6 @@ public class EcoCraftEnchantment extends CraftEnchantment { } 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) { - } + EnchantmentUtils.register(this); } } diff --git a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_17_R1/enchants/EcoCraftEnchantment.java b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_17_R1/enchants/EcoCraftEnchantment.java index f5a58348..7e8aab2a 100644 --- a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_17_R1/enchants/EcoCraftEnchantment.java +++ b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_17_R1/enchants/EcoCraftEnchantment.java @@ -2,16 +2,11 @@ package com.willfp.ecoenchants.proxy.v1_17_R1.enchants; import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.support.vanilla.VanillaEnchantmentMetadata; +import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils; import net.minecraft.world.item.enchantment.Enchantment; -import org.bukkit.NamespacedKey; import org.bukkit.craftbukkit.v1_17_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 { private final VanillaEnchantmentMetadata metadata; @@ -32,30 +27,6 @@ public class EcoCraftEnchantment extends CraftEnchantment { } 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) { - } + EnchantmentUtils.register(this); } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java index 7b9a99a0..abbc07a7 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java @@ -24,19 +24,16 @@ import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; import org.jetbrains.annotations.NotNull; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; -@SuppressWarnings({"unchecked", "deprecation", "RedundantSuppression"}) +@SuppressWarnings({"deprecation", "RedundantSuppression"}) public abstract class EcoEnchant extends Enchantment implements Listener, Watcher { /** * Instance of EcoEnchants for enchantments to be able to access. @@ -233,36 +230,7 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Watche * Only used internally. */ public void register() { - try { - Field byIdField = Enchantment.class.getDeclaredField("byKey"); - Field byNameField = 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()); - byName.remove(this.getDisplayName()); - - Map byNameClone = new HashMap<>(byName); - for (Map.Entry entry : byNameClone.entrySet()) { - if (entry.getValue().getKey().equals(this.getKey())) { - byName.remove(entry.getKey()); - } - } - - if (this.getPlugin().getConfigYml().getBool("advanced.dual-registration.enabled")) { - byName.put(this.getDisplayName(), this); - } - - Field f = Enchantment.class.getDeclaredField("acceptingNew"); - f.setAccessible(true); - f.set(null, true); - f.setAccessible(false); - - Enchantment.registerEnchantment(this); - } catch (NoSuchFieldException | IllegalAccessException ignored) { - } + EnchantmentUtils.register(this); } /** diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/EnchantmentUtils.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/EnchantmentUtils.java index 43100c3d..c250b2f0 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/EnchantmentUtils.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/EnchantmentUtils.java @@ -4,14 +4,22 @@ import com.willfp.eco.core.integrations.placeholder.PlaceholderEntry; import com.willfp.eco.core.integrations.placeholder.PlaceholderManager; import com.willfp.eco.util.NumberUtils; import com.willfp.eco.util.StringUtils; +import com.willfp.ecoenchants.EcoEnchantsPlugin; import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchants; import lombok.experimental.UtilityClass; +import org.bukkit.NamespacedKey; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + @UtilityClass +@SuppressWarnings({"unchecked", "deprecation"}) public class EnchantmentUtils { /** * If the enchantment has successfully passed its specified chance. @@ -68,4 +76,47 @@ public class EnchantmentUtils { ); }); } + + /** + * Register enchantment with the server. + * + * @param enchantment The enchantment. + */ + public static void register(@NotNull final Enchantment enchantment) { + try { + Field byIdField = Enchantment.class.getDeclaredField("byKey"); + Field byNameField = Enchantment.class.getDeclaredField("byName"); + byIdField.setAccessible(true); + byNameField.setAccessible(true); + Map byKey = (Map) byIdField.get(null); + Map byName = (Map) byNameField.get(null); + byKey.remove(enchantment.getKey()); + byName.remove(enchantment.getName()); + + if (enchantment instanceof EcoEnchant) { + byName.remove(((EcoEnchant) enchantment).getDisplayName()); + } + + Map byNameClone = new HashMap<>(byName); + for (Map.Entry entry : byNameClone.entrySet()) { + if (entry.getValue().getKey().equals(enchantment.getKey())) { + byName.remove(entry.getKey()); + } + } + + if (enchantment instanceof EcoEnchant) { + if (EcoEnchantsPlugin.getInstance().getConfigYml().getBool("advanced.dual-registration.enabled")) { + byName.put(((EcoEnchant) enchantment).getDisplayName(), enchantment); + } + } + + Field f = Enchantment.class.getDeclaredField("acceptingNew"); + f.setAccessible(true); + f.set(null, true); + f.setAccessible(false); + + Enchantment.registerEnchantment(enchantment); + } catch (NoSuchFieldException | IllegalAccessException ignored) { + } + } }