How to store Inventory and Player objects for virtual chest plugin

Discussion in 'Plugin Development' started by nickdeveloper, Jan 18, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    nickdeveloper

    Hello,

    I started with the Bukkit API a few days ago, but I've been developing in Python for about a year now. I understand most Java syntax.

    I am trying to store an Inventory object and a Player object for a virtual chest plugin.
    My goal is to somehow save all player virtual chests in the onDisable() and read these saved objects in the onEnable().

    We could try to convert the inventory to a string (in the onDisable()) and put that in the config with the player name, then convert that back to an inventory object to load the inventory in the onEnable(). If this is a recommended solution, could someone clarify how it could be accomplished?

    Here is my code:

    VChest (main class)
    Code:
    package io.github.**********.VChest;
    
    import org.bukkit.Bukkit;
    
    import java.util.HashMap;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import io.github.**********.DataStorage.DataStorage;
    
    public final class VChest extends JavaPlugin {
      
        public static HashMap<Player, Inventory> inventories = DataStorage.getInvenMap();
      
        @Override
        public void onEnable() {
            getLogger().info("VChest 1 ENABLE");
            getLogger().info("Loading player VChests");
          
            // load process
          
        }
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
          
            if (cmd.getName().equalsIgnoreCase("vc")) {
              
                Player player = (Player) sender;
              
                // initialize HashMap to store vchests
                // HashMap<Player, Inventory> inventories = DataStorage.getInvenMap();
              
                if (!(inventories.containsKey(player))) {
                    Inventory chest = Bukkit.getServer().createInventory(null, 54);
                    inventories.put(player, chest);           
                }
              
                player.openInventory(inventories.get(player));
                inventories.put(player, inventories.get(player));
              
                return true;
            }
          
            return false;
        }
      
        @Override
        public void onDisable() {
            getLogger().info("Saving player VChests");
          
            // save process
          
            getLogger().info("VChest v1 DISABLE");
        }
    }
    
    DataStorage (helper class to work with HashMap)
    Code:
    package io.github.**********.DataStorage;
    
    import java.util.HashMap;
    
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    
    public class DataStorage {
        private static HashMap<Player, Inventory> invenMap = new HashMap<Player, Inventory>();
        public static HashMap<Player, Inventory> getInvenMap() {
            return invenMap;
        }
    }
    
    Regards
     
  2. Offline

    Toastlawine

    You have to save the inventories in a Config
    There are good methods in the Bukkit API to save ItemStacks:

    Code:
    public static void saveInventory(Player p) {
    
    Inventory inv = DataStorgae.getInvenMap().get(p);
    
    /*It is better to make many files with the UUIDs as the name for performance and also to prevent losing Inventories when changing your name.*/
    
    File file = new File("plugins/SomeDirectory/"+p.getUniqueId()+".yml");
    YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
    
    for (int i = 0; i < InventorySize; i++) {
       if (inv.getItem(i) != null) {
           config.set("Slot." + i, inv.getItem(i));
       }
    }
    try {
       config.save(file);
    } catch (IOException e) {
       e.printStackTrace();
    }
    //To read:
    ItemStack is = config.getItemStack("Slot."+SlotID);
    }
    So just iterate through your Players inventories in onDis- and Enable.
     
    Deathoffully likes this.
  3. Offline

    Deathoffully

    Hi,
    I did the same in a plugin a couple of days ago.

    Code:
    // save new data in list
    for (Entry<UUID, PlayerInventory> entry : VIRTUALINVENTORIES.entrySet()) {
      UUID key = entry.getKey();
    
      // add data to lists
      plugin.getConfig().set("Inventories"+"."+key.toString(), entry.getValue());
              
      plugin.saveConfig();    // save changes
    }
    This way you can store the inventories in the config but I have no idea how to read them from the config. In the example code I used a HashMap (VIRTUALINVENTORIES) to map the inventories to the players and stored each inventory in a subpath of "Inventories" with the UUID of the corresponding player. You don't need to make a string out of the inventory to store it, I think that's because it's serializable.
     
  4. Offline

    nickdeveloper

    Can you clarify the //To read: part? I think I understand it but how would I iterate through the file and make an inventory with this ItemStack is data so that I can replace the inventory in the hashmap with this newly retrieved data?
     
    Last edited: Jan 21, 2019
Thread Status:
Not open for further replies.

Share This Page