org.bukkit.util.io.BukkitObjectOutputStream Java Examples

The following examples show how to use org.bukkit.util.io.BukkitObjectOutputStream. 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: InventorySerialization.java    From helper with MIT License 5 votes vote down vote up
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);
    }
}
 
Example #6
Source File: InventorySerialization.java    From helper with MIT License 5 votes vote down vote up
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);
    }
}
 
Example #7
Source File: InventorySerialization.java    From helper with MIT License 5 votes vote down vote up
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);
    }
}
 
Example #8
Source File: ItemSerializer.java    From PerWorldInventory with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #9
Source File: ItemSerializer.java    From PerWorldInventory with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #10
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);
    }
}