Advertisement
williambriggs

Deserialization

Jul 20th, 2022
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. public static @NotNull Inventory deserializeInventory(byte[] bytes, Player player) throws IOException {
  2.  
  3. ByteArrayInputStream is = new ByteArrayInputStream(bytes);
  4. ObjectInputStream input = new ObjectInputStream(is);
  5.  
  6. Inventory backpack = Bukkit.createInventory(player, InventoryType.DISPENSER, Component.text("Backpack"));
  7.  
  8. int size = input.readInt();
  9.  
  10. for (int i = 0; i < size; i++) {
  11. int slot = input.readInt();
  12. int rawSize = input.readInt();
  13. byte[] rawItem = new byte[rawSize];
  14. if (input.read(rawItem) == rawSize){
  15. ItemStack itemStack = ItemStack.deserializeBytes(rawItem);
  16. backpack.setItem(slot, itemStack);
  17. }
  18. }
  19.  
  20.  
  21. return backpack;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement