How to use BukkitInput/Output stream?

Discussion in 'Plugin Development' started by gelloe, Apr 29, 2020.

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

    gelloe

    I've been trying to figure out this code for three days now (and I haven't even been working on the rest of the plugin for three days). Can't find much information about these classes and how to bug fix them.
    Code:
        public static Inventory loadInventory(String s) {
    
            File invFile = new File(s);
            Inventory newInv = Bukkit.createInventory(null, 9);
    
            if (invFile.exists()) {
                try (BukkitObjectInputStream bois = new BukkitObjectInputStream(new FileInputStream(invFile))) {
                    while (true) {
                        newInv.addItem((ItemStack) bois.readObject());
                    }
                } catch (OptionalDataException e) {
                    e.printStackTrace();
                } catch (EOFException e) {
                    return newInv;
                } catch (ClassNotFoundException | IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
        public static void saveInventory(Inventory i, String s) {
            File invFile = new File(s);
    
            if (!invFile.exists()) {
                try {
                    invFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(invFile));
                BukkitObjectOutputStream boos = new BukkitObjectOutputStream(oos);
                for (ItemStack is : i.getContents()) {
                    if (is == null)
                        continue;
                    boos.writeObject(is);
                }
                boos.flush();
                boos.close();
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    Always returns OptionalDataException and I can't figure out why. The error message points it back to this line
    Code:
    newInv.addItem((ItemStack) bois.readObject());
    Even if I don't apply the ItemStack cast. Even if I just try printing the object to the console, I still receive the error message. Any help is appreciated, thanks in advance.
     
  2. Offline

    Niv-Mizzet

    @gelloe Could you show the code where you implement/run the methods?
     
  3. Offline

    gelloe

    Nvm, I got the code working with help from a few other libraries. Here's the code in case anyone is interested:
    Code:
        public static String saveInventory(Inventory inv, int size) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            try (final BukkitObjectOutputStream data = new BukkitObjectOutputStream(stream)) {
                data.writeInt(size);
                for (int i = 0; i < inv.getSize(); i++) {
                    data.writeObject(inv.getItem(i));
                }
                data.flush();
                data.close();
                return Base64.getEncoder().encodeToString(stream.toByteArray());
            } catch (final IOException e) {
                e.printStackTrace();
            }
            return null;
        }
       
        public static String encodeItems(ArrayList<ItemStack> items) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            try (final BukkitObjectOutputStream data = new BukkitObjectOutputStream(stream)) {
                data.writeInt(items.size());
                for (ItemStack i : items)
                    data.writeObject(i);
                data.flush();
                data.close();
                return Base64.getEncoder().encodeToString(stream.toByteArray());
            } catch (final IOException e) {
                e.printStackTrace();
            }
            return null;
        }
     
Thread Status:
Not open for further replies.

Share This Page