Created FastGetEnchants

This commit is contained in:
Auxilor 2020-12-19 21:19:57 +00:00
parent f258d5a6c7
commit e4e7f860bf
7 changed files with 188 additions and 31 deletions

View File

@ -0,0 +1,10 @@
package com.willfp.ecoenchants.nms.api;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import java.util.Map;
public interface FastGetEnchantsWrapper {
Map<Enchantment, Integer> getEnchantmentsOnItem(ItemStack itemStack);
}

View File

@ -0,0 +1,32 @@
package com.willfp.ecoenchants.nms.v1_15_R1;
import com.willfp.ecoenchants.nms.api.FastGetEnchantsWrapper;
import net.minecraft.server.v1_15_R1.NBTBase;
import net.minecraft.server.v1_15_R1.NBTTagCompound;
import net.minecraft.server.v1_15_R1.NBTTagList;
import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_15_R1.util.CraftNamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
public class FastGetEnchants implements FastGetEnchantsWrapper {
@Override
public Map<Enchantment, Integer> getEnchantmentsOnItem(ItemStack itemStack) {
net.minecraft.server.v1_15_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();
HashMap<Enchantment, Integer> foundEnchantments = new HashMap<>();
for (NBTBase base : enchantmentNBT) {
NBTTagCompound compound = (NBTTagCompound) base;
String key = compound.getString("id");
int level = '\uffff' & compound.getShort("lvl");
Enchantment found = Enchantment.getByKey(CraftNamespacedKey.fromStringOrNull(key));
if(found != null) foundEnchantments.put(found, level);
}
return foundEnchantments;
}
}

View File

@ -0,0 +1,32 @@
package com.willfp.ecoenchants.nms.v1_16_R1;
import com.willfp.ecoenchants.nms.api.FastGetEnchantsWrapper;
import net.minecraft.server.v1_16_R1.NBTBase;
import net.minecraft.server.v1_16_R1.NBTTagCompound;
import net.minecraft.server.v1_16_R1.NBTTagList;
import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_16_R1.util.CraftNamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
public class FastGetEnchants implements FastGetEnchantsWrapper {
@Override
public Map<Enchantment, Integer> getEnchantmentsOnItem(ItemStack itemStack) {
net.minecraft.server.v1_16_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();
HashMap<Enchantment, Integer> foundEnchantments = new HashMap<>();
for (NBTBase base : enchantmentNBT) {
NBTTagCompound compound = (NBTTagCompound) base;
String key = compound.getString("id");
int level = '\uffff' & compound.getShort("lvl");
Enchantment found = Enchantment.getByKey(CraftNamespacedKey.fromStringOrNull(key));
if(found != null) foundEnchantments.put(found, level);
}
return foundEnchantments;
}
}

View File

@ -0,0 +1,32 @@
package com.willfp.ecoenchants.nms.v1_16_R2;
import com.willfp.ecoenchants.nms.api.FastGetEnchantsWrapper;
import net.minecraft.server.v1_16_R2.NBTBase;
import net.minecraft.server.v1_16_R2.NBTTagCompound;
import net.minecraft.server.v1_16_R2.NBTTagList;
import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_16_R2.util.CraftNamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
public class FastGetEnchants implements FastGetEnchantsWrapper {
@Override
public Map<Enchantment, Integer> getEnchantmentsOnItem(ItemStack itemStack) {
net.minecraft.server.v1_16_R2.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();
HashMap<Enchantment, Integer> foundEnchantments = new HashMap<>();
for (NBTBase base : enchantmentNBT) {
NBTTagCompound compound = (NBTTagCompound) base;
String key = compound.getString("id");
int level = '\uffff' & compound.getShort("lvl");
Enchantment found = Enchantment.getByKey(CraftNamespacedKey.fromStringOrNull(key));
if(found != null) foundEnchantments.put(found, level);
}
return foundEnchantments;
}
}

View File

@ -0,0 +1,32 @@
package com.willfp.ecoenchants.nms.v1_16_R3;
import com.willfp.ecoenchants.nms.api.FastGetEnchantsWrapper;
import net.minecraft.server.v1_16_R3.NBTBase;
import net.minecraft.server.v1_16_R3.NBTTagCompound;
import net.minecraft.server.v1_16_R3.NBTTagList;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_16_R3.util.CraftNamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
public class FastGetEnchants implements FastGetEnchantsWrapper {
@Override
public Map<Enchantment, Integer> getEnchantmentsOnItem(ItemStack itemStack) {
net.minecraft.server.v1_16_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();
HashMap<Enchantment, Integer> foundEnchantments = new HashMap<>();
for (NBTBase base : enchantmentNBT) {
NBTTagCompound compound = (NBTTagCompound) base;
String key = compound.getString("id");
int level = '\uffff' & compound.getShort("lvl");
Enchantment found = Enchantment.getByKey(CraftNamespacedKey.fromStringOrNull(key));
if(found != null) foundEnchantments.put(found, level);
}
return foundEnchantments;
}
}

