Saving an ItemStack Array

Discussion in 'Plugin Development' started by TheNewTao, Dec 14, 2015.

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

    TheNewTao

    Hi,

    I am not the most experienced person when it comes to the configuration API. I have a HashMap with a ItemStack as a key and an ItemStack array as the value. I am essentially creating a plugin that when you right click a certain item, it adds to the player's inventory a certain array of items, but I want to keep the hashmap even when the sever is reloaded. Ok so the question is: what is the best way of approaching this problem. I was reading the configuration API and it says that I can only use a string as a key and a list (which is what I was thinking of using) for the value. I don't know if this would be the most efficient way of doing it or if it would even work since I have an ItemStack as my key.

    Then, after doing some googling I found that some people suggested serializing everything into a string with this class:

    Code:
    package es.deantonious.armedblocks.util;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import org.bukkit.Bukkit;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.util.io.BukkitObjectInputStream;
    import org.bukkit.util.io.BukkitObjectOutputStream;
    import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
    public class InventoryToBase64 {
        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("Unable to save item stacks.", e);
            }  
        }
    
        public static Inventory fromBase64(String data) throws IOException {
            try {
                ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
                BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
                Inventory inventory = Bukkit.getServer().createInventory(null, dataInput.readInt());
    
                // Read the serialized inventory
                for (int i = 0; i < inventory.getSize(); i++) {
                    inventory.setItem(i, (ItemStack) dataInput.readObject());
                }
                dataInput.close();
                return inventory;
            } catch (ClassNotFoundException e) {
                throw new IOException("Unable to decode class type.", e);
            }
        }
    }
    What do you guys use to save inventories to a HashMap and then store the HashMap?

    Thanks.
     
  2. Offline

    mcdorli

    You could use Chloe chans saveable hashmap library. Then you just need to store the itemstacks somehow, like in a String.
     
  3. Offline

    teej107

    @TheNewTao keep in mind that the point of a configuration file is so it can be easily configurable. Serializing objects like that does not make them configurable. ItemStack already implements Bukkit's ConfigurationSerializable interface which makes it easy to serialize and deserialize objects (and hopefully keep them easily configurable)
     
  4. Offline

    TheNewTao

    @teej107

    To be honest, I do not need to configure anything in that HashMap, due to that everything is done in-game. I just need to store it.
     
Thread Status:
Not open for further replies.

Share This Page