How to save + load an inventory easily (IK, wrong section)

Discussion in 'Plugin Development' started by captainawesome7, Jul 25, 2011.

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

    captainawesome7

    Intro​
    Well after screwing with YAML for a little while (a lot longer than necessary because I forgot config.load() :/), I wrote two quick methods for saving and loading player's inventories.
    Saving a Player's Inventory
    To save a player's inventory just use the following method:
    Code:
    public void saveInventory(Player player) {
        Integer size = player.getInventory().getSize();
        Integer i = 0;
        File saveFile = new File(this.getDataFolder() + File.separator + "inventories", player.getName() + ".yml");
        Configuration config = new Configuration(saveFile);
        for(i=0; i<size; i++) {
            ItemStack item = player.getInventory().getItem(i);
            if(item.getAmount() !=0) {
                config.setProperty(i.toString() + ".amount", item.getAmount());
                Short durab = item.getDurability();
                config.setProperty(i.toString() + ".durability", durab.intValue());
                config.setProperty(i.toString() + ".type", item.getTypeId());
                config.save();
            }
        }
    }
    Note that this method saves the player's default inventory to a plugins/yourplugin/inventories/playerName.yml
    You can easily convert this method to save whatever inventory you want to.
    Loading a Player's Inventory
    To load a player's default inventory from the save location that was used above, use this method:
    Code:
    public void loadInventory(Player player) {
        File saveFile = new File(this.getDataFolder() + File.separator + "inventories", player.getName() + ".yml");
        Configuration config = new Configuration(saveFile);
        config.load();
        Integer i = 0;
        Integer size = player.getInventory().getSize();
        for(i=0; i<size; i++) {
            ItemStack item = new ItemStack(0, 0);
            if(config.getInt(i.toString() + ".amount", 0) !=0) {
                Integer amount = config.getInt(i.toString() + ".amount", 0);
                Integer durability = config.getInt(i.toString() + ".durability", 0);
                Integer type = config.getInt(i.toString() + ".type", 0);
                item.setAmount(amount);
                item.setTypeId(type);
                item.setDurability(Short.parseShort(durability.toString()));
                player.getInventory().setItem(i, item);
                player.updateInventory();
            }
        }
    }
    Note that like the method above, this uses the player's regular inventory, but can be easily converted to work with any custom inventory (like ones you can make using BukkitContrib)
    Practical Save Method
    Because the methods above use standard minecraft inventories, they're pretty much useless. A much more practical use is saving your own inventory created with BukkitContrib. A good way to do this is make a hashmap containing players/names and ItemStack[]s. Then load the inventory when applicable. To do that, use this saving method:
    Code:
    public HashMap<String, ItemStack[]> inventories = new HashMap<String, ItemStack[]>();
    public void saveInventory(Player player, ItemStack[] items) {
        File saveFile = new File(this.getDataFolder() + File.separator + "inventories", player.getName() + ".yml");
        Configuration config = new Configuration(saveFile);
        Integer size = 10;
        Integer i = 0;
        CustomInventory inv = new CustomInventory(size, "Extra Space");
        inv.setContents(inventories.get(player.getName()));
        for(i=0; i<size; i++) {
            ItemStack item = inv.getItem(i);
            if(item.getAmount() !=0) {
                config.setProperty(i.toString() + ".amount", item.getAmount());
                Short durab = item.getDurability();
                config.setProperty(i.toString() + ".durability", durab.intValue());
                config.setProperty(i.toString() + ".type", item.getTypeId());
                config.save();
            }
        }
        inventories.remove(player.getName());
    }
    Practical Load Method​
    For simplicity's sake we are going to use the CustomInventory again, instead of a different method of putting the new ItemStack[] into the HashMap. Here is the load method:
    Code:
    public void loadConfig(Player player, ItemStack[] items) {
        File saveFile = new File(this.getDataFolder() + File.separator + "inventories", player.getName() + ".yml");
        Configuration config = new Configuration(saveFile);
        config.load();
        Integer i = 0;
        Integer size = 10;
        CustomInventory inv = new CustomInventory(size, "Extra Space");
        for(i=0; i<size; i++) {
            ItemStack item = new ItemStack(0, 0);
            if(config.getInt(i.toString() + ".amount", 0) !=0) {
                Integer amount = config.getInt(i.toString() + ".amount", 0);
                Integer durability = config.getInt(i.toString() + ".durability", 0);
                Integer type = config.getInt(i.toString() + ".type", 0);
                item.setAmount(amount);
                item.setTypeId(type);
                item.setDurability(Short.parseShort(durability.toString()));
                inv.setItem(i, item);
            }
        }
        inventories.put(player.getName(), inv.getContents());
    }
    And yes I know this is in the wrong section, it was originally a question about inventory saving, but I realized that I just forgot Configuration.load(). I didn't feel like creating a whole new thread so I just edited this one.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2016
  2. Offline

    alta189

    I bet you noticed, but you code has a ton of BBCode in it
     
  3. Offline

    captainawesome7

    Yeah i deleted one line and XenForo went crazy on me. Should be fixed now.
     
  4. Offline

    alta189

    That's why I use the BBEditor for bigger posts
     
Thread Status:
Not open for further replies.

Share This Page