Loading Inventory Contents from a Config?

Discussion in 'Plugin Development' started by Epicballzy, Aug 18, 2014.

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

    Epicballzy

    How would you load inventory contents from a config? I got the part down where it saves the contents.. But the loading part is totally blank for me.

    What would I need to load it? A simple hint is fine for me.

    This is what I use to save the contents:
    Code:java
    1. public void createKit(Player p, String kitName) {
    2. Files.getInstance().kits.set(kitName + ".items", p.getInventory().getContents());
    3. p.getInventory().clear();
    4. Files.getInstance().saveKits();
    5. MessageManager.getInstance().msg(p, ChatColor.GREEN + "Successfully created kit: " + ChatColor.RED + kitName);
    6. }


    This will be used to create easy kits. Not sure if its the best way to get the items from your inv and set them...

    All help is appreciated!
    Thanks.
     
  2. Offline

    Forseth11

    You will need to make your own ItemStack parser from the items in the config.
     
  3. Offline

    Epicballzy

    Forseth11 This only allows me to set the contents, and not actually add the contents (int the config) with the items already in the inventory.

    Code:java
    1. @SuppressWarnings("deprecation")
    2. public void getKit(Player p, String kitName) {
    3. ItemStack[] contents = p.getInventory().getContents();
    4. List<?> list = Files.getInstance().kits.getList(kitName);
    5. for(int in = 0; in < list.size(); in++) {
    6. contents[in] = (ItemStack) list.get(in);
    7. }
    8. p.getInventory().setContents(contents);
    9. p.updateInventory();
    10. MessageManager.getInstance().msg(p, ChatColor.GREEN + "You received kit: " + ChatColor.RED + kitName);
    11. }


    Any suggestions?
    I've tried:
    Code:java
    1. p.getInventory().addItem(contents);//Gives an error
    2.  
    3. //and
    4.  
    5. //Obviously an error...
    6. p.getInventory().setContents(p.getInventory().getContents() + contents);
     
  4. Offline

    Lactem

    Code:
    p.getInventory().addItems(contents);//Gives an error

    You just forgot the s.
     
  5. Offline

    Epicballzy

    Lactem There is no method as "addItems(...);", only addItem(...);
     
  6. Offline

    Lactem

    I'm sorry. I said that incorrectly. You can add an array of items with the addItem() function. See because the method signature says addItem(ItemStack... items), meaning you can have multiple there. You could add one item, an array of items, or even
    Code:
    player.getInventory().addItem(new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR));
     
  7. Offline

    fireblast709

    Forseth11 ItemStacks are ConfigurationSerializable, which means that set(path, stack) will save it correctly. You can retrieve them again by using the supplied getter method getItemStack(path).
     
  8. Offline

    Forseth11

    fireblast709 I wish I have known that because I have made very complex methods to parse ItemStacks.
     
Thread Status:
Not open for further replies.

Share This Page