Java Code Examples for org.bukkit.util.io.BukkitObjectOutputStream#close()

The following examples show how to use org.bukkit.util.io.BukkitObjectOutputStream#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InventoryTypeAdapter.java    From IridiumSkyblock with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example 2
Source File: Base64Serialize.java    From ServerTutorial with MIT License 6 votes vote down vote up
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);
    }
}
 
Example 3
Source File: Base64Serialize.java    From ServerTutorial with MIT License 6 votes vote down vote up
/**
 *
 * 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);
    }
}
 
Example 4
Source File: InventoryTypeAdapter.java    From IridiumSkyblock with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example 5
Source File: Base64Serialization.java    From PlayerVaults with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}