Better way to find rarity on paper.

This commit is contained in:
alexcrea 2024-08-28 14:58:43 +02:00
parent 6a0baf91ff
commit cf4cc740e8
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
2 changed files with 78 additions and 3 deletions

View file

@ -12,12 +12,12 @@ public class EnchantmentRarity {
private final int itemValue;
private final int bookValue;
public EnchantmentRarity(int itemValue, int bookValue) {
private EnchantmentRarity(int itemValue, int bookValue) {
this.itemValue = itemValue;
this.bookValue = bookValue;
}
public EnchantmentRarity(int itemValue) {
private EnchantmentRarity(int itemValue) {
this(itemValue, Math.max(1, itemValue / 2));
}
@ -29,4 +29,24 @@ public class EnchantmentRarity {
return itemValue;
}
public static EnchantmentRarity getRarity(int itemValue, int bookValue){
int expectedBook = Math.max(1, itemValue / 2);
if((expectedBook == bookValue) && (itemValue != 0)) return getRarity(itemValue);
if(itemValue == 0 && bookValue == 0) return NO_RARITY;
return new EnchantmentRarity(itemValue, bookValue);
}
public static EnchantmentRarity getRarity(int itemValue){
return switch (itemValue) {
case 0 -> NO_RARITY;
case 1 -> COMMON;
case 2 -> UNCOMMON;
case 4 -> RARE;
case 8 -> VERY_RARE;
default -> new EnchantmentRarity(itemValue);
};
}
}

View file

@ -1,7 +1,9 @@
package xyz.alexcrea.cuanvil.enchant.wrapped;
import io.delilaheve.CustomAnvil;
import io.delilaheve.util.ItemUtil;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.enchantments.EnchantmentTarget;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
@ -11,7 +13,12 @@ import xyz.alexcrea.cuanvil.enchant.CAEnchantmentBase;
import xyz.alexcrea.cuanvil.enchant.EnchantmentProperties;
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
/**
* Custom Anvil enchantment implementation for vanilla registered enchantment.
@ -94,7 +101,7 @@ public class CABukkitEnchantment extends CAEnchantmentBase {
try {
return EnchantmentProperties.valueOf(enchantment.getKey().getKey().toUpperCase(Locale.ENGLISH)).getRarity();
} catch (IllegalArgumentException ignored) {
return EnchantmentRarity.COMMON;
return findRarity(enchantment);
}
}
@ -102,4 +109,52 @@ public class CABukkitEnchantment extends CAEnchantmentBase {
protected Enchantment getEnchant() {
return this.enchantment;
}
private static Method getAnvilCostMethod;
static {
Class<Enchantment> clazz = Enchantment.class;
try {
getAnvilCostMethod = clazz.getDeclaredMethod("getAnvilCost");
getAnvilCostMethod.setAccessible(true);
CustomAnvil.Companion.log("Detected getAnvilCost method");
} catch (NoSuchMethodException e) {
getAnvilCostMethod = null;
}
}
private static final Map<EnchantmentTarget, String> targetToGroup = new HashMap<>();
static {
targetToGroup.put(EnchantmentTarget.ARMOR, "armors");
targetToGroup.put(EnchantmentTarget.ARMOR_HEAD, "helmets");
targetToGroup.put(EnchantmentTarget.ARMOR_TORSO, "chestplate");
targetToGroup.put(EnchantmentTarget.ARMOR_LEGS, "leggings");
targetToGroup.put(EnchantmentTarget.ARMOR_FEET, "boots");
targetToGroup.put(EnchantmentTarget.BOW, "bow");
targetToGroup.put(EnchantmentTarget.BREAKABLE, "can_unbreak");
targetToGroup.put(EnchantmentTarget.CROSSBOW, "crossbow");
targetToGroup.put(EnchantmentTarget.FISHING_ROD, "fishing_rod");
targetToGroup.put(EnchantmentTarget.TOOL, "tools");
targetToGroup.put(EnchantmentTarget.TRIDENT, "trident");
targetToGroup.put(EnchantmentTarget.VANISHABLE, "can_vanish");
targetToGroup.put(EnchantmentTarget.WEAPON, "swords");
targetToGroup.put(EnchantmentTarget.WEARABLE, "wearable");
}
private static EnchantmentRarity findRarity(Enchantment enchantment) {
if(getAnvilCostMethod == null) return EnchantmentRarity.COMMON;
try {
int itemCost = (int) getAnvilCostMethod.invoke(enchantment);
return EnchantmentRarity.getRarity(itemCost);
} catch (IllegalAccessException | InvocationTargetException e) {
CustomAnvil.instance.getLogger().log(Level.SEVERE, "could not find cost for enchantment "+enchantment.getKey(), e);
return EnchantmentRarity.COMMON;
}
}
}