Base64 serializer redone to support mob spawners & should support ALL NBT
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user