Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rush.fgeradores.apis;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import java.util.Map;
- import java.util.Map.Entry;
- import org.bukkit.entity.EntityType;
- import org.bukkit.inventory.ItemStack;
- import rush.fgeradores.util.ReflectionUtils;
- public class ItemAPI {
- private static Field map;
- private static Method asNMSCopy;
- private static Method hasNBTTagCompound;
- private static Method getNBTTagCompound;
- @SuppressWarnings("deprecation")
- public static EntityType getEntityType(ItemStack item) {
- Map<String, Object> map = ItemAPI.getNBTTags(item);
- // Verificando se ele possui alguma NBTTag
- if (map == null) {
- return null;
- }
- for (Entry<String, Object> entry : map.entrySet()) {
- try {
- String value = entry.getValue().toString().toUpperCase().replace("\"", "").replace("'", "");
- try {
- EntityType tipo = EntityType.valueOf(value.toUpperCase());
- return tipo;
- } catch (Throwable e) {}
- try {
- EntityType tipo = EntityType.fromName(value);
- return tipo;
- } catch (Throwable e) {}
- } catch (Throwable e) {}
- }
- return null;
- }
- @SuppressWarnings("unchecked")
- private static Map<String, Object> getNBTTags(ItemStack item) {
- try {
- Object CraftItemStack = asNMSCopy.invoke(null, item);
- boolean hasNBTTag = (boolean) hasNBTTagCompound.invoke(CraftItemStack);
- if (hasNBTTag) {
- Object NBTTagCompound = getNBTTagCompound.invoke(CraftItemStack);
- return (Map<String, Object>) map.get(NBTTagCompound);
- }
- return null;
- } catch (Throwable e) {
- e.printStackTrace();
- return null;
- }
- }
- public static void load() {
- try
- {
- // Item Classes
- Class<?> CraftItemStackClass = ReflectionUtils.getOBClass("inventory.CraftItemStack");
- Class<?> ItemStackClass = ReflectionUtils.getNMSClass("ItemStack");
- Class<?> NBTTagCompoundClass = ReflectionUtils.getNMSClass("NBTTagCompound");
- // Field map
- map = NBTTagCompoundClass.getDeclaredField("map");
- map.setAccessible(true);
- // Item Handle Methods
- asNMSCopy = CraftItemStackClass.getDeclaredMethod("asNMSCopy", ItemStack.class);
- // Item NBTTag Methods
- getNBTTagCompound = ItemStackClass.getDeclaredMethod("getTag");
- hasNBTTagCompound = ItemStackClass.getDeclaredMethod("hasTag");
- }
- catch (Throwable e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement