1.15.2 How can I make a list of any size in the config?

Discussion in 'Spigot Plugin Development' started by HoiTemmieColeg, Feb 2, 2020.

  1. Hello to anyone reading this! I have a simple issue that I would like to solve, and if anyone could help me out it would be awesome.

    Basically I'm going to have a config that either looks like this:
    Code (YAML):
    players:
        gavinepic123
    : red
        beastman777
    : blue
        imtoolazytomakeupanothername
    : red
    or like this:
    Code (YAML):
    players:
        red
    :
            - gavinepic123
            - imtoolazytomakeupanothername
        blue
    :
           - beastman777
    Every time the server starts up I will use that list to assign each player to a team. I have no issue adding new players to this list, but my issue is that I need to figure out how to get each player from this list and run the code to add them to a team.

    If anyone could help me out, it would be awesome. If you need me to elaborate, I will be happy too. Just explain what you want me to elaborate on.
     
  2. You could store all of them in a List (Set) like this:
    Set<String> colors = yourConfig.getConfigurationSection("players").getKeys(false);
    and then loop through them, and store the players in a List and do whatever
    Code (Java):
    for(String color : colors) {
        List<String> players = config.getStringList("yourpath." + color);
        //do whatever
    }
     
  3. the method you are looking for is something like ConfigurationSection.getKeys(false). This will give you all the keys under that section so that you can do something like
    for(String key : myConfigSection.getKeys(false)) {
    myConfigSection.get(key);
    //do something
    }