Inventory to config saves as null

Discussion in 'Spigot Plugin Development' started by TheSupreme, May 18, 2018 at 12:58 AM.

  1. Code (Text):
        public Inventory getInventory(UUID Id) {
            this.reloadConfig();
            Inventory x = Bukkit.createInventory(null, 9, ChatColor.DARK_GRAY + "Artifacts");
            x.setContents(getItems(Id));
            return x;
        }
        public void setInventory(UUID Id, Inventory i) {
            PlayerObject o = getPlayerObject(Id);

            ItemStack[] invcontent = i.getContents();
            o.getUserFile().set("artifacts", invcontent);
            o.saveUserFile();
        }

        public ItemStack[] getItems(UUID Id) {

            PlayerObject o = getPlayerObject(Id);

            @SuppressWarnings("unchecked")
            ArrayList<ItemStack> content = (ArrayList<ItemStack>) o.getUserFile().getList("artifacts");

            ItemStack[] items;
            if (content != null) {
                items = new ItemStack[content.size()];
                for (int i = 0; i < content.size(); i++) {
                    ItemStack item = content.get(i);
                    if (item != null) {
                        items[i] = item;
                    } else {
                        items[i] = null;
                    }
                }
            } else
                items = new ItemStack[0];
            return items;
        }
    Set inventory code
    Code (Text):
    @EventHandler
        void OnInventoryClose(InventoryCloseEvent e) {
            if (e.getPlayer() instanceof Player) {
                Player p = (Player) e.getPlayer();

                if (e.getInventory().getTitle().contains("Artifacts")) {
                    Main.plugin().setInventory(p.getUniqueId(), e.getInventory());
                    Main.plugin().SetStats(p, true);
                }
            }
        }
    Code (Text):
    artifacts:
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
     

    This code has been working for me to save and load an inventory for some time now. Suddenly however, everything in the config file is saved as "null". I've done a lot of debugging and I'm quite clueless at where the problem is at. Any help is appreciated!
     
  2. What's inside the inventory when closing it?
    What's inside the array you try to store in the config (toString it's contents)?
     
  3. Random items. nothing fancy.
     
  4. You are storing the inventory and not the contents, you need to do #getContents()

    [​IMG]

    [​IMG]
     

Share This Page