Search code examples
javayamlconfigbukkit

Read unknown data from yml configuration file


I'm writing a bukkit plugin which should read data from a yml configuration file made by users like shown in the example below. I've searched for that before with no result, in fact I don't really know what I should search for.

Example config:

maps:
  01:
    name: world
    displayname: Plains
    icon: 2
    size: 1000
    info:
    - 'Some information'
    - 'Another information string'
  02:
    name: world_nether
    displayname: Nether
    icon: 87
    size: 500
    info:
    - 'Information'
    - 'More information'
    - 'As many lines as needed...'

The number of maps is unknown, the user can create as many as he wants, so there are not only 01 and 02, the content (name, icon, info,...) is always the same.

How can I read out the number of maps (01, 02,...) and what is a good way to save the different types of content (strings, integers, lists) in Java?


Solution

  • You can get a Set<String> of the children.

    See ConfigurationSection, FileConfiguration

    FileConfiguration fc = getConfig(); // Get the config
    ConfigurationSection cs = fc.getConfigurationSection("maps");
    boolean deep = false; 
    for (String key : cs.getKeys(deep)) {
        //Key will be 01
    }