Added VirtualInventory to byte[] serialization

Close #4
This commit is contained in:
NichtStudioCode 2021-09-16 21:33:10 +02:00
parent 77ee58439d
commit b8ccc1a6ca
2 changed files with 16 additions and 1 deletions

@ -42,7 +42,7 @@ public class VirtualInventory implements ConfigurationSerializable {
* @param stackSizes An array of maximum allowed stack sizes for each slot in the {@link VirtualInventory}. * @param stackSizes An array of maximum allowed stack sizes for each slot in the {@link VirtualInventory}.
*/ */
public VirtualInventory(@Nullable UUID uuid, int size, @Nullable ItemStack[] items, int[] stackSizes) { public VirtualInventory(@Nullable UUID uuid, int size, @Nullable ItemStack[] items, int[] stackSizes) {
this.uuid = uuid; this.uuid = uuid == null ? new UUID(0L, 0L) : uuid;
this.size = size; this.size = size;
this.items = items == null ? new ItemStack[size] : items; this.items = items == null ? new ItemStack[size] : items;
if (stackSizes == null) { if (stackSizes == null) {
@ -99,6 +99,10 @@ public class VirtualInventory implements ConfigurationSerializable {
return result; return result;
} }
public byte[] toByteArray() {
return VirtualInventoryManager.getInstance().serializeInventory(this);
}
/** /**
* Gets a set of {@link Window}s that display this {@link VirtualInventory}. * Gets a set of {@link Window}s that display this {@link VirtualInventory}.
* *
@ -189,6 +193,7 @@ public class VirtualInventory implements ConfigurationSerializable {
* *
* @return The {@link UUID} * @return The {@link UUID}
*/ */
@NotNull
public UUID getUuid() { public UUID getUuid() {
return uuid; return uuid;
} }

@ -120,6 +120,12 @@ public class VirtualInventoryManager {
return new File(SAVE_DIR, virtualInventory.getUuid() + ".vi2"); return new File(SAVE_DIR, virtualInventory.getUuid() + ".vi2");
} }
public byte[] serializeInventory(VirtualInventory vi) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializeInventory(vi, out);
return out.toByteArray();
}
public void serializeInventory(VirtualInventory vi, OutputStream out) { public void serializeInventory(VirtualInventory vi, OutputStream out) {
try { try {
DataOutputStream dos = new DataOutputStream(out); DataOutputStream dos = new DataOutputStream(out);
@ -146,6 +152,10 @@ public class VirtualInventoryManager {
} }
public VirtualInventory deserializeInventory(byte[] bytes) {
return deserializeInventory(new ByteArrayInputStream(bytes));
}
public VirtualInventory deserializeInventory(InputStream in) { public VirtualInventory deserializeInventory(InputStream in) {
try { try {
DataInputStream din = new DataInputStream(in); DataInputStream din = new DataInputStream(in);