saveInventory (InventoryCloseEvent)

Discussion in 'Spigot Plugin Development' started by iNoXs, Jun 18, 2016.

  1. Hey i want to save an inventory (in a gui) into a config this is my code

    Code (Text):
        public void saveInventory(InventoryCloseEvent e) {
            if (e.getInventory().getName().equalsIgnoreCase("§cSac à dos")) {
                 final ArrayList<ItemStack> invCont = new ArrayList<>();

                 for(ItemStack stack : inv.getContents()){
                     if(stack != null) invCont.add(stack);

                 SettingsManager.getInstance().getConfig().set(e.getPlayer().getName()+" bag.", invCont);
    But doesn't work :/
  2. For one thing, it's bad practice to save by a player's name. You should be using UUID (player.getUniqueId().toString())

    Also I've never seen someone save something to the config with a space in the key name. I think that's valid YML but it might be causing a problem.

    Also you put a dot after "bag" for some reason.

    Try this
    Code (Text):

    SettingsManager.getInstance().getConfig().set(e.getPlayer().getUniqueId.toString()+".bag", invCont);
    Then to get it from config, btw, you will need to do Config.getList(player.getUniqueId().toString() + ".bag") and then cast it to a list of itemstacks.
    • Agree Agree x 1
  3. Works but my config looks like this :
    Code (Text):
    iNoXs_Dev bag: {}
      bag: []
    itemStack are not detected
  4. Try removing ".getContents()" in the for loop. Inventories are iterable, so it's unnecessary extra code. And making the List final is pointless as well, there's no reason that it would need to be final.

    Why are you looping through an inventory "inv" that is never created? Did you cut out any code before posting it?
  5. yes i cut some code before ^^

    Code (Text):
        Inventory inv;
        public GuiInventory(){
            this.inv = Bukkit.createInventory(null, 27, "§cSac à dos");
    and i try to remove .getContents() and Final but same problem :/
  6. Why don't you just directly store the contents array?
  7. You could try instead of saving a list saving it like
    Code (YAML):
    : <item>
    : <Item>
    by doing a traditional for loop (like, int i = 0; I < inv.getSize()...) and doing config.set(player.getUniqueId().toString() + ".bag." + i, stack)

    For one thing this would make bags slightly nicer for players since their items would stay in the same place (I assume you're making a sort of backpack plugin), and this might solve your issue. Btw I'm not sure why or in what circumstances you are making this, but a config is probably not a particularly great solution for saving backpacks. MySQL would be better.
  8. If you don't wanna use a database, you could create a file for each player since it looks like OP is gonna be saving more than just a bag.
    • Agree Agree x 1
  9. Yep i know for MySQL but i need to learn and buy a database maybe later ^^ i do this plugin only for me
  10. Ah alright, was just checking.