Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ItemSerialization {
- public static String toBase64(Inventory inventory) {
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- DataOutputStream dataOutput = new DataOutputStream(outputStream);
- NBTTagList itemList = new NBTTagList();
- // Save every element in the list
- for (int i = 0; i < inventory.getSize(); i++) {
- NBTTagCompound outputObject = new NBTTagCompound();
- CraftItemStack craft = getCraftVersion(inventory.getItem(i));
- // Convert the item stack to a NBT compound
- if (craft != null)
- CraftItemStack.asNMSCopy(craft).save(outputObject);
- itemList.add(outputObject);
- }
- // Now save the list
- NBTBase.a(itemList, dataOutput);
- // Serialize that array
- return Base64Coder.encodeLines(outputStream.toByteArray());
- }
- public static Inventory fromBase64(String data) {
- ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
- NBTTagList itemList = (NBTTagList) NBTBase.b(new DataInputStream(inputStream));
- Inventory inventory = new CraftInventoryCustom(null, itemList.size());
- for (int i = 0; i < itemList.size(); i++) {
- NBTTagCompound inputObject = (NBTTagCompound) itemList.get(i);
- if (!inputObject.isEmpty()) {
- inventory.setItem(i, CraftItemStack.asCraftMirror(
- net.minecraft.server.v1_5_R2.ItemStack.createStack(inputObject)));
- }
- }
- // Serialize that array
- return inventory;
- }
- private static CraftItemStack getCraftVersion(ItemStack stack) {
- if (stack instanceof CraftItemStack)
- return (CraftItemStack) stack;
- else if (stack != null)
- return CraftItemStack.asCraftCopy(stack);
- else
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement