Iterate through config to give specific items to specific inventory slots

Discussion in 'Plugin Development' started by AoH_Ruthless, Feb 13, 2014.

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

    AoH_Ruthless

    So, I am trying to make a kit plugin. I save kits in a config file with the inventory slot. But I need help giving the items back to the player in specific inventory slots. I am unsure if the following for loop will work.

    Code:java
    1. String path = kitName + ".";
    2.  
    3. ConfigurationSection s = kits.getConfigurationSection(kitName + ".items");
    4. for (String str : s.getKeys(false)) {
    5. int slot = Integer.parseInt(str);
    6. if (0 > slot && slot > 36)
    7. return;
    8.  
    9. String string = kitName + ".items." + slot + ".";
    10. String type = kits.getString(string + "type");
    11. String name = kits.getString(string + "name");
    12. int amount = kits.getInt(string + "amount");
    13.  
    14. ItemStack is = new ItemStack(Material.matchMaterial(type.toUpperCase()), amount);
    15. ItemMeta im = is.getItemMeta();
    16.  
    17. im.setDisplayName(name);
    18. is.setItemMeta(im);
    19.  
    20. p.getInventory().setItem(slot, is);
    21. }
    22.  
    23. // We also need to give them armor.
    24. String helmet = kits.getString(path + "armor.helmet").toUpperCase();
    25. String chestplate = kits.getString(path + "armor.chestplate").toUpperCase();
    26. String leggings = kits.getString(path + "armor.leggings").toUpperCase();
    27. String boots = kits.getString(path + "armor.boots").toUpperCase();
    28.  
    29. // Make sure each isn't null.
    30. if (helmet != null)
    31. p.getInventory().setHelmet(new ItemStack(Material.matchMaterial(helmet)));
    32. if (chestplate != null)
    33. p.getInventory().setChestplate(new ItemStack(Material.matchMaterial(chestplate)));
    34. if (leggings != null)
    35. p.getInventory().setLeggings(new ItemStack(Material.matchMaterial(leggings)));
    36. if (boots != null)
    37. p.getInventory().setBoots(new ItemStack(Material.matchMaterial(boots)));


    This is what the config will look like (sample):

    Code:
    kits:
      kitName:
        items:
          '0':
            type: diamond
            name: 'blah'
            amount: 4
          '10':
            type: gold_ingot
            name: 'gold'
            amount: 12
        armor:
          helmet: diamond_helmet
          leggings: gold_boots  
    You may be asking why I haven't tested it. This is because it is such a large plugin that I am not even close to completing yet. If I tested it in the current stage, it might not even compile properly.
     
Thread Status:
Not open for further replies.

Share This Page