Saving custom inventory

Discussion in 'Plugin Development' started by slater96, Feb 1, 2013.

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

    slater96

    I'm using this serializable class I made to save the itemstacks.
    Code:
    private int typeId;
        private int amount;
        private short durability;
        private byte data;
        private String itemName;
        private List<String> itemLore;
        private HashMap<Enchantment, Integer> enchantments = new HashMap<Enchantment, Integer>();
       
        public SerializableInventory(ItemStack i) {
            typeId = i.getTypeId();
            amount = i.getAmount();
            durability = i.getDurability();
            data = i.getData().getData();
            itemName = i.getItemMeta().getDisplayName();
            itemLore = i.getItemMeta().getLore();
            for (Entry<Enchantment, Integer> enchantment : i.getEnchantments().entrySet()) {
                enchantments.put(enchantment.getKey(), enchantment.getValue());
            }
        }
       
        public ItemStack restoreInventory() {
            ItemStack i = new ItemStack(typeId, amount, durability);
            i.getData().setData(data);
            for (Entry<Enchantment, Integer> enchantment : enchantments.entrySet()) {
                i.addEnchantment(enchantment.getKey(), enchantment.getValue());
            }
            ItemMeta itemMeta = i.getItemMeta();
            itemMeta.setDisplayName(itemName);
            itemMeta.setLore(itemLore);
            return i;
        }
    And then I'm saving it with
    Code:
    @EventHandler
        public void inventoryClose(InventoryCloseEvent event) {
            Inventory inv = event.getInventory();
            Player p = (Player) event.getPlayer();
            if (this.inInventory.contains(p.getName())) {
                for (ItemStack contents : inv.getContents()) {
                    this.blockStorage.put(p.getName(), new SerializableInventory(contents));
                }
                this.inInventory.remove(p.getName());
            }
        }
    But i get this error
    Code:
    20:14:00 [SEVERE] Could not pass event InventoryCloseEvent to BlockStorage v0.1
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at net.minecraft.server.v1_4_R1.PlayerConnection.handleContainerClose(PlayerConnection.java:1163)
    at net.minecraft.server.v1_4_R1.Packet101CloseWindow.handle(SourceFile:17)
    at net.minecraft.server.v1_4_R1.NetworkManager.b(NetworkManager.java:290)
    at net.minecraft.server.v1_4_R1.PlayerConnection.d(PlayerConnection.java:113)
    at net.minecraft.server.v1_4_R1.ServerConnection.b(SourceFile:39)
    at net.minecraft.server.v1_4_R1.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_4_R1.MinecraftServer.r(MinecraftServer.java:598)
    at net.minecraft.server.v1_4_R1.DedicatedServer.r(DedicatedServer.java:224)
    at net.minecraft.server.v1_4_R1.MinecraftServer.q(MinecraftServer.java:494)
    at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java:427)
    at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.NullPointerException
    at me.slater96.BlockStorage.SerializableInventory.<init>(SerializableInventory.java:25)
    at me.slater96.BlockStorage.BlockListener.inventoryClose(BlockListener.java:94)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    ... 14 more
    Line 25 of serializable class is
    Code:
    typeId = i.getTypeId();
    Not sure what I'm doing wrong, anyone know how to fix this please?
     
  2. Maybe one of the ItemStacks is null? ( free inventory space )
     
  3. Offline

    slater96

    Ah yeah, it works fine when all the slots have an item in. Do you know how to fix this?
     
  4. add an if(i==null) {} check...
     
    slater96 likes this.
  5. Offline

    nisovin

    You check for null.
     
    slater96 likes this.
  6. Offline

    slater96

    Thanks that fixed it. How would I set the inventory contents using my restore method as setContents is ItemStack[] not just ItemStack.
     
  7. Cycle through your saved values and add the to the inventory...
     
  8. Offline

    slater96

    I'm not too sure how to do this, could you help me please. I had a go at doing
    Code:
    for (Entry<String, SerializableInventory> items : this.blockStorage.entrySet()) {
                                        items.getValue().restoreInventory();
                                    }
    But that didn't work.
     
  9. Offline

    vildaberper

    Code:
    private ItemStack[] getItemStacks(String path, int size){
        YamlConfiguration c = getConfig();
        ItemStack[] is = new ItemStack[size];
     
        if(c.getConfigurationSection(path) == null)
            return null;
        for(String k : c.getConfigurationSection(path).getKeys(false))
            is[Integer.parseInt(k)] = c.getItemStack(path + "." + k, null);
        return is;
    }
     
    private void setItemStacks(String path, ItemStack[] is){
        YamlConfiguration c = getConfig();
     
        if(is == null)
            c.set(path, null);
        else
            for(int i = 0; i < is.length; i++)
                c.set(path + "." + i, is[i]);
        try{
            c.save(getConfigFile());
        }catch(Exception e){ }
    }
    That's how I do it in DefaultCommands (saving it in an .yml). You'll have to figure out how to modify the code. ;)
     
Thread Status:
Not open for further replies.

Share This Page