[SOLVED]Loop Through YML

Discussion in 'Plugin Development' started by thefiscster510, May 2, 2012.

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

    thefiscster510

    have a worldedit CubiodRegion (Two Vector Points)
    I have a cuboid.contains(Vector, Vector) think
    But, I have all kinds of different regions defined in my regions.yml.. It looks something like this.
    Code:
    REG:
      Title =3:
        MAX: (44.0, 255.0, 244.0)
        MIN: (40.0, 0.0, 241.0)
      Me Name =3:
        MAX: (40.0, 71.0, 261.0)
        MIN: (31.0, 69.0, 250.0)
      asdf:
        MAX: (42.0, 71.0, 240.0)
        MIN: (38.0, 71.0, 238.0)



    How would I loop through each one and use the cuboid.contains on it without knowing how many there are?
     
  2. Offline

    CorrieKay

    to loop through a config, you need to understand how a config works, kinda. Lemme explain.

    theres a method in the configuration section (which the config extends) called getKeys(boolean)

    Basically, you need to grab the configuration section of the area you wanna work through. What this means is, you grab the node (i call them nodes, but theyre really configuration sections) that contains all of the key/values you need to iterate through, and then grab a list of the keys, and iterate through it. basically it will look like this:

    Code:
    config:
      herp: derp
      derp: herp
    lets say this is my config. By default, the configuration section youre working with is the root/top level configuration section. it contains the entire config. if you do this:
    for(String key : config.getKeys(false)){
    System.out.println(config.getString(key));
    }
    you would get:
    derp
    herp

    Now, you need to work through just a specific part of your config? how about this configuration?
    Code:
    herp:
      herp1: derp1
      herp2: derp2
    derp:
      derp1: herp1
      derp2: herp2
    First if you wanted to only look at the keys under "derp" you would grab the config section for that node, and then iterate through its keys. It looks something like this
    for(String key : config.getConfigurationSection("derp").getKeys(false)){
    System.out.println(config.getString("derp."+key));
    }

    To make it simpler, instead of using the parent config, i believe you could work with the configuration section directly, with
    ConfigurationSection configSection = config.getConfigurationSection("derp");
    for(String key : configSection.getKeys(false)){
    System.out.println(configSection.getString(key));
    }

    the configuration section is like a mini config, basically. In fact, most of the getter/setter methods that your configuration has, are inherited from ConfigurationSection.

    As a last note, when using getKeys(boolean) the boolean stands for "deep". basically, do you want to grab keys that are direct children of the node youre looking at, or do you want to grab their children as well? for example, if you set it to true, and then printed a list of keys for this
    Code:
    herp:
      derp:
    derp1:
      derp2:
    for(String key : config.getKeys(true)){
    System.out.println(key);
    }
    you would get this
    derp
    derp.derp1
    derp.derp2


    I think thats pretty much about it :3
     
    robertlit, griffenx, NathanG_ and 4 others like this.
  3. Offline

    thefiscster510

    CorrieKay
    Ok, So if i had this config,
    Code:
    REG:
      Title =3:
        MAX: (44.0, 255.0, 244.0)
        MIN: (40.0, 0.0, 241.0)
      Me Name =3:
        MAX: (40.0, 71.0, 261.0)
        MIN: (31.0, 69.0, 250.0)
      asdf:
        MAX: (42.0, 71.0, 240.0)
        MIN: (38.0, 71.0, 238.0) 
    I would do
    Code:
    for(String key : config.getConfigurationSection("REG").getKeys(false)){
    System.out.println(config.getString("derp."+key+".MAX"));
    }
    and it would return each of their max settings?
     
  4. Offline

    CorrieKay

    yes, it would print out, to the console, a string located there :3
     
    thefiscster510 likes this.
  5. Offline

    thefiscster510

    Works! Thanks!
     
    CorrieKay likes this.
  6. Offline

    xGhOsTkiLLeRx

    Additional question:

    My config looks like:

    Code:
    kills:
      '100':
        farbe: '&8'
        zeichen: '*'
      '250':
        farbe: '&e'
        zeichen: '='
    How would I get it, that only the 100 and 250 (the root) is added e.g. to a list?
    Tried it with config.getString(key) but it returns not what I want.

    Any help?
    (Tried this:
    Code:java
    1. for(String key : config.getConfigurationSection("kills").getKeys(false)) {
    2. System.out.println(config.getString(key));
    3. }

    And retrurned only this:MemorySection[path='250', root='YamlConfiguration']
     
  7. Offline

    CorrieKay

    System.out.println(config.getString("kills."+key));
     
  8. Offline

    xGhOsTkiLLeRx


    MemorySection[path='kills.250', root='YamlConfiguration'] would be the output. I'd like to get only the '100'... (and other values of course)

    Solved: It's just the key variable. Lol.
     
  9. Offline

    arwrock

    Sorry for bumping old threads, but how would I put these into ArrayLists, and then send them to a player?
    I have:
    Code:
    Drinks:
      Beer:
          lore: From a nice, cold keg.
          effect:
          time:
          tier:
      Creeper Energy:
          lore: It'll knock your block off!
          effect:
          time:
          tier:
    Say I want to save all names (under Drinks) into an ArrayList<String>. How would I go about doing that? Also, how would I get the lores of both items?
     
  10. Offline

    fireblast709

    arwrock necropost much xD. Get the keys from the ConfigurationSection 'Drinks', which gives you all drinks
     
    arwrock likes this.
  11. Offline

    arwrock

    Lol yeah. But how would I get the lores and put them into an ArrayList?

    Also thanks.
     
  12. Offline

    fireblast709

    arwrock its probably better to have an object where you store the lore, effect and such, linked to the name with Map
     
  13. Offline

    arwrock

    I mean in a config file.
     
Thread Status:
Not open for further replies.

Share This Page