getChildrenList() and getChildrenMap()

Hey guys so I’ve been fiddling around with HOCON configs for a while here and I’m a bit stumped as to what these two methods getChildrenList() and getChildrenMap() do exactly, and how to use them for my needs.

From my Understanding getChildrenList() Returns a list of all the elements inside of a node if they’re separated by commas? Not sure if bracket or curly brace makes a difference.

getChildrenMap()
I am not sure what it does at all, I’ve seen a few different explanations but they don’t quite seem to agree. I assume it would grab all the nodes in nodeA{nodeB = "123" nodeC = "123"} and puts them in a map using the node Key as the key and the value as the value, but I have attempted using it to no avail.

I was reading the source/documentation for configurate and it seems that if you have multiple of the same node, it will merge them?


This is the current project I’m working on using configs

you can take a look at the config, and I successfully load in the Strings from the commands and vote-sites nodes using getChildrenList()

Now my plugin is becoming a bit more complex so I have something like this currently

     Rewards{
            random:["12", "reward1", "say Reward1"]
            random:["12", "reward2", "say Reward2"]
            random:["15", "reward3", "say Reward3"]
            random:["13", "reward4", "say Reward4"]
            random:["78", "reward5", "say Reward5"]
            set:["say This is a set Reward"]
            set:["say this is another set Reward"]

        }

From what I read, I thought if I used getChildrenList() on config.Rewards.random or config.Rewards.set it would load in all of them into the same internal “list”.

Any advice on how to use these two functions, or how I could approach my problem of correctly loading the above config information into an array would be appreciated. Or maybe you have suggestions as to how I could better design the config file.

Thanks in advance guys <3

1 Like

In your config you have not defined a list, you have defined several times the same fields. Your config should look like:

     Rewards{
            random:[ ["12", "reward1", "say Reward1"]
                     ["12", "reward2", "say Reward2"]
                     ["15", "reward3", "say Reward3"]
                     ["13", "reward4", "say Reward4"]
                     ["78", "reward5", "say Reward5"] ]
            set:[ ["say This is a set Reward"] ["say this is another set Reward"] ]
        }

Now you can call getList() or getChildrenList() on config.Rewards.random and config.Rewards.set

The HOCON spec means that all of those lists are merged into one.

getChildrenList() gets the node as a list of objects, and getChildrenMap() gets the node as a Map<Object, ConfigurationNode> (for arbitrary-name keys).

So using getChildrenList on Rewards.random here–

Rewards{
            random:["12", "reward1", "say Reward1"]
            random:["12", "reward2", "say Reward2"]
}

Using

node.getNode("config","Rewards","random").getChildrenList().stream()
                   .map(ConfigurationNode::getString).collect(Collectors.toList());

would load in List<String> of elements [“12”, “reward1”, “say Reward1”,“12”, “reward2”, “say Reward2”] or would it merge them as [“12”,“reward2”,“say Reward2”].

Just in the initial parsing of the config, they’re merged into one list. Again, this is a standard feature of HOCON. I’m not sure you quite understand.

bork = [
    {
        x = 20
        y = 69
    }
    {
        enable = true
    }
]

If you call getChildrenList() on bork, it will return List(Compound(x = 20, y = 69), Compound(enable = true)).

Conversely, consider this notation:

bork {
    thing1 {
        x = 20
        y = 69
    }
    thing2 {
        enable = true
    }
}

Here, instead of a list of arbitrary objects, it’s a map of arbitrary objects. Thus, calling getChildrenMap() on bork returns Map(thing1 -> Compound(x = 20, y = 69), thing2 -> Compound(enable = true)).

I am using a miscellaneous and arbitrary notation to describe the returned objects, and don’t care.

2 Likes

Alright I think I get it now, Thanks for the explanation, I think that made things a lot clearer.