API Saving Inventory in config

Discussion in 'Resources' started by nielsbwashere, Jul 7, 2015.


Was this useful to you?

  1. Nope, not at all

  2. Yes, a little

  3. Yes! Amazing!

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


    Hey guys, nielsbwashere here.
    I had a real problem with my plugins, I couldn't save Inventories; you can't save them in a config!
    So for the sake of saving Inventories, I thought why not create a class that handles it for you.
    Down here it is!

    class InventoryHelper{
        public static Inventory getInventory(String s, Configuration c){
            if(s==null||s.equals(""))return null;
            int length= config.getConfigurationSection(path).getKeys(false).size();
            Inventory inv = Bukkit.createInventory(null, length);
            for(int i=0;i<length;i++){
                ItemStack is = c.getItemStack(s+"."+i);
            return inv;
        public static void setInventory(String s, FileConfiguration fc, Inventory inv){
            for(int i=0;i<inv.getSize();i++){
                fc.set(s+"."+i, inv.getContents()[i]);
            //Save the config
    You just use
    when you want it to open.

    On close event, you just save it by saying:
    InventoryHelper.setInventory("Backpacks."+player.getName(), this, event.getInventory())
    I hope this helped somebody, for me this was really useful.

    EDIT: Thanks to FisheyLP I was able to improve the code
    Last edited by a moderator: Jul 10, 2015
  2. Nice, but thats more an Util than an API I think.
    And instead of the JavaPlugin argument in .setInventory use FileConfiguration to allow custom configs
    //EDIT: Make a new method which uses FileConfiguration and File instead of JavaPlugin and keep the method you have now
    And change the String s to String path to make it less confusing.
    And instead of saving and getting .Length use something like this:
    //getting the size of the inventory
    int size = config.getConfigurationSection(path).getKeys(false).size();
    Last edited: Jul 8, 2015
  3. Offline


    Can't one just use something simple as this to save and get a inventory?:

      public static void saveInv(Player p) throws IOException {
      YamlConfiguration c = new YamlConfiguration();
      c.set("inventory.armor", p.getInventory().getArmorContents());
      c.set("inventory.content", p.getInventory().getContents());
         c.save(new File("plugins/" + File.separator + "StaffModeAlexay" + File.separator + "Inventories", p.getName()+ ".yml"));
      public static void restoreInv(Player p) throws IOException {
         YamlConfiguration c = YamlConfiguration.loadConfiguration(new File("plugins/" + File.separator + "StaffModeAlexay" + File.separator + "Inventories" , p.getName()+".yml"));
      ItemStack[] content = ((List<ItemStack>) c.get("inventory.armor")).toArray(new ItemStack[0]);
      content = ((List<ItemStack>) c.get("inventory.content")).toArray(new ItemStack[0]);
  4. Offline


    Nope, you can't save inventories, bukkit hasn't added that.
    You can save ItemStacks, but not inventories, I think that is a strange part about bukkit.
    Last edited by a moderator: Jul 8, 2015
  5. Offline


    @nielsbwashere Of course, you could make your own object, make it serializable and have a list of ItemStacks in it representing an inventory. Just add a constructor which takes an Inventory and serialize/deserialize the crap out of it ^^
  6. Offline


    Though, why would you do that when you can use this method instead :p
    Also; serialize and deserialize is probably harder then using this
  7. Offline


    @nielsbwashere It's cleaner, doesn't use casting and no need to suppress any warnings (which is a REALLY bad habit in any case).

    EDIT: Lol, just realized this was under "Resources". Thought it was a question.
  8. Offline


    :p this doesn't really use casting as well, and it doesn't suppress any warnings :p
  9. Offline


    Nah, that was the code you quoted. Oh well ^^
Thread Status:
Not open for further replies.

Share This Page