Advertisement
Guest User

Untitled

a guest
May 26th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. package rush.fgeradores.apis;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Method;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7.  
  8. import org.bukkit.entity.EntityType;
  9. import org.bukkit.inventory.ItemStack;
  10.  
  11. import rush.fgeradores.util.ReflectionUtils;
  12.  
  13. public class ItemAPI {
  14.  
  15. private static Field map;
  16. private static Method asNMSCopy;
  17. private static Method hasNBTTagCompound;
  18. private static Method getNBTTagCompound;
  19.  
  20. @SuppressWarnings("deprecation")
  21. public static EntityType getEntityType(ItemStack item) {
  22. Map<String, Object> map = ItemAPI.getNBTTags(item);
  23.  
  24. // Verificando se ele possui alguma NBTTag
  25. if (map == null) {
  26. return null;
  27. }
  28.  
  29. for (Entry<String, Object> entry : map.entrySet()) {
  30. try {
  31. String value = entry.getValue().toString().toUpperCase().replace("\"", "").replace("'", "");
  32.  
  33. try {
  34. EntityType tipo = EntityType.valueOf(value.toUpperCase());
  35. return tipo;
  36. } catch (Throwable e) {}
  37.  
  38. try {
  39. EntityType tipo = EntityType.fromName(value);
  40. return tipo;
  41. } catch (Throwable e) {}
  42.  
  43. } catch (Throwable e) {}
  44. }
  45.  
  46. return null;
  47. }
  48.  
  49. @SuppressWarnings("unchecked")
  50. private static Map<String, Object> getNBTTags(ItemStack item) {
  51. try {
  52. Object CraftItemStack = asNMSCopy.invoke(null, item);
  53. boolean hasNBTTag = (boolean) hasNBTTagCompound.invoke(CraftItemStack);
  54. if (hasNBTTag) {
  55. Object NBTTagCompound = getNBTTagCompound.invoke(CraftItemStack);
  56. return (Map<String, Object>) map.get(NBTTagCompound);
  57. }
  58. return null;
  59. } catch (Throwable e) {
  60. e.printStackTrace();
  61. return null;
  62. }
  63. }
  64.  
  65. public static void load() {
  66. try
  67. {
  68. // Item Classes
  69. Class<?> CraftItemStackClass = ReflectionUtils.getOBClass("inventory.CraftItemStack");
  70. Class<?> ItemStackClass = ReflectionUtils.getNMSClass("ItemStack");
  71. Class<?> NBTTagCompoundClass = ReflectionUtils.getNMSClass("NBTTagCompound");
  72.  
  73. // Field map
  74. map = NBTTagCompoundClass.getDeclaredField("map");
  75. map.setAccessible(true);
  76.  
  77. // Item Handle Methods
  78. asNMSCopy = CraftItemStackClass.getDeclaredMethod("asNMSCopy", ItemStack.class);
  79.  
  80. // Item NBTTag Methods
  81. getNBTTagCompound = ItemStackClass.getDeclaredMethod("getTag");
  82. hasNBTTagCompound = ItemStackClass.getDeclaredMethod("hasTag");
  83.  
  84. }
  85. catch (Throwable e) {
  86. e.printStackTrace();
  87. }
  88. }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement