Solved Looping trought a yml file

Discussion in 'Spigot Plugin Development' started by anakinsky249, Jun 13, 2017.

  1. Hey, I need to send the user a message in this format:
    Code (Text):
    sender.sendMessage(/*Name - 2nd Name - Level*/);
    I have a .yml file which looks like that:
    Code (Text):
    Main:
      name1:
        scnd: None
      name2:
        scnd: 2ndName
        lvl: 7
    If we follow the format, Name would be equal to "Main.name1", 2nd Name would be equal to "None" and Level isn't specified so I'll count it as level one. What I'd like to do: Have three arrays, one with name, one with 2nd name and one with level. If this works, I'd be able to use that:
    Code (Text):
    for(int i = 0 ; i < namesArray.length ; i++){
        sender.sendMessage( namesArray[i] + " - " + scndArray[i] + " - " + levelArray[i]);

    }

    You get the idea. How can I do that ?


    Thanks.
     
  2. Strahan

    Benefactor

    No, not really lol. If you mean just loop the data and dump it, I'd do it like:

    Code (Text):
    for (String cfg : this.getConfig().getConfigurationSection("Main").getKeys(false)) {
        String scnd  = this.getConfig().getString("Main." + cfg + ".scnd");
        String level = this.getConfig().getString("Main." + cfg + ".lvl");
        sender.sendMessage(cfg + ((scnd == null || scnd.equalsIgnoreCase("none"))?"":" - " + scnd) + " - " + ((level == null)?"1":level));
    }
    If you are using a cache instead of reading the config, substitute as necessary. I don't think I'd use three separate arrays though. Personally, I like having one master object. I'd make a multidimensional hashmap and do something like:

    Code (Text):
    for (Map.Entry<String, Map<String, String>> data : info.entrySet()) {
        String msg = data.getKey();
        msg += ((data.getValue().get("scnd") == null || data.getValue().get("scnd").equalsIgnoreCase("none"))?"":" - " + data.getValue().get("scnd"));
        msg += " - " + ((data.getValue().get("level")== null)?"1":data.getValue().get("level"));
        sender.sendMessage(msg);
    }
    Bear in mind, I'm a Java beginner though so take my comments with a grain of salt.
     
  3. Code (Text):
    YamlConfiguration#getConfigurationSection("main").getKeys(true)
    Loop through that and, iirc, you should be able to get the values of the keys in the desired sections.
     
  4. Just needed some modifications to make it work! Thanks.