mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
use reflection for enchantment definition
This commit is contained in:
parent
b7e19355a8
commit
76e5059632
2 changed files with 78 additions and 8 deletions
|
|
@ -5,31 +5,32 @@ import org.bukkit.inventory.ItemStack;
|
|||
import org.jetbrains.annotations.NotNull;
|
||||
import su.nightexpress.excellentenchants.api.enchantment.CustomEnchantment;
|
||||
import su.nightexpress.excellentenchants.api.item.ItemSet;
|
||||
import su.nightexpress.excellentenchants.api.wrapper.EnchantDefinition;
|
||||
import xyz.alexcrea.cuanvil.enchant.AdditionalTestEnchantment;
|
||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class CAEEV5Enchantment extends CABukkitEnchantment implements AdditionalTestEnchantment {
|
||||
|
||||
@NotNull CustomEnchantment eeenchantment;
|
||||
@NotNull EnchantDefinition definition;
|
||||
@NotNull Object definition;
|
||||
|
||||
public CAEEV5Enchantment(@NotNull CustomEnchantment enchantment) {
|
||||
super(enchantment.getBukkitEnchantment(), EnchantmentRarity.getRarity(enchantment.getDefinition().getAnvilCost()));
|
||||
super(enchantment.getBukkitEnchantment(), EnchantmentRarity.getRarity(getAnvilCost(enchantment)));
|
||||
this.eeenchantment = enchantment;
|
||||
this.definition = enchantment.getDefinition();
|
||||
this.definition = getDefinition(enchantment);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnchantConflict(@NotNull Map<CAEnchantment, Integer> enchantments, @NotNull Material itemMat) {
|
||||
if (!definition.hasConflicts()) return false;
|
||||
if (!hasConflicts()) return false;
|
||||
|
||||
Set<String> conflicts = definition.getExclusiveSet();
|
||||
Set<String> conflicts = getExclusiveSet();
|
||||
|
||||
for (CAEnchantment caEnchantment : enchantments.keySet()) {
|
||||
if (conflicts.contains(caEnchantment.getName())) return true;
|
||||
|
|
@ -52,4 +53,74 @@ public class CAEEV5Enchantment extends CABukkitEnchantment implements Additional
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
private static final Method getDefinitonMethod;
|
||||
|
||||
private static final Method getAnvilCostMethod;
|
||||
private static final Method hasConflictsMethod;
|
||||
private static final Method getExclusiveSetMethod;
|
||||
static {
|
||||
var enchClazz = CustomEnchantment.class;
|
||||
try {
|
||||
getDefinitonMethod = enchClazz.getDeclaredMethod("getDefinition");
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
Class<?> definitionClazz;
|
||||
try {
|
||||
definitionClazz = Class.forName("su.nightexpress.excellentenchants.api.EnchantDefinition");
|
||||
} catch (ClassNotFoundException e) {
|
||||
try {
|
||||
definitionClazz = Class.forName("su.nightexpress.excellentenchants.api.wrapper.EnchantDefinition");
|
||||
} catch (ClassNotFoundException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Now definition methods
|
||||
try {
|
||||
getAnvilCostMethod = definitionClazz.getDeclaredMethod("getAnvilCost");
|
||||
hasConflictsMethod = definitionClazz.getDeclaredMethod("hasConflicts");
|
||||
getExclusiveSetMethod = definitionClazz.getDeclaredMethod("getExclusiveSet");
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static Object getDefinition(CustomEnchantment enchantment) {
|
||||
try {
|
||||
return getDefinitonMethod.invoke(enchantment);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static int getAnvilCost(CustomEnchantment enchantment) {
|
||||
try {
|
||||
return (int) getAnvilCostMethod.invoke(getDefinition(enchantment));
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasConflicts() {
|
||||
try {
|
||||
return (boolean) hasConflictsMethod.invoke(definition);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Set<String> getExclusiveSet() {
|
||||
try {
|
||||
return (Set<String>) getExclusiveSetMethod.invoke(definition);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -163,8 +163,8 @@ class ExcellentEnchantsDependency {
|
|||
}
|
||||
|
||||
when (listenerVersion) {
|
||||
ListenerVersion.V5_3,
|
||||
ListenerVersion.V5,
|
||||
ListenerVersion.V5_3
|
||||
-> this.usedAnvilListener = v5AnvilListener!!
|
||||
ListenerVersion.PRE_V5 -> this.usedAnvilListener = preV5AnvilListener!!
|
||||
ListenerVersion.LEGACY -> this.usedAnvilListener = legacyAnvilListener!!
|
||||
|
|
@ -185,7 +185,6 @@ class ExcellentEnchantsDependency {
|
|||
this.handleRechargeMethod.setAccessible(true)
|
||||
|
||||
try {
|
||||
this.usedAnvilListener.javaClass.methods.forEach { method -> CustomAnvil.instance.logger.warning { method.name } }
|
||||
this.handleCombineMethod = this.usedAnvilListener.javaClass.getDeclaredMethod(
|
||||
"anvilCombine",
|
||||
PrepareAnvilEvent::class.java, ItemStack::class.java, ItemStack::class.java, ItemStack::class.java
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue