List from config help

Discussion in 'Plugin Development' started by Rmarmorstein, Nov 30, 2013.

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

    Rmarmorstein

    I'm trying to get a list of items from a configuration, and then give those items, along with the amount, to the player. This is what the part of the config i want to grab looks like,

    Code:
    rewards:
      items:
        obsidian: 32
        diamond: 10
    The items there are examples, the user can put any item in the game.

    This is the code that I've been using to get the config, and attempt to give it to the player:

    Code:java
    1. public void getItems() {
    2. ConfigurationSection section = this.getConfig().getConfigurationSection("rewards.items");
    3.  
    4. for (String key : section.getKeys(true)) {
    5. items.put(key, section.getString(key));
    6. }
    7. }
    8.  
    9. public void giveItems(Player p) {
    10. if(p != null && p.isOnline()) {
    11. List<String> itm = new ArrayList<String>(items.keySet());
    12. List<String> qty = new ArrayList<String>(items.values());
    13. names = itm.toArray(names);
    14. amt = qty.toArray(amt);
    15.  
    16. for(int x = 0; x < items.size(); x++) {
    17. p.getInventory().addItem(new ItemStack(Material.getMaterial(names[x]), Integer.parseInt(amt[x])));
    18. }
    19. }
    20. }


    I call the giveItems() method on the vote event and from a manual command I've been using to test, with the argument of the player that I want to give the item to.

    Does anyone have any idea why this would not work? What do i need to do to get this to work?
     
  2. Offline

    _Skyden_

    There is a way of laying out lists in the config file and using a function to get all the elements as a list. Looking into that might help.
     
  3. Offline

    Rmarmorstein

    I've looked into the lists, it would not work I dont think because I need to get the value with the item. I guess I can try and find a way to do it with a list, but, i need to get the amount of items to send.
     
  4. Offline

    _Skyden_

    Try something like this.

    config.yml
    Code:
    rewards:
      items:
        - obsidianitemid, obsidianamount
        - diamonditemid, diamondamount
    Java Code:
    Code:java
    1. public static ArrayList<ItemStack> getItems() {
    2. ArrayList<String> data = (ArrayList<String>) PotionProtect.plugin.getConfig().getList("rewards.items");
    3. ArrayList<ItemStack> output = new ArrayList<ItemStack>();
    4.  
    5. for (String s : data) {
    6. String[] values = s.split(", ");
    7. ItemStack i = new ItemStack(Integer.parseInt(values[0]), Integer.parseInt(values[1]));
    8. output.add(i);
    9. }
    10.  
    11. return output;
    12. }
    13.  
    14. public static void giveItems(Player player) {
    15. if (player != null && player.isOnline()) {
    16. for (ItemStack i : getItems()) {
    17. player.getInventory().addItem(i);
    18. }
    19. }
    20. }
     
    Rmarmorstein likes this.
  5. Offline

    _Skyden_

    To save calling the method getItems() each time you give items, you should probably save the ArrayList it returns to a global variable when you load the plugin and change to for loop in giveItems() to use that variable instead of the method getItems()

    Hope I helped.
     
Thread Status:
Not open for further replies.

Share This Page