How do I get a list of all of the values inside of a ConfigurationSection?

Discussion in 'Spigot Plugin Development' started by Hallowizer, Apr 23, 2017.

  1. Before you think about arrays, here is what I am talking about:

    Code (Text):
    key1:
      key2: true
      key3: false
      key4: "hi"
    So, my question is, what function do I need to run with the argument of "key1" to get the ["key2","key3","key4"]?
     
  2. Code (Java):
    ConfigurationSection section = getConfig().getConfigurationSection("key1");
    //This will get the configuration section, I'd just check if it's not null before continuing.

    if (section != null) {
     
           for (String keys : section.getKeys(true)) {

                 //do something with keys
           }

    }
     
    Using the getKeys method will get all the KEYS of that configuration section. If you put true it will return
    key1, key2, key3 and their values (true, false, "hi") if you set it to false it will just return key1, key2 and key3

    Hope that helped


    --- you would set it to false for what you want to do (if you only want to return the sub nodes without their values)
     
  3. If you pass 'true', it'll return the full tree rooted at the section you call it on (in the given example, only key2, key3 and key4. If we had, for example:
    Code (YAML):
    key1:
      key2
    : false
      key3
    : true
      key4
    :
        key5
    : 'foo'
    It'd return key2, key3, key4, key5
     
  4. Yea I forgot to mention that :p