Set Inventory Contents From A Config

Discussion in 'Spigot Plugin Development' started by Roimeiqui7, May 25, 2018.

  1. Hello, I have no idea how can I set an inventory contents getted from a config.

    I have this:

    Code (Text):
    @EventHandler
        public void onCloseInentory(InventoryCloseEvent e) {
            if(e.getInventory().getTitle().equals("Scylla")) {
                ItemStack[] contentsList = e.getInventory().getContents();
                f = new File(MainPvCommand.userdata, File.separator + e.getPlayer().getName()+".yml");
                fc=YamlConfiguration.loadConfiguration(f);
                fc.set(e.getPlayer().getName() + ".Contents", contentsList);
                try {
                    fc.save(f);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
    Thats Works fine but i have no idea how can i set this contents:

    Code (Text):
    }else if(arg[0].equalsIgnoreCase("open")){
                            Inventory mpv = Bukkit.createInventory(null, 54, "Scylla");
                           
                        //HERE
                            p.openInventory(mpv);                  
                        }
    Thanks :D
     
  2. By creating an inventory, there is a method that allows you to set an itemstack by doing:
    Code (Text):
    inventory#setItem(slot, itemstack);
    So if you have an existing itemstack from a configuration file, you also should set it's slot, not only the itemstack.

    If you want to get an itemstack:
    Code (Text):
    config#getItemStack(path)
    Keep in mind, this code works only when saving in a configuration file an itemstack as an itemstack not just a material or a material that has missing values because it will throw you a null exception.
     
    #2 Nosmakos, May 25, 2018
    Last edited: May 25, 2018
  3. I want to set the contents (ItemStack[])
     
  4. It should work just fine but also save their slot too.
     
  5. Whenever you do the YamlConfiguration#get(String path), you can cast the return value to whatever it is you want so long as what you cast it to is "ConfigurationSerializable". For example, you can actually do this:

    public class A implements ConfigurationSerializable{
    //Constructors and a bunch of other crap here.

    //Add your public Map<String, Object> serialize() method here
    }

    you can then do

    A whateverYourVariableNameisHere = (A) YamlConfiguration#get(path);

    That should work with your ItemStack[] thingy so long as you store the value as an ItemStack[] :)
     
  6. You could just manually go through the contents of the config section. You'd have to work with the item meta, but it'd probably be simpler than above. /shrug
     
    • Like Like x 1