API Saving Inventory in config

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

?

Was this useful to you?

  1. Nope, not at all

    33.3%
  2. Yes, a little

    55.6%
  3. Yes! Amazing!

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

    nielsbwashere

    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!

    Code:
    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);
                if(is==null)continue;
                inv.addItem(is);
            }
            return inv;
        }
        public static void setInventory(String s, FileConfiguration fc, Inventory inv){
            if(s==null||s.equals(""))return;
            for(int i=0;i<inv.getSize();i++){
                fc.set(s+"."+i, inv.getContents()[i]);
            }
            //Save the config
        }
    }
    
    You just use
    Code:
    player.openInventory(InventoryHelper.getInventory("Backpacks."+player.getName(),getConfig())) 
    when you want it to open.

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

    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:
    Code:
    //getting the size of the inventory
    int size = config.getConfigurationSection(path).getKeys(false).size();
     
    Last edited: Jul 8, 2015
  3. Offline

    DoggyCode™

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

    Code:
     
      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"));
      }
       
      @SuppressWarnings("unchecked")
      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]);
      p.getInventory().setArmorContents(content);
      content = ((List<ItemStack>) c.get("inventory.content")).toArray(new ItemStack[0]);
      p.getInventory().setContents(content);
       
      }
     
  4. Offline

    nielsbwashere

    @DoggyCode™
    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

    Lolmewn

    @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

    nielsbwashere

    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

    Lolmewn

    @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

    nielsbwashere

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

    Lolmewn

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

Share This Page