Config.yml file list of objects ?

Discussion in 'Plugin Development' started by Andr3Ws, Jan 18, 2019.

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

    Andr3Ws

    Hi, I'm new into plugin development and I have a problem with the config.yml file! I've created a list of list?

    Example :

    Mobs:
    - 'mob1':
    name: 'Zombie'
    souls_gives: '10'
    - 'mob2':
    name: 'Skeleton'
    souls_gives: '20'

    how can I get the value name ? I first tried to create :

    List<List> temp = (List<List>) plugin.getConfig().getList("Mobs");

    and then to get the value I tried :

    temp.get(0).get(0);

    but it throw an error null, then I also tried :

    List<Object> temp = (List<Object>) plugin.getConfig().getList("Mobs");
    temp.get(0);
    and it show {mob1={name=Zombie, souls_gives=20}}
    how i access this data ?

    Sorry for my English, and I really appreciate any kind of help!
     
  2. @Andr3Ws

    I think it will be easier to use configuration sections instead of lists.

    The config should have methods like getSection or getConfigurationSection and createSection or createConfigurationSection. (I don't remember which one.)

    I suggest you create the example config programmatically so that you can't make annoying syntax errors.

    Code:
    FileConfiguration config = getConfig();
    ConfigurationSection mobsSection = config.createSection("Mobs");
    ConfigurationSection zombieSection = mobsSection.createSection("mob1");
    zombieSection.set("name", "Zombie");
    // you should write the rest
    saveConfig();
    You could put the code above in the beginning of the onEnable for now. (Move it to a better place later.)

    For the end of the onEnable, use code like this:
    Code:
    FileConfiguration config = getConfig();
    ConfigurationSection mobsSection = config.getConfigurationSection("Mobs");
    Set<String> mobKeys = mobsSection.getKeys(false);
    for (String mobKey : mobKeys){
    ConfigurationSection currentMobSection = mobsSection.getConfigurationSection(mobKey);
    String name = currentMobSection.getString("name");
    // just an example
    }
    My code is probably not completely correct, but it should give you an idea what to do. (I only give you code because this is related to config rather than basic programming.)
     
    Andr3Ws likes this.
  3. Offline

    Andr3Ws

    thank you !! It works !
     
Thread Status:
Not open for further replies.

Share This Page