47 lines
1.8 KiB
Plaintext
47 lines
1.8 KiB
Plaintext
package Sdf1_login;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.bukkit.util.io.BukkitObjectInputStream;
|
|
import org.bukkit.util.io.BukkitObjectOutputStream;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.util.Base64;
|
|
|
|
public class InventorySerializer {
|
|
|
|
public static String toBase64(ItemStack[] items) throws IOException {
|
|
try {
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
|
|
dataOutput.writeInt(items.length);
|
|
for (ItemStack item : items) {
|
|
dataOutput.writeObject(item);
|
|
}
|
|
dataOutput.close();
|
|
return Base64.getEncoder().encodeToString(outputStream.toByteArray());
|
|
} catch (Exception e) {
|
|
throw new IOException("Unable to serialize item stacks.", e);
|
|
}
|
|
}
|
|
|
|
public static ItemStack[] fromBase64(String data) throws IOException {
|
|
if (data == null || data.isEmpty()) {
|
|
return new ItemStack[0];
|
|
}
|
|
try {
|
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64.getDecoder().decode(data));
|
|
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
|
|
ItemStack[] items = new ItemStack[dataInput.readInt()];
|
|
for (int i = 0; i < items.length; i++) {
|
|
items[i] = (ItemStack) dataInput.readObject();
|
|
}
|
|
dataInput.close();
|
|
return items;
|
|
} catch (ClassNotFoundException e) {
|
|
throw new IOException("Unable to deserialize item stacks.", e);
|
|
}
|
|
}
|
|
}
|