Resource Load and set inventory from config

Discussion in 'Spigot Plugin Development' started by Attacler, Apr 20, 2017.

  1. Hello there,

    I created a little method for creating/getting an inventory from the config!

    Code (Text):
        public Inventory loadinventory(String name, int rows){
            Inventory inv = Bukkit.createInventory(null, 9 * rows, ChatColor.GOLD + name); //create inventory with the name given
         
            FileConfiguration config = getConfig(); //this has to be your configuration path
            for(int i = 0; i < 9 * rows; i++){ //for loop so we can loop trough all slots and add items
                String path = "inventory." + name + "." + i; //this is the path where you get an slot from the config, you can change this to what you want, make sure you add + i and name!
                if(config.get(path) != null){ //check if the slot is setted in the config
                    Material material = Material.valueOf(config.getString(path + ".type").toUpperCase()); //create a material from the config
                    ItemStack item = new ItemStack(material); //make a itemstack from the material
                    ItemMeta im = item.getItemMeta(); //get the item propperties
                    im.setDisplayName(config.getString(path + ".name")); // set the name of the item
                    item.setItemMeta(im); //save the item meta
                    inv.setItem(i, item); // set the item in the inventory
                }
            }
            return inv;
        }

    Code (Text):
        public void setslot(String inventoryname, int slot, ItemStack item){
            FileConfiguration config = getConfig(); //this has to be your configuration path
            String path = "inventory." + inventoryname + "." + slot; //this is the path where you get an slot from the config, you can change this to what you want, make sure you add + i and name!
            ItemMeta im = item.getItemMeta(); //get the item propperties
            String name = im.getDisplayName(); //get the item name
            String material = item.getType().toString(); // getting the material as a string
            config.set(path + ".type", material); //add the material to the config
            config.set(path + ".name", name); //add the name to the config
            saveConfig(); //save the config
        }

    I hope you guys can do something with it!

    Greetings,
    Bart
     
    #1 Attacler, Apr 20, 2017
    Last edited: Apr 20, 2017
    • Useful Useful x 1
  2. I mean, I could probably figure this out in 5 minutes, but thanks, I guess
     
  3. No problem :D (im not that good with java, so i try and make my scripts usefull!)
     
  4. In the get inventory method, instead of i < 54; you should do i <= 9 * rows; as you don't know what the size of the inventory will be.
     
    • Useful Useful x 1
  5. Thank you, i changed it!
     
    • Friendly Friendly x 1