Get list of items under parent YAML

Discussion in 'Plugin Development' started by Nogtail, May 23, 2014.

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

    Nogtail

    If I had a YAML file:
    Code:
    objects:
      1:
          server: example
          location:
            world: world
            x: 400
            y: 500
            z: 600
      2:
          server: example
          location:
            world: world
            x: 400
            y: 500
            z: 600
    and I wanted to get a list of all items in "objects" ("1" and "2") how would I do this?
     
  2. Offline

    NathanWolf

    Set<String> keys = root.getKeys(false);

    EDIT, or:

    Set<String> keys = root.getConfigurationSection("objects").getKeys(false);
     
  3. 1. put this somewhere in your class:
    Code:java
    1.  
    2. Set<String> list = null;
    3.  

    2. in onEnable():
    Code:java
    1.  
    2. list = yourConfig.getConfigurationSection("obects").getKeys(false);
    3.  

    3.:
    Code:java
    1.  
    2. for(String key : list) {
    3.  
    4. String server = yourConfig.getString("objects."+key+".server");
    5. World world = getServer().getWorld(yourConfig.getString("objects."+key+".location.world"));
    6. int x = yourConfig.getInt("objects."+key+".location.x");
    7. int y = yourConfig.getInt("objects."+key+".location.y");
    8. int z = yourConfig.getInt("objects."+key+".location.z");
    9. }
    10.  

    done :D
     
Thread Status:
Not open for further replies.

Share This Page