How can i get all subnodes in a Config(yml)

Discussion in 'Plugin Development' started by Mr. X, Dec 18, 2011.

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

    Mr. X

    Hi
    I want to read a config who includes nodes who have no static names. Also i think i can get the names of the nodes with
    Code:
    config.getKeys("myrootnode")
    but this code gives me nothing .I have no more ideas can anyone help me?
     
  2. Offline

    se1by

    You have to use a boolean value, not a string.
    true to get all subnodes, false to get the first subnodes, not the subnodes of the subnodes.
     
  3. Offline

    Mr. X

    What did you mean? can you give a example?
     
  4. Offline

    Acrobot

    I guess he meant:
    config.getKeys(false)
    to get all the 1st-level nodes (ones nearest the left side of your file)

    config.getKeys(true)
    to get all nodes (main ones and their subnodes)
     
  5. Offline

    Mr. X

    I guess it too but it shouldnt work. Eclipse underline it.
     
  6. Offline

    Acrobot

    @Mr. X
    Well, have you tried config.getKeys() without any arguments?
     
  7. Offline

    Pandemoneus

  8. Offline

    Mr. X

    mhh im to stupid:
    Code:
    this.getConfig().getList("formats");
    I use it i'm get NULL everytime!
    my config:
    Code:
    formats:
     1:
      prefix: '&3[&4Pvp&3]'
      suffix: ''
    I don't see what is the problem can anyone help me pls?
     
  9. Offline

    Sagacious_Zed Bukkit Docs

    @Mr. X
    For the key formats, the value is not a list, therefore it will return null.
     
  10. Offline

    Mr. X

    ok, and how can i get the subnodes of formats?
     
  11. Offline

    Windwaker

    If you just want to get the subnodes of "formats" you can...
    Code:java
    1. getConfig().getConfigurationSection("formats").getKeys(false);

    Would get you "1" and any other nodes in the same position.

    If you...
    Code:java
    1. getConfig().getConfigurationSection("formats").getKeys(true);

    You will get all the subnodes :)
     
  12. Offline

    bergerkiller

    @WalkerCrouse Don't forget that 'getConfigurationSection' will return null if it doesn't exist. Either force-make this node when loading or ignore it at all.
    Code:
    ConfigurationSection sec = getConfig().getConfigurationSection("formats");
    if (sec != null) {
        for (String key : sec.getKeys(false)) {
            //do stuff here
        }
    }
    
    
    But don't mind me, I made a complete configuration wrapper because Bukkit config is simply annoying. Look at this beauty :)
    Code:
            if (useSpawnLimits) {
                //Spawn restrictions
                ConfigurationNode limits = config.getNode("spawnlimits");
                ConfigurationNode node;
                node = limits.getNode("default");
                for (String key : node.getKeys()) {
                    SpawnHandler.setDefaultLimit(key, node.get(key, -1));
                }
                node = limits.getNode("global");
                for (String key : node.getKeys()) {
                    SpawnHandler.setGlobalLimit(key, node.get(key, -1));
                }
                node = limits.getNode("worlds");
                for (ConfigurationNode cn : node.getNodes()) {
                    String world = cn.getName();
                    for (String key : cn.getKeys()) {
                        SpawnHandler.setWorldLimit(world, key, cn.get(key, -1));
                    }
                }
            }
     
  13. Offline

    Windwaker

    As do I ;)
     
  14. Offline

    bergerkiller

    @WalkerCrouse nice :)
    I see you left the 'default' system in there...I just threw it away. Prefer having the defaults being set while you read the value from the configuration instead. Any use for the 'defaults'?
     
  15. Offline

    Windwaker

    I found that setting a value as apposed to adding defaults caused some issues like overwriting the entire config when I try to set another value. Don't know why, don't care :D
     
  16. Offline

    bergerkiller

  17. Offline

    Mr. X

    Thanks for the Example, it works now but when the YML includes a "Ä" or "ü" or "ö" then it no longer works
     
  18. Offline

    Windwaker

    I don't think YAML supports special characters
     
  19. Offline

    Chiller

    Haha "special" characters :p
     
  20. Offline

    Windwaker

Thread Status:
Not open for further replies.

Share This Page