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; package com.massivecraft.factions.util;
import org.bukkit.Bukkit;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.util.io.BukkitObjectInputStream; import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream; 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;
}
} }

View File

@ -11,6 +11,7 @@ import com.massivecraft.factions.struct.BanInfo;
import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.struct.Role; import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.util.InventoryUtil;
import com.massivecraft.factions.util.LazyLocation; import com.massivecraft.factions.util.LazyLocation;
import com.massivecraft.factions.util.MiscUtil; import com.massivecraft.factions.util.MiscUtil;
import com.massivecraft.factions.util.RelationUtil; import com.massivecraft.factions.util.RelationUtil;
@ -26,15 +27,11 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; 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 abstract class MemoryFaction implements Faction, EconomyParticipator {
public HashMap<Integer, String> rules = new HashMap<Integer, String>(); public HashMap<Integer, String> rules = new HashMap<Integer, String>();
public int tnt; public int tnt;
@ -359,11 +356,9 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
} else { } else {
//long startTime = System.nanoTime(); //long startTime = System.nanoTime();
ItemStack[] contents = new ItemStack[0]; ItemStack[] contents = new ItemStack[0];
try {
contents = StringToInventory(chestSerialized); contents = InventoryUtil.fromBase64(chestSerialized).getContents();
} catch (IOException e) {
e.printStackTrace();
}
inventory.setContents(contents); inventory.setContents(contents);
return inventory; return inventory;
} }
@ -371,7 +366,7 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
@Override @Override
public void setChest(Inventory inventory) { public void setChest(Inventory inventory) {
chestSerialized = InventoryToString(inventory.getContents()); chestSerialized = InventoryUtil.toBase64(inventory);
} }
@Override @Override