Getting a string list from config without a list

Discussion in 'Plugin Development' started by stamo, Apr 5, 2014.

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

    stamo

    I do not really know how to phrase this question. But, how do you get a list of strings from within a sub group in a config file.
    For example I have this comfig:
    Code:
    foo1:
        something: blah
        someotherthing: blah2
    foo2:
        something: blah
        somethingthing: blah2
    How do I get foo1 and foo2?
     
  2. Offline

    Serializator

    Code:java
    1. for(String table : getConfig().getKeys(false)) {
    2. String blah1 = getConfig().getString(table + ".blah1");
    3. String blah2 = getConfig().getString(table + ".blah2");
    4. }
     
  3. Offline

    Shayana

    The method getConfig().getKeys(false) in type JavaPlugin returns a Collection of String containing each key at the first level of the config.yml
    You can use it at any level. For instance :

    Code:
    Smthg1:
      value1: s
      value2: ss
    Smthg2:
      value3: sss
      value4: ssss
    
    Using
    Code:java
    1. Set<String> lvl1 = Plugin.getConfig().getKeys(false);
    2. for(String s : lvl1) {
    3. System.out.println(s);
    4. }
    5.  


    Will return : "Smthg1" and "Smthg2"

    While this code :

    Code:java
    1. Set<String> lvl2 = Plugin.getConfig().getConfigurationSection("Smthg1").getKeys(false);
    2. for(String s : lvl2) {
    3. System.out.println(s);
    4. }


    Will return : "Smthg1.value1" and "Smthg1.value2"

    Note : Passing the argument true while using getKeys() will return all the keys of the configuration section, no matter their level. (For our first example "Smthg1", "Smthg1.value1" and "Smthg1.value2" would have been printed.

    Hope it helped !
     
    amhokies and stamo like this.
Thread Status:
Not open for further replies.

Share This Page