How can I get an array of user specified 'keys' from the config? (Not the values)

Discussion in 'Plugin Development' started by toughenough6, Jun 24, 2013.

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

    toughenough6

    So far I have generated a configuration file. It works fine, and I can grab and send values to it. However, I want to grab a list or array of user specified 'keys' from it. For example, my plugin is for my own server and its purpose is to reward players economically for reaching certain levels in McMMO. I already have the Vault and McMMO integration working great, but working with the config in this manner is what is confusing me.

    For example, the default config looks like this:

    Code:
    # Be sure to use proper spelling, etc. in the filling in of values.
    # Ensure that you have Vault installed, as well as McMMO.
     
    # Whether or not this plugin should actually reward the players
    Enabled: true
     
    # Whether or not the plugin should broadcast rewards to the server, as opposed to
    # simply messaging the player.
    Broadcast To Server: false
     
    # The individual rewards for certain levels. Here players get a 50 currency reward when they hit level 5, and so on.
    Level Rewards:
      '5': 50
      '10': 100
      '25': 250
      '50': 500
    
    The bottom is what I'm interested in. I can manually grab the 5, 10, 25, and 50 keys and reward the players with that much money, but what if I want to allow the user to configure the config to add more rewards at more intervals? How would I do that? I've figured out how to grab the values themselves with this:

    Code:java
    1. List<String> rewards = getConfig().getStringList("Level Rewards");
    2. for (String s : rewards)
    3. //Here would be where I act on each value
    4. }

    (Perhaps I shouldn't use strings, but that's not my problem)

    However, that doesn't give me their 'key', does it? For example, if I loaded the default config shown above with that, I would get 50, 100, 250, and 500 passed through the for loop, but not the 5, 10, 25, and 50 that I need as well, right?

    Maybe the methods I'm using do have the capabilities to do what I need, or maybe they don't. This is my first time asking for help here and I really hope that someone can help me out here!
     
  2. Offline

    Rprrr

  3. Offline

    toughenough6

  4. Offline

    Rprrr

    toughenough6
    Code:
            if (getConfig().getConfigurationSection("Level_Rewards") !=null){           
                Set<String> keySet = getConfig().getConfigurationSection("Level_Rewards").getKeys(false);   
               
                for (String s : keySet){
                    Bukkit.broadcastMessage(s);
                }           
            }
    Would print out all keys in the configuration section 'Level_Rewards'.
     
  5. Offline

    toughenough6


    That's what I was suspecting, thanks so much! Should I just create another list with the values and then just have them correspond via their indexes in their respective lists?
     
  6. Offline

    Rprrr

    toughenough6
    I'm not really sure on what you want to do. I'm guessing you want to have a sort of map, for example a HashMap, with as the key the level, and as the value the reward? Here's what I would do in that case:

    Code:
            HashMap<Integer, Integer> levelRewards = new HashMap<Integer, Integer>();
            if (getConfig().getConfigurationSection("Level_Rewards") !=null){         
                Set<String> levelKeyStrings = getConfig().getConfigurationSection("Level_Rewards").getKeys(false); 
             
                for (String levelKeyString : levelKeyStrings){             
                    Integer levelKey = Integer.parseInt(levelKeyString);         
                    Integer rewardValue = getConfig().getInt("Level_Rewards." + levelKeyString);
                 
                    levelRewards.put(levelKey, rewardValue);             
                }         
            }
     
  7. Offline

    toughenough6


    Oh my god yes! Perfect! I've gotten hashmaps to work in the past but I'm always wary to try them because I always have trouble but I think I just learned a lot as well as just fixed my problem I've had for an hour! You're a life saver.

    Thanks again, this means a lot and it's going to save me a lot of time and effort, not only now but in the future due to this new understanding of HashMaps.


    I only had one problem which was a simple error in your code, at line 7 in your code snippet there you had "Integer rewardValue = getConfig().getInt(levelKeyString);", when it should have been "getConfig().getInt("Level_Rewards" + levelKeyString);". Once I fixed that it worked great and my plugin is on its way to working perfectly!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
    Rprrr likes this.
  8. Offline

    Rprrr

    toughenough6
    Sorry - my bad! I guess I wasn't paying enough attention when writing that line. :p You're right. :)
     
Thread Status:
Not open for further replies.

Share This Page