Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.destroystokyo.paper.util.SneakyThrow;
- import com.google.common.io.ByteArrayDataOutput;
- import com.google.common.io.ByteStreams;
- import lombok.NonNull;
- import org.bukkit.Bukkit;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.util.io.BukkitObjectInputStream;
- import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
- import javax.annotation.Nullable;
- import java.io.*;
- import java.util.Arrays;
- import java.util.Objects;
- // Author: Dymeth.Ru
- @SuppressWarnings({"unused", "NullableProblems"})
- public class InventorySerializer {
- public static @NonNull String inventoryToString(@NonNull Inventory inventory) {
- return Base64Coder.encodeLines(inventoryToBytes(inventory));
- }
- public static byte @NonNull [] inventoryToBytes(@NonNull Inventory inventory) {
- return itemsToBytes(inventory.getContents());
- }
- public static @NonNull String itemsToString(@Nullable ItemStack @NonNull [] items) {
- return Base64Coder.encodeLines(itemsToBytes(items));
- }
- public static byte @NonNull [] itemsToBytes(@Nullable ItemStack @NonNull [] items) {
- try {
- //noinspection UnstableApiUsage
- ByteArrayDataOutput stream = ByteStreams.newDataOutput();
- stream.writeInt(items.length);
- for (ItemStack item : items) {
- if (item == null || item.getType().isAir()) {
- stream.writeInt(0);
- } else {
- byte[] bytes = item.serializeAsBytes();
- stream.writeInt(bytes.length);
- stream.write(bytes);
- }
- }
- return stream.toByteArray();
- } catch (Exception e) {
- throw new RuntimeException("Unable to serialize item stacks", e);
- }
- }
- public static @NonNull Inventory stringToInventory(@NonNull String data) {
- return bytesToInventory(Base64Coder.decodeLines(data));
- }
- public static @NonNull Inventory bytesToInventory(byte @NonNull [] data) {
- ItemStack[] contents = bytesToItems(data);
- Inventory inventory = Bukkit.createInventory(null, contents.length);
- inventory.setContents(contents);
- return inventory;
- }
- public static @Nullable ItemStack @NonNull [] stringToItems(@NonNull String data) {
- return bytesToItems(Base64Coder.decodeLines(data));
- }
- public static @Nullable ItemStack @NonNull [] bytesToItems(byte @NonNull [] data) {
- //noinspection UnstableApiUsage
- return bytesToItems(ByteStreams.newDataInput(data));
- }
- public static @Nullable ItemStack @NonNull [] bytesToItems(@NonNull DataInput stream) {
- try {
- ItemStack[] items = new ItemStack[stream.readInt()];
- for (int i = 0; i < items.length; i++) {
- int length = stream.readInt();
- if (length == 0) continue;
- byte[] bytes = new byte[length];
- stream.readFully(bytes, 0, bytes.length);
- ItemStack stack = ItemStack.deserializeBytes(bytes);
- items[i] = stack;
- }
- return items;
- } catch (Exception e) {
- throw new RuntimeException("Unable to deserialize item stacks", e);
- }
- }
- public static byte[] serializeInventory(@NonNull Inventory inventory) {
- try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos);) {
- writeInventory(inventory, oos);
- return bos.toByteArray();
- } catch (IOException e) {
- SneakyThrow.sneaky(e);
- return null;
- }
- }
- public static void writeInventory(@NonNull Inventory inventory, @NonNull ObjectOutputStream oos) throws IOException {
- ItemStack[] contents = inventory.getContents();
- int size = contents.length;
- int sized = (int) Arrays.stream(contents).filter(Objects::nonNull).count();
- oos.writeInt(sized);
- for (int slot = 0; slot < size; slot++) {
- ItemStack item = contents[slot];
- if (item == null) {
- continue;
- }
- oos.writeInt(slot);
- byte[] itemAsBytes = item.serializeAsBytes();
- oos.writeInt(itemAsBytes.length);
- oos.write(itemAsBytes);
- }
- }
- public static void readInventory(@NonNull Inventory inventory, @NonNull ObjectInputStream ois) throws IOException, ClassNotFoundException {
- int size = ois.readInt();
- for (int i = 0; i < size; i++) {
- int slot = ois.readInt();
- int rawSize = ois.readInt();
- byte[] rawItem = new byte[rawSize];
- if (ois.read(rawItem) == rawSize) {
- ItemStack itemStack = ItemStack.deserializeBytes(rawItem);
- inventory.setItem(slot, itemStack);
- }
- }
- }
- public static void deserializeInventory(@NonNull Inventory inventory, byte[] bytes) {
- try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInputStream ois = new BukkitObjectInputStream(bis);) {
- readInventory(inventory, ois);
- } catch (ClassNotFoundException | IOException e) {
- SneakyThrow.sneaky(e);
- }
- }
- }
Add Comment
Please, Sign In to add comment