Guest User

InventorySerializer.java

a guest
Dec 7th, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.33 KB | None | 0 0
  1. import com.destroystokyo.paper.util.SneakyThrow;
  2. import com.google.common.io.ByteArrayDataOutput;
  3. import com.google.common.io.ByteStreams;
  4. import lombok.NonNull;
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.inventory.Inventory;
  7. import org.bukkit.inventory.ItemStack;
  8. import org.bukkit.util.io.BukkitObjectInputStream;
  9. import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
  10.  
  11. import javax.annotation.Nullable;
  12. import java.io.*;
  13. import java.util.Arrays;
  14. import java.util.Objects;
  15.  
  16. // Author: Dymeth.Ru
  17. @SuppressWarnings({"unused", "NullableProblems"})
  18. public class InventorySerializer {
  19.     public static @NonNull String inventoryToString(@NonNull Inventory inventory) {
  20.         return Base64Coder.encodeLines(inventoryToBytes(inventory));
  21.     }
  22.  
  23.     public static byte @NonNull [] inventoryToBytes(@NonNull Inventory inventory) {
  24.         return itemsToBytes(inventory.getContents());
  25.     }
  26.  
  27.     public static @NonNull String itemsToString(@Nullable ItemStack @NonNull [] items) {
  28.         return Base64Coder.encodeLines(itemsToBytes(items));
  29.     }
  30.  
  31.     public static byte @NonNull [] itemsToBytes(@Nullable ItemStack @NonNull [] items) {
  32.         try {
  33.             //noinspection UnstableApiUsage
  34.             ByteArrayDataOutput stream = ByteStreams.newDataOutput();
  35.             stream.writeInt(items.length);
  36.             for (ItemStack item : items) {
  37.                 if (item == null || item.getType().isAir()) {
  38.                     stream.writeInt(0);
  39.                 } else {
  40.                     byte[] bytes = item.serializeAsBytes();
  41.                     stream.writeInt(bytes.length);
  42.                     stream.write(bytes);
  43.                 }
  44.             }
  45.             return stream.toByteArray();
  46.         } catch (Exception e) {
  47.             throw new RuntimeException("Unable to serialize item stacks", e);
  48.         }
  49.     }
  50.  
  51.     public static @NonNull Inventory stringToInventory(@NonNull String data) {
  52.         return bytesToInventory(Base64Coder.decodeLines(data));
  53.     }
  54.  
  55.     public static @NonNull Inventory bytesToInventory(byte @NonNull [] data) {
  56.         ItemStack[] contents = bytesToItems(data);
  57.         Inventory inventory = Bukkit.createInventory(null, contents.length);
  58.         inventory.setContents(contents);
  59.         return inventory;
  60.     }
  61.  
  62.     public static @Nullable ItemStack @NonNull [] stringToItems(@NonNull String data) {
  63.         return bytesToItems(Base64Coder.decodeLines(data));
  64.     }
  65.  
  66.     public static @Nullable ItemStack @NonNull [] bytesToItems(byte @NonNull [] data) {
  67.         //noinspection UnstableApiUsage
  68.         return bytesToItems(ByteStreams.newDataInput(data));
  69.     }
  70.  
  71.     public static @Nullable ItemStack @NonNull [] bytesToItems(@NonNull DataInput stream) {
  72.         try {
  73.             ItemStack[] items = new ItemStack[stream.readInt()];
  74.             for (int i = 0; i < items.length; i++) {
  75.                 int length = stream.readInt();
  76.                 if (length == 0) continue;
  77.                 byte[] bytes = new byte[length];
  78.                 stream.readFully(bytes, 0, bytes.length);
  79.                 ItemStack stack = ItemStack.deserializeBytes(bytes);
  80.                 items[i] = stack;
  81.             }
  82.             return items;
  83.         } catch (Exception e) {
  84.             throw new RuntimeException("Unable to deserialize item stacks", e);
  85.         }
  86.     }
  87.  
  88.     public static byte[] serializeInventory(@NonNull Inventory inventory) {
  89.         try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos);) {
  90.             writeInventory(inventory, oos);
  91.             return bos.toByteArray();
  92.         } catch (IOException e) {
  93.             SneakyThrow.sneaky(e);
  94.             return null;
  95.         }
  96.     }
  97.  
  98.  
  99.     public static void writeInventory(@NonNull Inventory inventory, @NonNull ObjectOutputStream oos) throws IOException {
  100.         ItemStack[] contents = inventory.getContents();
  101.         int size = contents.length;
  102.         int sized = (int) Arrays.stream(contents).filter(Objects::nonNull).count();
  103.         oos.writeInt(sized);
  104.         for (int slot = 0; slot < size; slot++) {
  105.             ItemStack item = contents[slot];
  106.             if (item == null) {
  107.                 continue;
  108.             }
  109.             oos.writeInt(slot);
  110.             byte[] itemAsBytes = item.serializeAsBytes();
  111.             oos.writeInt(itemAsBytes.length);
  112.             oos.write(itemAsBytes);
  113.         }
  114.     }
  115.  
  116.     public static void readInventory(@NonNull Inventory inventory, @NonNull ObjectInputStream ois) throws IOException, ClassNotFoundException {
  117.         int size = ois.readInt();
  118.         for (int i = 0; i < size; i++) {
  119.             int slot = ois.readInt();
  120.             int rawSize = ois.readInt();
  121.             byte[] rawItem = new byte[rawSize];
  122.             if (ois.read(rawItem) == rawSize) {
  123.                 ItemStack itemStack = ItemStack.deserializeBytes(rawItem);
  124.                 inventory.setItem(slot, itemStack);
  125.             }
  126.         }
  127.     }
  128.  
  129.     public static void deserializeInventory(@NonNull Inventory inventory, byte[] bytes) {
  130.         try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInputStream ois = new BukkitObjectInputStream(bis);) {
  131.             readInventory(inventory, ois);
  132.         } catch (ClassNotFoundException | IOException e) {
  133.             SneakyThrow.sneaky(e);
  134.         }
  135.     }
  136. }
  137.  
Add Comment
Please, Sign In to add comment