Saving inventories into configs

Discussion in 'Plugin Development' started by BelugaWhale666, Jun 16, 2021.

  1. Offline

    BelugaWhale666

    Hi! I'm trying to make a plugin that adds a /hub command, but I have a problem: In the hub, the player's inventory gets wiped, so I want to save their inventory into a config. I've wrote some code to do that, but it doesn't save.


    Main Class:
    Code:
    package com.mounterisastudios.nav;
    
    import com.mounterisastudios.nav.commands.HubCommand;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.util.ArrayList;
    import java.util.Locale;
    
    public class Main extends JavaPlugin {
    
    
        @Override
        public void onEnable(){
            this.saveDefaultConfig();
            new HubCommand(this);
        }
    
        public static void saveinvtoconfig(Inventory inv, Player p, FileConfiguration config, String world) {
            ArrayList<ItemStack[]> invc = new ArrayList<>();
            invc.add(inv.getContents());
            for (int i = 0; i < invc.size(); i++) {
                if (config.getConfigurationSection("playerinventories." + p.getName().toLowerCase(Locale.ROOT) + "." + world + ".itemstack" + String.valueOf(i)) == null){
                    config.createSection("playerinventories." + p.getName().toLowerCase(Locale.ROOT) + "." + world + ".itemstack" + String.valueOf(i));
                }
                config.set("playerinventories." + p.getName().toLowerCase(Locale.ROOT) + "." + world + ".itemstack" + String.valueOf(i), invc.get(i));
    
            }
    
        }
    
        public static Inventory getinvfromconfig(Player p, FileConfiguration config, String world) {
            ArrayList<ItemStack> invc = new ArrayList<>();
            for (int i = 0; i < config.getConfigurationSection("playerinventories." + p.getName().toLowerCase(Locale.ROOT) + "." + world + ".itemstack").getKeys(true).size(); i++) {
                invc.add(config.getItemStack("playerinventories." + p.getName().toLowerCase(Locale.ROOT) + "." + world + ".itemstack" + String.valueOf(i)));
            }
            Inventory inv = null;
            for (int x = 0; x < config.getConfigurationSection("playerinventories." + p.getName().toLowerCase(Locale.ROOT) + "." + world + ".itemstack").getKeys(true).size(); x++){
                inv.addItem(invc.get(x));
            }
    
            return inv;
    
        }
    
    }
    
    Hub Command Class:
    Code:
    package com.mounterisastudios.nav.commands;
    
    import com.mounterisastudios.nav.Main;
    import org.bukkit.*;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    import java.util.ArrayList;
    
    public class HubCommand implements CommandExecutor{
    
        private Main plugin;
    
        public HubCommand(Main plugin) {
            this.plugin = plugin;
    
            plugin.getCommand("hub").setExecutor(this);
        }
    
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "Only players can travel through interstellar space dimensions, silly!");
                return true;
            }
            Player p = (Player) sender;
            Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), "mvtp " + p.getName() + " hub");
            p.setOp(false);
            p.setGameMode(GameMode.ADVENTURE);
            Main.saveinvtoconfig(p.getInventory(), p, this.plugin.getConfig(), "overworld");
            p.getInventory().clear();
    
            return false;
        }
    
    
    
    
    
    }
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    davidclue

    You are forgetting about saving their armor too, here's something that should in theory work but it's untested.
    Code:
    public static void saveInventory(Inventory inv, String uuid, Main plugin) throws IOException {
            File f = new File(plugin.getDataFolder().getAbsolutePath(), "inventories.yml");
            FileConfiguration c = YamlConfiguration.loadConfiguration(f);
            c.set("armor."+uuid, ((PlayerInventory) inv).getArmorContents());
            c.set("content."+uuid, ((PlayerInventory) inv).getContents());
            c.save(f);
        }
        @SuppressWarnings("unchecked")
        public static void restoreInventory(Player p, Main plugin) throws IOException {
            File f = new File(plugin.getDataFolder().getAbsolutePath(), "inventories.yml");
            FileConfiguration c = YamlConfiguration.loadConfiguration(f);
            ItemStack[] content = ((List<ItemStack>) c.get("armor."+p.getUniqueId())).toArray(new ItemStack[0]);
            p.getInventory().setArmorContents(content);
            content = ((List<ItemStack>) c.get("content."+p.getUniqueId())).toArray(new ItemStack[0]);
            p.getInventory().setContents(content);
        }
     

Share This Page