Solved Getting all main items in config

Discussion in 'Plugin Development' started by boardinggamer, Nov 25, 2012.

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

    boardinggamer

    So I wasn't sure what the items are called. They may be keys but I will just call them main items for this.

    Here is an example of my file
    Code:
    johanna28:
      Money: 25.0
    creeperzgod:
      Money: 25.0
    What would I use to go through all the main items in the config? (johanna28, creeperzgod, whatevertherewillbe)
    I tried
    Code:
    for (String names : config.getKeys(true)){
    //code here
    }
    Which obviously must be wrong.

    Can someone help me out here?
     
  2. Offline

    Bavestry

    Just to clarify, you want to look through all of the names, and check how much money they have? Or did you want to get all of the names that exist in the config? If you're trying to go through all of them to check if one exists or not, you need not go through all of them. You would just need to do this:

    Code:
    if(this.getConfig().contains("PLAYERNAME TO CHECK FOR HERE")){
    //code
    }else{
    //code if it's not there.
    }
    Hope this helped!
     
  3. Offline

    boardinggamer

    Bavestry
    Well That was just an example one with the money since it was already open. What it does is checks all the banks members (in another config) and will make a string[] containing all the banks they are part of. so it will have to check every main item in the .yml file to get the members of them
     
  4. Offline

    Bavestry

    Ah I see, maybe something like
    Code:
    this.getConfig().getKeys(true);
    I'm not too sure about this though..
     
  5. Offline

    CorrieKay

    Use false. Using true returns all configuration sections. (johanna28, johanna28.money, creeperzgod, creeperzgod.money) If you use false, it only returns the keys of the specific configuration section youre dealing with.

    So to get the money of everyone, you do this

    Code:
    for(String s : config.getKeys(false)){
      double balance = config.getDouble(s+".money");
      System.out.println(s+" has "+balance+" dollars");
    }
     
    Bavestry likes this.
  6. Offline

    Bavestry

    So, if you use true it returns absolutely everything in the configuration file? Aka the names of the people and their money? In the past I had made several threads about that, nobody gave me a solid answer. Using false would return only each of the players' money?
     
  7. Offline

    boardinggamer

    CorrieKay
    I will try using false and see if it works. then edit this post with what happens

    EDIT: It worked. thank you very much for the help
     
    CorrieKay likes this.
  8. Offline

    CorrieKay

    Not exactly like that. The configuration file is an interesting object, as it is a configuration section made up of configuration sections.

    To show you what i mean, take a look at this configuration file:

    Code:
    key1:
      subkey1:
        value: string 1
      subkey2:
        value string 2
    key2:
      subkey:
        value: string 1
    
    The configuration file you get when you load that as a config... "getKeys(false)" will return key1 and key2.

    If you call getConfigurationFile("key1") it will basically return, in terms of usability, a configuration file that looks like this

    Code:
    subkey1:
      value: string 1
    subkey2:
      value: string 2
    
    So from there, you can call getKeys(false) to return the subkey keys. Note, that unless you specifically use the configuration section you get from getConfigurationSection, you will still need to specify the parent key, otherwise youre trying to look for a "subkey" in the "key" directory, you get it? So if you wanted to look at all of the values, you could do this

    Code:
    for(String key : config.getKeys(false)){
      for(String subkey : config.getConfigurationSection(key).getKeys(false)){
        String value = config.getString(key+".'+subkey); //this will be your values
      }
    }
    
    So in essence, kindasorta yes: it returns all of the keys of the specific configuration section youre dealing with.
     
    Bavestry likes this.
  9. Offline

    Bavestry

    So, you could essentially, if you didn't know ANY of the names in the config, you could get "subkey1" by doing "getKeys(false).getKeys(false)"? Or would you do:
    Code:
    String key1 = getConfig().getKeys(false);
    String subkey1 = key1.getKeys(false);
    
    This is probably wrong though..
     
  10. Offline

    CorrieKay

    getKeys always returns a set of strings, so you'd have to iterate through it
     
    Bavestry likes this.
  11. Offline

    Bavestry

    Alright, thanks. ^.^
     
Thread Status:
Not open for further replies.

Share This Page