Cleaned up registration code

This commit is contained in:
Auxilor 2021-06-12 10:37:22 +01:00
parent d201654511
commit ca8169d46b
6 changed files with 61 additions and 158 deletions

View File

@ -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<NamespacedKey, org.bukkit.enchantments.Enchantment> byKey = (Map<NamespacedKey, org.bukkit.enchantments.Enchantment>) byIdField.get(null);
Map<String, org.bukkit.enchantments.Enchantment> byName = (Map<String, org.bukkit.enchantments.Enchantment>) byNameField.get(null);
byKey.remove(this.getKey());
byName.remove(this.getName());
Map<String, org.bukkit.enchantments.Enchantment> byNameClone = new HashMap<>(byName);
for (Map.Entry<String, org.bukkit.enchantments.Enchantment> 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);
}
}

View File

@ -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<NamespacedKey, org.bukkit.enchantments.Enchantment> byKey = (Map<NamespacedKey, org.bukkit.enchantments.Enchantment>) byIdField.get(null);
Map<String, org.bukkit.enchantments.Enchantment> byName = (Map<String, org.bukkit.enchantments.Enchantment>) byNameField.get(null);
byKey.remove(this.getKey());
byName.remove(this.getName());
Map<String, org.bukkit.enchantments.Enchantment> byNameClone = new HashMap<>(byName);
for (Map.Entry<String, org.bukkit.enchantments.Enchantment> 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);
}
}

View File

@ -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<NamespacedKey, org.bukkit.enchantments.Enchantment> byKey = (Map<NamespacedKey, org.bukkit.enchantments.Enchantment>) byIdField.get(null);
Map<String, org.bukkit.enchantments.Enchantment> byName = (Map<String, org.bukkit.enchantments.Enchantment>) byNameField.get(null);
byKey.remove(this.getKey());
byName.remove(this.getName());
Map<String, org.bukkit.enchantments.Enchantment> byNameClone = new HashMap<>(byName);
for (Map.Entry<String, org.bukkit.enchantments.Enchantment> 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);
}
}

View File

@ -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<NamespacedKey, org.bukkit.enchantments.Enchantment> byKey = (Map<NamespacedKey, org.bukkit.enchantments.Enchantment>) byIdField.get(null);
Map<String, org.bukkit.enchantments.Enchantment> byName = (Map<String, org.bukkit.enchantments.Enchantment>) byNameField.get(null);
byKey.remove(this.getKey());
byName.remove(this.getName());
Map<String, org.bukkit.enchantments.Enchantment> byNameClone = new HashMap<>(byName);
for (Map.Entry<String, org.bukkit.enchantments.Enchantment> 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);
}
}

View File

@ -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<NamespacedKey, Enchantment> byKey = (Map<NamespacedKey, Enchantment>) byIdField.get(null);
Map<String, Enchantment> byName = (Map<String, Enchantment>) byNameField.get(null);
byKey.remove(this.getKey());
byName.remove(this.getName());
byName.remove(this.getDisplayName());
Map<String, Enchantment> byNameClone = new HashMap<>(byName);
for (Map.Entry<String, Enchantment> 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);
}
/**

View File

@ -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<NamespacedKey, Enchantment> byKey = (Map<NamespacedKey, Enchantment>) byIdField.get(null);
Map<String, Enchantment> byName = (Map<String, Enchantment>) byNameField.get(null);
byKey.remove(enchantment.getKey());
byName.remove(enchantment.getName());
if (enchantment instanceof EcoEnchant) {
byName.remove(((EcoEnchant) enchantment).getDisplayName());
}
Map<String, Enchantment> byNameClone = new HashMap<>(byName);
for (Map.Entry<String, Enchantment> 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) {
}
}
}