Optimised FastGetEnchants for enchantment-specific level getting

This commit is contained in:
Auxilor 2020-12-20 13:19:24 +00:00
parent ff102602a4
commit faa9afc2a2
7 changed files with 77 additions and 4 deletions

View File

@ -7,4 +7,5 @@ import java.util.Map;
public interface FastGetEnchantsWrapper {
Map<Enchantment, Integer> getEnchantmentsOnItem(ItemStack itemStack);
int getLevelOnItem(ItemStack itemStack, Enchantment enchantment);
}

View File

@ -29,4 +29,20 @@ public class FastGetEnchants implements FastGetEnchantsWrapper {
}
return foundEnchantments;
}
@Override
public int getLevelOnItem(ItemStack itemStack, Enchantment enchantment) {
net.minecraft.server.v1_15_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();
for (NBTBase base : enchantmentNBT) {
NBTTagCompound compound = (NBTTagCompound) base;
String key = compound.getString("id");
if(!key.equals(enchantment.getKey().toString()))
continue;
return '\uffff' & compound.getShort("lvl");
}
return 0;
}
}

View File

@ -29,4 +29,20 @@ public class FastGetEnchants implements FastGetEnchantsWrapper {
}
return foundEnchantments;
}
@Override
public int getLevelOnItem(ItemStack itemStack, Enchantment enchantment) {
net.minecraft.server.v1_16_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();
for (NBTBase base : enchantmentNBT) {
NBTTagCompound compound = (NBTTagCompound) base;
String key = compound.getString("id");
if(!key.equals(enchantment.getKey().toString()))
continue;
return '\uffff' & compound.getShort("lvl");
}
return 0;
}
}

View File

@ -29,4 +29,20 @@ public class FastGetEnchants implements FastGetEnchantsWrapper {
}
return foundEnchantments;
}
@Override
public int getLevelOnItem(ItemStack itemStack, Enchantment enchantment) {
net.minecraft.server.v1_16_R2.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();
for (NBTBase base : enchantmentNBT) {
NBTTagCompound compound = (NBTTagCompound) base;
String key = compound.getString("id");
if(!key.equals(enchantment.getKey().toString()))
continue;
return '\uffff' & compound.getShort("lvl");
}
return 0;
}
}

View File

@ -29,4 +29,20 @@ public class FastGetEnchants implements FastGetEnchantsWrapper {
}
return foundEnchantments;
}
@Override
public int getLevelOnItem(ItemStack itemStack, Enchantment enchantment) {
net.minecraft.server.v1_16_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();
for (NBTBase base : enchantmentNBT) {
NBTTagCompound compound = (NBTTagCompound) base;
String key = compound.getString("id");
if(!key.equals(enchantment.getKey().toString()))
continue;
return '\uffff' & compound.getShort("lvl");
}
return 0;
}
}

View File

@ -47,9 +47,7 @@ public class EnchantChecks {
if (item == null) return 0;
if (item.getType().equals(Material.AIR)) return 0;
Integer level = FastGetEnchants.getEnchantsOnItem(item).get(enchantment);
return level == null ? 0 : level;
return FastGetEnchants.getLevelOnItem(item, enchantment);
}
/**

View File

@ -19,13 +19,23 @@ public class FastGetEnchants {
* Efficiently get enchantments on an item without instantiating ItemMeta
*
* @param item The item to query
*
* @return A map of all enchantments, where the integer is the level
*/
public static Map<Enchantment, Integer> getEnchantsOnItem(ItemStack item) {
return fastGetEnchantsWrapper.getEnchantmentsOnItem(item);
}
/**
* Efficiently get level of enchantment on an item without using getByKey or instantiating keys or ItemMeta
*
* @param item The item to query
* @param enchantment The enchantment to query
* @return The level found, or 0 if not found
*/
public static int getLevelOnItem(ItemStack item, Enchantment enchantment) {
return fastGetEnchantsWrapper.getLevelOnItem(item, enchantment);
}
static {
try {
final Class<?> class2 = Class.forName("com.willfp.ecoenchants.nms." + EcoEnchantsPlugin.NMS_VERSION + ".FastGetEnchants");