mirror of
https://github.com/Auxilor/EcoEnchants.git
synced 2024-12-24 20:07:34 +01:00
Optimised FastGetEnchants for enchantment-specific level getting
This commit is contained in:
parent
ff102602a4
commit
faa9afc2a2
@ -7,4 +7,5 @@ import java.util.Map;
|
||||
|
||||
public interface FastGetEnchantsWrapper {
|
||||
Map<Enchantment, Integer> getEnchantmentsOnItem(ItemStack itemStack);
|
||||
int getLevelOnItem(ItemStack itemStack, Enchantment enchantment);
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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");
|
||||
|
Loading…
Reference in New Issue
Block a user