From 6a1072b9354df6104929991b35b2e9d11c700315 Mon Sep 17 00:00:00 2001 From: ProSavage Date: Sun, 22 Jul 2018 21:38:35 -0500 Subject: [PATCH] Base64 serializer redone to support mob spawners & should support ALL NBT --- .../factions/util/InventoryUtil.java | 46 +++++++++++++++++++ .../factions/zcore/persist/MemoryFaction.java | 15 ++---- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/util/InventoryUtil.java b/src/main/java/com/massivecraft/factions/util/InventoryUtil.java index c9904c4f..98f31df6 100644 --- a/src/main/java/com/massivecraft/factions/util/InventoryUtil.java +++ b/src/main/java/com/massivecraft/factions/util/InventoryUtil.java @@ -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; + } + } diff --git a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java index c7917340..55a53075 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java @@ -11,6 +11,7 @@ import com.massivecraft.factions.struct.BanInfo; import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.struct.Role; +import com.massivecraft.factions.util.InventoryUtil; import com.massivecraft.factions.util.LazyLocation; import com.massivecraft.factions.util.MiscUtil; import com.massivecraft.factions.util.RelationUtil; @@ -26,15 +27,11 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; -import java.io.IOException; import java.util.*; import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; -import static com.massivecraft.factions.util.InventoryUtil.InventoryToString; -import static com.massivecraft.factions.util.InventoryUtil.StringToInventory; - public abstract class MemoryFaction implements Faction, EconomyParticipator { public HashMap rules = new HashMap(); public int tnt; @@ -359,11 +356,9 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator { } else { //long startTime = System.nanoTime(); ItemStack[] contents = new ItemStack[0]; - try { - contents = StringToInventory(chestSerialized); - } catch (IOException e) { - e.printStackTrace(); - } + + contents = InventoryUtil.fromBase64(chestSerialized).getContents(); + inventory.setContents(contents); return inventory; } @@ -371,7 +366,7 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator { @Override public void setChest(Inventory inventory) { - chestSerialized = InventoryToString(inventory.getContents()); + chestSerialized = InventoryUtil.toBase64(inventory); } @Override