ItemStack List in file resets each time custom inventory is closed in game

Discussion in 'Spigot Plugin Development' started by DownThePark, Aug 14, 2018.

  1. Hello, developers.

    Problem: I am having an issue with my code where, after I save a custom inventory to a file, all the contents (ItemStacks) are present in the file, but after I reopen the chest, everything disappears not only in inventory in-game, but also inside the file.

    Expected Result:
    I should be able to open and close the custom inventory using /chest with the ItemStacks saving to the file and not disappearing the next time I use /chest

    Here is my code:
    Code (Text):

    package com.downthepark.sc.chest;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;

    public class Chest extends JavaPlugin implements Listener {

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

            Player player = (Player) sender;

            File storage = new File(getDataFolder(), player.getUniqueId().toString() + ".yml");

            Inventory chest = getServer().createInventory(player, 54, "Portable Double Chest");

            if (command.getName().equals("chest")) {

                if (!storage.exists()) {
                    try {
                        if (storage.createNewFile()) {
                            player.openInventory(chest);
                            getLogger().log(Level.INFO, "Created NEW chest for " + player.getName());
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                } else {
                    chest.setContents(getItemStacksFromFile(storage));
                    player.openInventory(chest);
                }
            }
            return false;
        }

        @EventHandler
        public void onInventoryClose(InventoryCloseEvent event) {

            File storage = new File(getDataFolder(), event.getPlayer().getUniqueId().toString() + ".yml");

            if(event.getInventory().getName().equals("Portable Double Chest")) {
                setItemStacksToFile(storage, event.getInventory());
                getLogger().log(Level.INFO, "Saved " + event.getPlayer().getName() + "'s chest.");
            }
        }

        private ItemStack[] getItemStacksFromFile(File file) {

            YamlConfiguration yaml = YamlConfiguration.loadConfiguration(file);

            int size = yaml.getStringList("Inventory").size();

            ItemStack[] items = new ItemStack[size];

            for (int i = 0; i < size; i++) {
                items[i] = yaml.getItemStack("Inventory");
            }
            return items;

        }

        private void setItemStacksToFile(File file, Inventory inventory) {

            YamlConfiguration yaml = YamlConfiguration.loadConfiguration(file);

            int size = inventory.getSize();

            ItemStack[] items = new ItemStack[size];

            if (inventory.getName().equals("Portable Double Chest")) {

                for (int i = 0; i < items.length; i++) {
                    items[i] = inventory.getItem(i);
                }
                yaml.set("Inventory", items);
                try {
                    yaml.save(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }

    }
     
    Thanks in advance.