Solved How can I get all the elements of a config?

Discussion in 'Spigot Plugin Development' started by erikgamer07, Jan 18, 2020.

  1. Hello! I'm making a minigame and I need to register the maps loaded before when the server enables. The config is:

    Code (YAML):
    Players: 10
    Teams
    : 2
    PreLobbyX
    : 4953.701224334189
    PreLobbyY
    : 18.708223388341857
    PreLobbyZ
    : -344.81456390408323
    SignX
    : -50
    SignY
    : 102
    SignZ
    : 309
    Blue
    : 5037.295801518616 12.0 -30.129752261252634 -56.93924 -5.640746
    Green
    : 4959.363014891304 12.0 -28.901576063489767 -301.75427 17.764294
    Red
    : 4960.878999861226 12.0 93.16451151251292 -214.16972 0.7625952
     
    I need to get ALL of the elements you just saw. I have to put them in a arraylist of strings.
    In the arraylist, I need to have the next elements: Teams, PreLobbyX, PreLobbyY, PreLobbyZ, SignX, SignY, SignZ, Blue, Green and Red.

    Does anyone knows how to do it? Thanks!!
     
  2. It would be better if you get all of them and save them in hashmap, with their names as keys.

    As I recall there was something like

    getConfig().getKeys(false);

    your code would look like

    Code (Java):

    private final Map<String, String> values = new HashMap<>();

    public void onEnable(){
    getConfig().getKeys(false).forEach(key -> {
         values.put(key, getConfig().getString(key));
    });
     
    and you would access them by

    values.get("Teams")
    and etc..
     
  3. Code (Java):
    getConfig().getKeys(false)
     
    • Useful Useful x 1
  4. You should probably put quotes around those and then do

    Code (Java):
    File file = new File("plugins/{config folder}", "config.yml");
    FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);

    String players = cfg.getString("Players");
    // Do this for all of the different stuff
    After you do that, you can then do this here

    Code (Java):
    String[] stuff = {players, variable2};
    // Continue on with this for however many variables you have
    You can then access them by doing stuff[0] and so on
     
  5. I would say that this is really BAD practice.
     
  6. Thank you, It worked!