BingoRufus

ItemStack Serializer

Aug 25th, 2020
2,398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. import java.lang.reflect.InvocationTargetException;
  2. import java.lang.reflect.Method;
  3.  
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.Material;
  6. import org.bukkit.inventory.ItemStack;
  7.  
  8. import com.google.gson.JsonElement;
  9. import com.google.gson.JsonObject;
  10. import com.google.gson.JsonParser;
  11.  
  12. public class ItemSerializer {
  13.     private Class<?> craftItemStack;
  14.     private Class<?> mojangsonParser;
  15.     private Class<?> nmsItemStack;
  16.  
  17.     public ItemSerializer() {
  18.         try {
  19.             String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
  20.  
  21.             craftItemStack = Class
  22.                     .forName("org.bukkit.craftbukkit.{v}.inventory.CraftItemStack".replace("{v}", version));
  23.  
  24.             mojangsonParser = Class.forName("net.minecraft.server.{v}.MojangsonParser".replace("{v}", version));
  25.  
  26.             nmsItemStack = Class.forName("net.minecraft.server.{v}.ItemStack".replace("{v}", version));
  27.  
  28.         } catch (ClassNotFoundException e) {
  29.             e.printStackTrace();
  30.         }
  31.  
  32.     }
  33.  
  34.     private Object nmsItem(ItemStack item) throws IllegalAccessException, IllegalArgumentException,
  35.             InvocationTargetException, NoSuchMethodException, SecurityException {
  36.  
  37.         Method asNms = craftItemStack.getMethod("asNMSCopy", ItemStack.class);
  38.         asNms.setAccessible(true);
  39.         Object nmsItem = asNms.invoke(craftItemStack, item);
  40.         return nmsItem;
  41.  
  42.     }
  43.  
  44.     public String serialize(ItemStack item) {
  45.         JsonObject itemJson = new JsonObject();
  46.         itemJson.addProperty("id", item.getType().getKey().toString());
  47.         itemJson.addProperty("Count", item.getAmount());
  48.         itemJson.add("tag", (JsonElement) new JsonParser().parse(getNBT(item)));
  49.         return itemJson.toString();
  50.     }
  51.  
  52.     public ItemStack deserialize(String json) {
  53.         try {
  54.             JsonObject itemJson = (JsonObject) new JsonParser().parse(json);
  55.             Material mat = Material.matchMaterial(itemJson.get("id").getAsString());
  56.             int count = itemJson.get("Count").getAsInt();
  57.  
  58.             ItemStack item = new ItemStack(mat, count);
  59.             Object nmsItem = nmsItem(item);
  60.  
  61.             Method parseNBT = mojangsonParser.getMethod("parse", String.class);
  62.             Object nbtCompound = parseNBT.invoke(mojangsonParser, itemJson.get("tag").getAsString()); // Turns NBT
  63.                                                                                                         // string into
  64.                                                                                                         // NBTTagCompound
  65.  
  66.             Method setTag = nmsItemStack.getMethod("setTag", nbtCompound.getClass());
  67.             setTag.invoke(nmsItem, nbtCompound);
  68.  
  69.             Method asBukkitCopy = craftItemStack.getMethod("asBukkitCopy", nmsItemStack);
  70.             ItemStack editedItem = (ItemStack) asBukkitCopy.invoke(craftItemStack, nmsItem);
  71.             return editedItem;
  72.  
  73.         } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
  74.                 | InvocationTargetException e) {
  75.             e.printStackTrace();
  76.         }
  77.         return new ItemStack(Material.AIR);
  78.  
  79.     }
  80.  
  81.     private String getNBT(ItemStack item) {
  82.  
  83.         try {
  84.             Object nmsItem = nmsItem(item);
  85.             if (nmsItem == null) {
  86.                 throw new IllegalArgumentException(item.getType().name() + " could not be queried!");
  87.             }
  88.             Method hasTag = nmsItem.getClass().getMethod("hasTag");
  89.  
  90.             if ((boolean) hasTag.invoke(nmsItem)) {
  91.                 Method getTag = nmsItem.getClass().getMethod("getTag");
  92.                 Object nbtData = getTag.invoke(nmsItem);
  93.                 Method asString = nbtData.getClass().getMethod("asString");
  94.                 return (String) asString.invoke(nbtData);
  95.             }
  96.  
  97.         } catch (IllegalArgumentException | NoSuchMethodException | SecurityException | IllegalAccessException
  98.                 | InvocationTargetException e) {
  99.             e.printStackTrace();
  100.         }
  101.         return "{}";
  102.  
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment