Merge pull request #16 from WhiteFoxHD/1.6.x

F Chest fix
This commit is contained in:
Naman 2018-07-10 13:04:54 -05:00 committed by GitHub
commit 9b211c12ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 96 deletions

View File

@ -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);
} }
dataOutput.close();
if (is.getAmount() != 1) { return Base64Coder.encodeLines(outputStream.toByteArray());
String isAmount = String.valueOf(is.getAmount()); } catch (Exception e) {
//serializedItemStack += ":a@" + isAmount; throw new IllegalStateException("Unable to save item stacks.", e);
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());
} }
} }
public static ItemStack[] StringToInventory(String data) throws IOException {
try {
/* ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
NBTItem nbtItem = new NBTItem(is); BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
Set<String> nbtKeySet = nbtItem.getKeys(); ItemStack[] items = new ItemStack[dataInput.readInt()];
for (int i = 0; i < items.length; i++) {
for (String nbtKey : nbtKeySet) { items[i] = (ItemStack) dataInput.readObject();
serializedItemStack.append(":n@" + nbtKey + "@" + nbtItem.getType(nbtKey).getId() + "@" + )
} }
*/ dataInput.close();
// serialization += i + "#" + serializedItemStack + ";"; return items;
serialization.append(i + "#" + serializedItemStack + ";"); } catch (ClassNotFoundException e) {
throw new IOException("Unable to decode class type.", e);
} }
} }
return serialization.toString();
}
public static Inventory StringToInventory(String invString) {
String[] serializedBlocks = invString.split(";");
String invInfo = serializedBlocks[0];
Inventory deserializedInventory = Bukkit.getServer().createInventory(null, Integer.valueOf(invInfo));
for (int i = 1; i < serializedBlocks.length; i++) {
String[] serializedBlock = serializedBlocks[i].split("#");
int stackPosition = Integer.valueOf(serializedBlock[0]);
if (stackPosition >= deserializedInventory.getSize()) {
continue;
}
ItemStack is = null;
Boolean createdItemStack = false;
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;
}
} }

View File

@ -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");
} }