Getting a list of enabled strings from YML

Discussion in 'Plugin Development' started by ron975, Oct 15, 2012.

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

    ron975

    Assume I have a yml file like this
    Code:
    foobar:
      foo:
        enabled: true
      bar:
        enabled: false
      foo2:
        enabled: false
      bar2:
        enabled: true
    
    How would I go about getting all the strings with enabled being true? (In this case, foo and bar2)
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    There are no strings here. At least in the sense of what yaml considers as a string type.
     
  3. Offline

    TwistedMexi

  4. Offline

    ron975

    By strings, I mean the name of the keys under foobar.

    Wouldn't getStringList("foobar") return a List<String> containing
    foo
    bar
    foo2
    bar2
    ?

    Or if not, what is the correct way of doing that?
    TwistedMexi
    I think you misunderstood me, if not, forgive me, but
    I need to check, for every key under foobar if the subkey "enabled" is true, and then put those keys into a list
     
  5. Offline

    TwistedMexi

    No, that's not how you read a yaml, there's structure to it - which is explained in the documentation I already provided above.
     
  6. Offline

    ron975

    I've read the documentation, and I still don't quite understand completely

    From what I've got, are you suggesting this?
    Code:java
    1.  
    2. plugin.getConfig().getConfigurationSection("foobar").getKeys(false);
    3.  
     
  7. Offline

    TwistedMexi

    No, I understood you. What you're doing here is checking for the boolean value (true/false)

    Code:
    foobar:
      foo:
        enabled: true
      bar:
        enabled: false
      foo2:
        enabled: false
      bar2:
        enabled: true
    foobar.foo.enabled would be the boolean and it's value would return true as above.

    You're not getting the string of anything, and my suggestion is to read over that documentation, as it tells you everything you need to know about reading and setting yaml files.

    http://wiki.bukkit.org/Configuration_API_Reference#Getting_Values
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    Yes that is how you read all the keys for foobar. You then need to use those keys to check if they are enabled.
     
    TwistedMexi likes this.
  9. Offline

    TwistedMexi

    yes, you'll also want to look at plugin.getConfig().getBoolean(); You're on the right track now.
     
  10. Offline

    ron975

    TwistedMexi
    I can't test the code yet at the moment myself, but would this theoretically work?
    Code:java
    1.  
    2. Set<String> foo = plugin.getConfig().getConfigurationSection("foobar").getKeys(false);
    3. List<String> bar = new ArrayList<String>(foo); //Because I like working with Lists better
    4. for (String s : foo){
    5. if(!plugin.getConfig().getBoolean("foobar."+foo+".enabled")){
    6. if (validItems.contains(foo)){
    7. validItems.remove(foo);
    8. }
    9.  
     
  11. Offline

    TwistedMexi

    No, try this:
    PHP:
    List<Stringkeylist plugin.getConfig().getConfigurationSection("foobar").getKeys(false);
    for (
    String key in keylist)
    {
    if (
    getConfig().getBoolean(key))
    {
    //do stuff because this item is true.
    }
    }
    I don't have a way of checking this right now either, but should be similar to that.
     
    kroltan likes this.
  12. Offline

    ron975

    getKeys returns Set<String>, not sure if it'll cast correctly

    Also, wouldn't getBoolean(key) get, for example, "foo" as a boolean, and not "foobar.foo.enabled"?
     
  13. Offline

    TwistedMexi

    yes, that code is probably not working. but thats the structure you need. If you're still needing help when I'm home I'll get some working code for ya.
     
    ron975 likes this.
  14. Offline

    Sagacious_Zed Bukkit Docs

    No you can't cast a set to a list, but you can iterate through it with a for loop none the less.

    As for the key, you will either need the correct section or you will need to concatenate the right key.
     
    TwistedMexi and ron975 like this.
  15. Offline

    ron975

    Thanks guys, I'll test it out when I can.

    Sagacious_Zed
    For purely selfish reasons, I like working with Lists more, therefore,
    List<String> bar = new ArrayList<String>(foo);
     
  16. Offline

    TwistedMexi

    You may want to break that habbit once you start getting into bigger plug-ins, List can result in a performance loss over other options in many scenarios.
     
Thread Status:
Not open for further replies.

Share This Page