下面列出了怎么用org.bukkit.util.io.BukkitObjectOutputStream的API类实例代码及写法,或者点击链接到github查看源代码。
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(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);
}
}
/**
*
* A method to serialize an {@link ItemStack} array to Base64 String.
*
* <p />
*
* Based off of {@link #toBase64(Inventory)}.
*
* @param items to turn into a Base64 String.
* @return Base64 string of the items.
* @throws IllegalStateException
*/
public static String itemStackArrayToBase64(ItemStack[] items){
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
// Write the size of the inventory
dataOutput.writeInt(items.length);
// Save every element in the list
for (int i = 0; i < items.length; i++) {
dataOutput.writeObject(items[i]);
}
// Serialize that array
dataOutput.close();
return Base64Coder.encodeLines(outputStream.toByteArray());
} catch (Exception e) {
throw new IllegalStateException("Unable to save item stacks.", e);
}
}
public static String InventoryToString(ItemStack[] items) throws IllegalStateException {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
dataOutput.writeInt(items.length);
for (ItemStack item : items) {
dataOutput.writeObject(item);
}
dataOutput.close();
return Base64Coder.encodeLines(outputStream.toByteArray());
} catch (Exception e) {
throw new IllegalStateException("Unable to save item stacks.", e);
}
}
public static byte[] encodeItemStack(ItemStack item) {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
try (BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream)) {
dataOutput.writeObject(item);
return outputStream.toByteArray();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static byte[] encodeItemStacks(ItemStack[] items) {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
try (BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream)) {
dataOutput.writeInt(items.length);
for (ItemStack item : items) {
dataOutput.writeObject(item);
}
return outputStream.toByteArray();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static byte[] encodeInventory(Inventory inventory) {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
try (BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream)) {
dataOutput.writeInt(inventory.getSize());
for (int i = 0; i < inventory.getSize(); i++) {
dataOutput.writeObject(inventory.getItem(i));
}
return outputStream.toByteArray();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Serialize an ItemStack to a JsonObject.
* <p>
* The item itself will be saved as a Base64 encoded string to
* simplify the serialization and deserialization process. The result is
* not human readable.
* </p>
*
* @param item The item to serialize.
* @param index The position in the inventory.
* @return A JsonObject with the serialized item.
*/
public JsonObject serializeItem(ItemStack item, int index) {
JsonObject values = new JsonObject();
if (item == null)
return null;
/*
* Check to see if the item is a skull with a null owner.
* This is because some people are getting skulls with null owners, which causes Spigot to throw an error
* when it tries to serialize the item. If this ever gets fixed in Spigot, this will be removed.
*/
if (item.getType() == Material.SKULL_ITEM) {
SkullMeta meta = (SkullMeta) item.getItemMeta();
if (meta.hasOwner() && (meta.getOwner() == null || meta.getOwner().isEmpty())) {
item.setItemMeta(plugin.getServer().getItemFactory().getItemMeta(Material.SKULL_ITEM));
}
}
values.addProperty("index", index);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream bos = new BukkitObjectOutputStream(outputStream)) {
bos.writeObject(item);
String encoded = Base64Coder.encodeLines(outputStream.toByteArray());
values.addProperty("item", encoded);
} catch (IOException ex) {
ConsoleLogger.severe("Unable to serialize item '" + item.getType().toString() + "':", ex);
return null;
}
return values;
}
/**
* Serialize an ItemStack to a JsonObject.
* <p>
* The item itself will be saved as a Base64 encoded string to
* simplify the serialization and deserialization process. The result is
* not human readable.
* </p>
*
* @param item The item to serialize.
* @param index The position in the inventory.
* @return A JsonObject with the serialized item.
*/
public JsonObject serializeItem(ItemStack item, int index) {
JsonObject values = new JsonObject();
if (item == null)
return null;
/*
* Check to see if the item is a skull with a null owner.
* This is because some people are getting skulls with null owners, which causes Spigot to throw an error
* when it tries to serialize the item. If this ever gets fixed in Spigot, this will be removed.
*/
if (item.getType() == Material.SKULL_ITEM) {
SkullMeta meta = (SkullMeta) item.getItemMeta();
if (meta.hasOwner() && (meta.getOwner() == null || meta.getOwner().isEmpty())) {
item.setItemMeta(plugin.getServer().getItemFactory().getItemMeta(Material.SKULL_ITEM));
}
}
values.addProperty("index", index);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream bos = new BukkitObjectOutputStream(outputStream)) {
bos.writeObject(item);
String encoded = Base64Coder.encodeLines(outputStream.toByteArray());
values.addProperty("item", encoded);
} catch (IOException ex) {
ConsoleLogger.severe("Unable to serialize item '" + item.getType().toString() + "':", ex);
return null;
}
return values;
}
public static String toBase64(Inventory inventory, int size, String target) {
try {
ByteArrayOutputStream finalOutputStream = new ByteArrayOutputStream();
ByteArrayOutputStream temporaryOutputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(temporaryOutputStream);
int failedItems = 0;
// Write the size of the inventory
dataOutput.writeInt(size);
// Save every element in the list
for (int i = 0; i < inventory.getSize(); i++) {
try {
dataOutput.writeObject(inventory.getItem(i));
} catch (Exception ignored) {
failedItems++;
temporaryOutputStream.reset();
} finally {
if (temporaryOutputStream.size() == 0) {
dataOutput.writeObject(null);
}
finalOutputStream.write(temporaryOutputStream.toByteArray());
temporaryOutputStream.reset();
}
}
if (failedItems > 0) {
PlayerVaults.getInstance().getLogger().severe("Failed to save " + failedItems + " invalid items to vault " + target);
}
PlayerVaults.debug("Serialized " + inventory.getSize() + " items");
// Serialize that array
dataOutput.close();
return Base64Coder.encodeLines(finalOutputStream.toByteArray());
} catch (Exception e) {
throw new IllegalStateException("Cannot into itemstacksz!", e);
}
}