How to save inventory contents ?

Discussion in 'Spigot Plugin Development' started by MadMaxCookie, May 12, 2016.

  1. Hello so I'm wondering how to save the inventory contents from slot 9 to slot 44

    Code (Java):
    Inventory inv
    Code (Java):
     // how to do it ?
    getConfig().set("path", inv.getContents());
     
  2. MiniDigger

    Supporter

    make a list where you put all itemstacks from these slot in and save that list to your config
     
  3. how do I check the slots ?
     
  4. MiniDigger

    Supporter

    Inventory#getContents is an array. entry 0 is slot 0 an so on.
     
  5. could you add example ? from slot 9 to slot 44 ?
     
  6. @MiniDigger

    is this gonna work ?
    Code (Java):

                for(int i = 9; i<= 44; i++) {
                    plugin.getConfig().set(p.getName() + ".household", inv2.getContents());
                    plugin.getConfig().set(plugin.getConfig().getString(p.getName() + ".Married.who") + ".household", inv2.getContents());
                }
     
  7. If you want to save it exeactly and don't wanna to edit it than you can use the NBTTAGCOMPOUND. To do that you need the nms inventory and run the Methode a (nbttagcomound) and all data of the inventory will be stored in the NBTTAGCOMPOUND.
     
  8. but I want the method getting the certain slots contents and save it to config do you know something how ?
     
  9. MiniDigger

    Supporter

    don't save the whole array, only entry i.
    also: you do you want to save the inventory twice?
     
  10. :p My Bad

    how about this ?
    Code (Java):
    ItemStack test;
    for(int i = 9; i <=44 i++) {
    test = e.getInventory(i);
    }

    //save test to config ?
     
    Edit: Sorry wrong :D
     
  11. MiniDigger

    Supporter

    make a list with itemstacks
    for every number i between 9 and 44
    get the item in slot i
    add that item into the list
    endfor
    save the list to the config
    save the config
     
  12. like this ?
    Code (Java):
                List<ItemStack> items = null;
                for(int i = 9; i <= 44; i++) {
                    ItemStack all = e.getInventory().getItem(i);
                    items.add(all);
                }
                plugin.getConfig().set(p.getName() + ".Married.household", items);
                plugin.saveConfig();
    or this way

    Code (Java):
                List<ItemStack> items = null;
                for(int i = 9; i <= 44; i++) {
                    items.add(e.getInventory(i));
                }
                plugin.getConfig().set(p.getName() + ".Married.household", items);
                plugin.saveConfig();
     
  13. MiniDigger

    Supporter

    the first one. be sure to initialise your list tho or you will get a npe
     
  14. how to do that ?
     
  15. yeah did this
    Code (Java):

                List<ItemStack> items = new ArrayList<ItemStack>();
                for(int i = 9; i <= 44; i++) {
                    ItemStack all = e.getInventory().getItem(i);
                    items.add(all);
                }
                plugin.getConfig().set(p.getName() + ".Married.household", items);
                plugin.saveConfig();
    but the config give me null when there is no item or material . air

    Code (Java):

        household:
        - ==: org.bukkit.inventory.ItemStack
          type: STONE
          damage: 1
          amount: 64
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
    is it fine or an error ?
     
  16. MiniDigger

    Supporter

    it is fine. you just need to be careful when loading (I am not sure if bukkit handles null correctly)
     
  17. oh let me see how about replace the null to ItemStack Air ?
     
  18. MiniDigger

    Supporter

    yes
     
  19. hello so I have this
    Code (Java):

            ItemStack[] content = ((List<ItemStack>) plugin.getConfig().get(p.getName() + ".household")).toArray(new ItemStack[0]);
     
    now I don't know how to load it on slots 9 to slots 44