1

How do I load tree/indexes from a YAML file in Bukkit? This is the file I want to grab all the values from:

groups:
  myGroup1:
    prefix: [test]
    permissions:
      - test
  myGroup2:
    prefix: [test2]
    permissions:
      - test2

This YAML file is a configuration, where users can add as many groups as they want so it is impossible to collect things like YamlConfiguration.getString("groups.myGroup1.[..])").

I need to have list of all things in "groups:", so it should look like YamlConfiguration.getString("groups.%groupName%.[..])"). Does someone know how to collect all the things after "groups:" (It can just be the group names) Thanks for the help!

kmecpp
  • 2,371
  • 1
  • 23
  • 38
Microz
  • 97
  • 1
  • 8
  • You can use Jackson to read yml files. search for Jackson && yml && java – Dakoda Jul 28 '17 at 22:10
  • Thanks! Can you give me a small instruction how to load all objects after "groups:"? – Microz Jul 28 '17 at 22:18
  • 2
    Possible duplicate of [Parse a yaml file](https://stackoverflow.com/questions/25796637/parse-a-yaml-file) – maszter Jul 28 '17 at 22:43
  • Since you've tagged your question Bukkit, [this](https://bukkit.gamepedia.com/Configuration_API_Reference) is probably what you are looking for. – bcsb1001 Jul 29 '17 at 00:26

1 Answers1

2

Once you've loaded your YAML file and have an instance of YamlConfiguration you can use getKeys(boolean) to get the list of keys in the current section.

If the parameter is true, then all the keys will be retrieved recursively. If it is false, then it will only get the top level keys. So calling yml.getConfigurationSection("groups").getKeys(false) on your example file would produce the following result:

[myGroup1, myGroup2]

In your case, using that to parse the yaml file would look something like this:

ConfigurationSection section = yml.getConfigurationSection("groups");
for (String group : section.getKeys(false)) {
    List<String> prefixes = section.getStringList("prefix");
    List<String> permissions = section.getStringList("permissions");
}
kmecpp
  • 2,371
  • 1
  • 23
  • 38