Base64 serializer redone to support mob spawners & should support ALL NBT

This commit is contained in:
ProSavage
2018-07-22 21:38:35 -05:00
parent df9506ca9c
commit 6a1072b935
2 changed files with 51 additions and 10 deletions

View File

@@ -1,5 +1,7 @@
package com.massivecraft.factions.util;
import org.bukkit.Bukkit;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream;
@@ -42,4 +44,48 @@ public class InventoryUtil {
}
}
public static String toBase64(Inventory inventory) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
// Write the size of the inventory
dataOutput.writeInt(inventory.getSize());
// Save every element in the list
for (int i = 0; i < inventory.getSize(); i++) {
dataOutput.writeObject(inventory.getItem(i));
}
// Serialize that array
dataOutput.close();
return Base64Coder.encodeLines(outputStream.toByteArray());
} catch (Exception e) {
throw new IllegalStateException("Cannot into itemstacksz!", e);
}
}
public static String toBase64(ItemStack[] is, int size) {
Inventory inventory = Bukkit.createInventory(null, size);
inventory.setContents(is);
return toBase64(inventory);
}
public static Inventory fromBase64(String data) {
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
Inventory inventory = Bukkit.getServer().createInventory(null, dataInput.readInt());
// Read the serialized inventory
for (int i = 0; i < inventory.getSize(); i++) {
inventory.setItem(i, (ItemStack) dataInput.readObject());
}
dataInput.close();
return inventory;
} catch (Exception e) {
}
return null;
}
}