commit
9b211c12ce
@ -1,107 +1,51 @@
|
|||||||
package com.massivecraft.factions.util;
|
package com.massivecraft.factions.zcore.util;
|
||||||
|
|
||||||
import com.massivecraft.factions.zcore.nbtapi.NBTItem;
|
|
||||||
import com.massivecraft.factions.zcore.nbtapi.NBTType;
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.enchantments.Enchantment;
|
import org.bukkit.enchantments.Enchantment;
|
||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.bukkit.util.io.BukkitObjectInputStream;
|
||||||
|
import org.bukkit.util.io.BukkitObjectOutputStream;
|
||||||
|
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class InventoryUtil {
|
public class InventoryUtil {
|
||||||
|
|
||||||
|
|
||||||
public static String InventoryToString(Inventory invInventory) {
|
public static String InventoryToString(ItemStack[] items) throws IllegalStateException {
|
||||||
// String serialization = invInventory.getSize() + ";";
|
try {
|
||||||
StringBuilder serialization = new StringBuilder(invInventory.getSize() + ";");
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
for (int i = 0; i < invInventory.getSize(); i++) {
|
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
|
||||||
ItemStack is = invInventory.getItem(i);
|
dataOutput.writeInt(items.length);
|
||||||
if (is != null) {
|
for (int i = 0; i < items.length; i++) {
|
||||||
// String serializedItemStack = new String();
|
dataOutput.writeObject(items[i]);
|
||||||
StringBuilder serializedItemStack = new StringBuilder();
|
|
||||||
|
|
||||||
String isType = String.valueOf(is.getType().getId());
|
|
||||||
// serializedItemStack += "t@" + isType;
|
|
||||||
serializedItemStack.append("t@" + isType);
|
|
||||||
|
|
||||||
if (is.getDurability() != 0) {
|
|
||||||
String isDurability = String.valueOf(is.getDurability());
|
|
||||||
//serializedItemStack += ":d@" + isDurability;
|
|
||||||
serializedItemStack.append(":d@" + isDurability);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is.getAmount() != 1) {
|
|
||||||
String isAmount = String.valueOf(is.getAmount());
|
|
||||||
//serializedItemStack += ":a@" + isAmount;
|
|
||||||
serializedItemStack.append(":a@" + isAmount);
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<Enchantment, Integer> isEnch = is.getEnchantments();
|
|
||||||
if (isEnch.size() > 0) {
|
|
||||||
for (Map.Entry<Enchantment, Integer> ench : isEnch.entrySet()) {
|
|
||||||
// serializedItemStack += ":e@" + ench.getKey().getId() + "@" + ench.getValue();
|
|
||||||
serializedItemStack.append(":e@" + ench.getKey().getId() + "@" + ench.getValue());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
NBTItem nbtItem = new NBTItem(is);
|
|
||||||
Set<String> nbtKeySet = nbtItem.getKeys();
|
|
||||||
|
|
||||||
for (String nbtKey : nbtKeySet) {
|
|
||||||
serializedItemStack.append(":n@" + nbtKey + "@" + nbtItem.getType(nbtKey).getId() + "@" + )
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
// serialization += i + "#" + serializedItemStack + ";";
|
|
||||||
serialization.append(i + "#" + serializedItemStack + ";");
|
|
||||||
}
|
}
|
||||||
|
dataOutput.close();
|
||||||
|
return Base64Coder.encodeLines(outputStream.toByteArray());
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IllegalStateException("Unable to save item stacks.", e);
|
||||||
}
|
}
|
||||||
return serialization.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Inventory StringToInventory(String invString) {
|
public static ItemStack[] StringToInventory(String data) throws IOException {
|
||||||
String[] serializedBlocks = invString.split(";");
|
try {
|
||||||
String invInfo = serializedBlocks[0];
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
|
||||||
Inventory deserializedInventory = Bukkit.getServer().createInventory(null, Integer.valueOf(invInfo));
|
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
|
||||||
|
ItemStack[] items = new ItemStack[dataInput.readInt()];
|
||||||
for (int i = 1; i < serializedBlocks.length; i++) {
|
for (int i = 0; i < items.length; i++) {
|
||||||
String[] serializedBlock = serializedBlocks[i].split("#");
|
items[i] = (ItemStack) dataInput.readObject();
|
||||||
int stackPosition = Integer.valueOf(serializedBlock[0]);
|
|
||||||
|
|
||||||
if (stackPosition >= deserializedInventory.getSize()) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
dataInput.close();
|
||||||
ItemStack is = null;
|
return items;
|
||||||
Boolean createdItemStack = false;
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new IOException("Unable to decode class type.", e);
|
||||||
String[] serializedItemStack = serializedBlock[1].split(":");
|
|
||||||
for (String itemInfo : serializedItemStack) {
|
|
||||||
String[] itemAttribute = itemInfo.split("@");
|
|
||||||
if (itemAttribute[0].equals("t")) {
|
|
||||||
is = new ItemStack(Material.getMaterial(Integer.valueOf(itemAttribute[1])));
|
|
||||||
createdItemStack = true;
|
|
||||||
} else if (itemAttribute[0].equals("d") && createdItemStack) {
|
|
||||||
is.setDurability(Short.valueOf(itemAttribute[1]));
|
|
||||||
} else if (itemAttribute[0].equals("a") && createdItemStack) {
|
|
||||||
is.setAmount(Integer.valueOf(itemAttribute[1]));
|
|
||||||
} else if (itemAttribute[0].equals("e") && createdItemStack) {
|
|
||||||
is.addEnchantment(Enchantment.getById(Integer.valueOf(itemAttribute[1])), Integer.valueOf(itemAttribute[2]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
deserializedInventory.setItem(stackPosition, is);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return deserializedInventory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -261,22 +261,20 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
|
|||||||
return inventory;
|
return inventory;
|
||||||
} else {
|
} else {
|
||||||
//long startTime = System.nanoTime();
|
//long startTime = System.nanoTime();
|
||||||
Inventory contents = StringToInventory(chestSerialized);
|
ItemStack[] contents = new ItemStack[0];
|
||||||
inventory.setContents(contents.getContents());
|
try {
|
||||||
// long endTime = System.nanoTime();
|
contents = StringToInventory(chestSerialized);
|
||||||
// long duration = (endTime - startTime);
|
} catch (IOException e) {
|
||||||
// Bukkit.broadcastMessage("chest deserialization time: " + duration + " nano seconds");
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
inventory.setContents(contents);
|
||||||
return inventory;
|
return inventory;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setChest(Inventory inventory) {
|
public void setChest(Inventory inventory) {
|
||||||
// long startTime = System.nanoTime();
|
chestSerialized = InventoryToString(inventory.getContents());
|
||||||
chestSerialized = InventoryToString(inventory);
|
|
||||||
// long endTime = System.nanoTime();
|
|
||||||
// long duration = (endTime - startTime);
|
|
||||||
// Bukkit.broadcastMessage("chest serialization time: " + duration + " nano seconds");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user