Solved BukkitObjectOutputStream

Discussion in 'Plugin Development' started by Excel8392, Apr 6, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Excel8392

    For the plugin I am trying to create, I want to use a MongoDB database for storing player data.
    When trying to figure out how to store serialized ItemStacks, I stumbled upon this class, and it's counterpart BukkitObjectInputStream.
    Legend has it that this is able to convert my ItemStack into something I can save in config, but what is the output exactly? What does that byte[] give me, and how can I save it in a mongo DB database?
     
  2. I misunderstood the entire question

    Why not print it to console and see what it returns?
     
  3. Offline

    Excel8392

    Sorry I forgot to put that in the post - BukkitObjectOutputStream. It is in the title.
    So the byte[] output can be stored as a string?


    EDIT:
    I figured it out, for those who want to know how to use BukkitObjectOutputStream, and BukkitObjectInputStream for serializing things like itemstacks for storing, then here is an example:
    Code:
    private static String serializeItemStack(ItemStack item) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            try {
                BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
                dataOutput.writeObject(item);
                dataOutput.close();
                return Base64Coder.encodeLines(outputStream.toByteArray());
            } catch (IOException exception) {
                exception.printStackTrace();
            }
            return null;
        }
    
        private static ItemStack deserializeItemStack(String item) {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(item));
            try {
                BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
                dataInput.close();
                return (ItemStack) dataInput.readObject();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return null;
        }
    I use the Base64Coder to encode them for storing, then you can use it again to decode then deserialize the itemstacks.
     
    Last edited: Apr 7, 2020
Thread Status:
Not open for further replies.

Share This Page