Solved How to add a subgroup to config.yml

Discussion in 'Plugin Development' started by yuanjv, Feb 22, 2022.

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

    yuanjv

    this is my original code:

    String w = e.getFrom().getName();
    String p = String.valueOf(e.getPlayer().getUniqueId());
    List<String> newW= new ArrayList<>();
    newW.add(w);
    configuration.addDefault(p,newW);

    the result is:

    (uuid):
    - (world 1)
    - (world 2)
    ...


    What I wanted is:

    (uuid):
    worlds:
    - (world 1)
    - (world 2)
    ...

    How to use plugin.getConfig() to create the "worlds" subgroup?

    List<String> newW= new ArrayList<>();
    newW.add(w);configuration.createSection(p);
    configuration.set(p+".worlds",newW);


    I sovled it myself...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 22, 2022
  2. Offline

    Strahan

    For future reference, you don't really need to manually create sections. It will do that automatically. Also if you want to save some typing, you can call toString() on getUniqueId() to return a String.

    Also be aware if you are just creating the list and adding a single String to save, it will overwrite any data in the List that is in config when you save. Be sure to read the List in first before you add/save.
     
Thread Status:
Not open for further replies.

Share This Page