View File

@ -2,6 +2,7 @@ package com.willfp.ecoenchants.enchantments.util;
import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.nms.FastGetEnchants;
import com.willfp.ecoenchants.util.DurabilityUtils; import com.willfp.ecoenchants.util.DurabilityUtils;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
@ -45,10 +46,10 @@ public class EnchantChecks {
public static int getItemLevel(ItemStack item, Enchantment enchantment) { public static int getItemLevel(ItemStack item, Enchantment enchantment) {
if (item == null) return 0; if (item == null) return 0;
if (item.getType().equals(Material.AIR)) return 0; if (item.getType().equals(Material.AIR)) return 0;
if (!item.hasItemMeta()) return 0;
if (item.getItemMeta() == null) return 0;
return item.getItemMeta().getEnchantLevel(enchantment); Integer level = FastGetEnchants.getEnchantsOnItem(item).get(enchantment);
return level == null ? 0 : level;
} }
/** /**
@ -61,15 +62,12 @@ public class EnchantChecks {
public static Map<EcoEnchant, Integer> getEnchantsOnItem(ItemStack item) { public static Map<EcoEnchant, Integer> getEnchantsOnItem(ItemStack item) {
if (item == null) return new HashMap<>(); if (item == null) return new HashMap<>();
if (item.getType().equals(Material.AIR)) return new HashMap<>(); if (item.getType().equals(Material.AIR)) return new HashMap<>();
if (!item.hasItemMeta()) return new HashMap<>();
if (item.getItemMeta() == null) return new HashMap<>();
Map<EcoEnchant, Integer> ecoEnchants = new HashMap<>(); Map<EcoEnchant, Integer> ecoEnchants = new HashMap<>();
item.getEnchantments().forEach(((enchantment, integer) -> { for (Map.Entry<Enchantment, Integer> enchantmentIntegerEntry : FastGetEnchants.getEnchantsOnItem(item).entrySet()) {
if (EcoEnchants.getFromEnchantment(enchantment) != null) { EcoEnchant enchant = EcoEnchants.getFromEnchantment(enchantmentIntegerEntry.getKey());
ecoEnchants.put(EcoEnchants.getFromEnchantment(enchantment), integer); if(enchant != null) ecoEnchants.put(enchant, enchantmentIntegerEntry.getValue());
} }
}));
return ecoEnchants; return ecoEnchants;
} }
@ -177,16 +175,7 @@ public class EnchantChecks {
ItemStack item = entity.getEquipment().getItemInMainHand(); ItemStack item = entity.getEquipment().getItemInMainHand();
if (item == null) return new HashMap<>(); return getEnchantsOnItem(item);
Map<EcoEnchant, Integer> ecoEnchants = new HashMap<>();
for (Map.Entry<Enchantment, Integer> entry : item.getEnchantments().entrySet()) {
EcoEnchant enchant = EcoEnchants.getFromEnchantment(entry.getKey());
if(enchant != null) ecoEnchants.put(enchant, entry.getValue());
}
return ecoEnchants;
} }
/** /**
@ -231,17 +220,7 @@ public class EnchantChecks {
ItemStack item = entity.getEquipment().getItemInOffHand(); ItemStack item = entity.getEquipment().getItemInOffHand();
if (item == null) return new HashMap<>(); return getEnchantsOnItem(item);
Map<EcoEnchant, Integer> ecoEnchants = new HashMap<>();
item.getEnchantments().forEach(((enchantment, integer) -> {
if (EcoEnchants.getFromEnchantment(enchantment) != null) {
ecoEnchants.put(EcoEnchants.getFromEnchantment(enchantment), integer);
}
}));
return ecoEnchants;
} }
/** /**

View File

@ -0,0 +1,40 @@
package com.willfp.ecoenchants.nms;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.nms.api.FastGetEnchantsWrapper;
import com.willfp.ecoenchants.util.internal.Logger;
import org.bukkit.Bukkit;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import java.util.Map;
/**
* Utility class to break a block as if the player had done it manually
*/
public class FastGetEnchants {
private static FastGetEnchantsWrapper fastGetEnchantsWrapper;
/**
* 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);
}
static {
try {
final Class<?> class2 = Class.forName("com.willfp.ecoenchants.nms." + EcoEnchantsPlugin.NMS_VERSION + ".FastGetEnchants");
if (FastGetEnchantsWrapper.class.isAssignableFrom(class2)) {
fastGetEnchantsWrapper = (FastGetEnchantsWrapper) class2.getConstructor().newInstance();
}
} catch (Exception e) {
Logger.error("&cYou're running an unsupported server version: " + EcoEnchantsPlugin.NMS_VERSION);
Bukkit.getPluginManager().disablePlugin(EcoEnchantsPlugin.getInstance());
}
}
}