Solved ItemStack is null?

Discussion in 'Plugin Development' started by Banjer_HD, Aug 1, 2016.

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

    Banjer_HD

    Helo,
    I am working on a plugin that needs to save and load inventories from the config...
    On the loading of an inventory all the itemstacks are giving null...

    Loading an inventory:
    Code:
       ArrayList<ItemStack> list = new ArrayList<ItemStack>();
                                 
       for(String key : plugin.customConfig.getConfigurationSection("inventories." + p.getName()).getKeys(false)) {
              list.add(plugin.customConfig.getItemStack("inventories." + p.getName() + key));
         }
                                 
       for(ItemStack is : list) {
            Bukkit.broadcastMessage(is + "");
         }
    
    Saving an inventory:
    Code:
            Inventory i = e.getPlayer().getInventory();
    
            int w = 0;
            for(ItemStack is : i.getContents()) {
                if(is != null) {
                    plugin.customConfig.set("inventories." + e.getPlayer().getName() + "." + w, is);
                    plugin.saveCustomYml(plugin.customConfig, plugin.customYml);
                    w++;
                }
            }
    
    Thank you for reading this thread!:)
     
  2. Offline

    bwfcwalshy Retired Staff

    @Banjer_HD Path in the first piece of code: inventories.bwfcwalshy1
    Path in the second piece of code: inventories.bwfcwalshy.1

    Spot the difference!
     
    Zombie_Striker likes this.
  3. Offline

    Banjer_HD

    Oh, THANKS!!! Wow i changed it on the last piece becouse i want it to work with more people... forgot the first piece O_O

    that's harder than I thought...
    How can you know the slot where the item is in?

    Config:
    Code:
    inventories:
      Banjer_HD:
        '0':
          ==: org.bukkit.inventory.ItemStack
          type: POWERED_RAIL
          amount: 64
        '1':
          ==: org.bukkit.inventory.ItemStack
          type: CACTUS
          amount: 3
        '2':
          ==: org.bukkit.inventory.ItemStack
          type: ANVIL
        '12':
          ==: org.bukkit.inventory.ItemStack
          type: DIAMOND_SWORD
          damage: 12
          meta:
            ==: ItemMeta
            meta-type: UNSPECIFIC
            display-name: My sword
            enchants:
              DAMAGE_ALL: 4
            repair-cost: 1
    
    EDIT by Banjer_HD: Removed: "EDIT by Moderator: merged posts, please use the edit button instead of double posting."" xD <3
     
    Last edited: Aug 1, 2016
  4. Offline

    Lordloss

    Just read the items out of the config in the same order as you put it in. The only problem is that you ignore spaces in the inventory, so they will not be in the same spots. What you can do is increment the item number also if the slot is empty.
     
  5. Offline

    Banjer_HD

    SOLVED
     
Thread Status:
Not open for further replies.

Share This Page