Solved [Config]Get keys that are inside a directory that is inside a section

Discussion in 'Plugin Development' started by xX4w3s0m3Xx, Jul 26, 2017.

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

    xX4w3s0m3Xx

    I had no idea how to make this a title.
    I will try to explain as clear as possible what I am trying to achieve.
    So I got this config file:
    Code:
    Hunts:
      a:
        '0':
          Location: 80/67/234/world
          ActivatedBy: []
        '1':
          Location: 80/67/232/world
          ActivatedBy: []
      b:
        '0':
          Location: 80/67/230/world
          ActivatedBy: []
        '1':
          Location: 80/67/232/world
          ActivatedBy: []
        '2':
          Location: 80/67/234/world
          ActivatedBy: []
        '3':
          Location: 80/67/228/world
          ActivatedBy: []
    Now I am trying to return the numbers (in a: "0, 1" and in b: "0, 1, 2, 3") with also knowing what their "parents" are(so a and b). I tried this:
    Code:
    for (String hunt: plugin.getConfig().getConfigurationSection("Hunts").getKeys(false)) {
                System.out.println(hunt);
            }
    and that outputs the a and b, but I also want to output the numbers, so I tried this:
    Code:
    for (String hunt: plugin.getConfig().getConfigurationSection("Hunts").getKeys(false)) {
                List<String> test = plugin.getConfig().;
                System.out.println(test);
            }
    But that prints [] 2 times. Is there someone that can brighten this up for me?
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    xX4w3s0m3Xx

  4. Offline

    timtower Administrator Administrator Moderator

    @xX4w3s0m3Xx
    Code:
    List<String> test = plugin.getConfig().;
    Talking about the code, that is a non functional line.
     
  5. Offline

    xX4w3s0m3Xx

    @timtower why is it non functional? Is that assignment not possible?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @xX4w3s0m3Xx It ends with a dot, there needs to be a function after that, or some sort of call.
    Or at least a class cast from some sort of configuration class to List.
     
  7. Offline

    Zombie_Striker

    @xX4w3s0m3Xx
    You have a period, but you are not calling any methods. This would not compile.

    Either way, to get the the numbers as well, for each "Hunt" key, loop through the keys of those paths (The "Hunt."+key paths)
     
  8. Offline

    Machine Maker

    @xX4w3s0m3Xx
    I would use nested for loops and loop through the keys a and b, and then in that for loop, loop through the keys there.
     
  9. Offline

    xX4w3s0m3Xx

    @timtower I think I had getList(hunt); because I thought I had to get all the numbers from each hunt like that.

    @Zombie_Striker and I will try what you said tomorrow
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    xX4w3s0m3Xx

  12. Offline

    timtower Administrator Administrator Moderator

    @xX4w3s0m3Xx The letters are keys, but the numbers are keys as well.
    Keys as in key value pair (which yml is)
     
  13. Offline

    xX4w3s0m3Xx

    @timtower Thank you for explaining! So in your previous post you meant that I need the keys in order to get the values?
     
  14. Offline

    Machine Maker

    @xX4w3s0m3Xx
    try this
    Code:
    HashMap<String, List<String>> configKeys = new HashMap<String, List<String>>();
    for (String letter : plugin.getConfig().getConfigurationSection("Hunts").getKeys(false) { //gives 'a' and 'b'
        configKeys.put(letter, plugin.getConfig().getConfigurationSection("Hunts." + letter).getKeys(false);
    }
    
    This will create a hash map that looks like this:
    {"a": [ "0", "1"], b: ["0", "1", "2", "3"]}
     
  15. Offline

    xX4w3s0m3Xx

    @X1machinemaker1X
    Thank you very much, although that code has some errors, I got it working, because of the "getConfigurationSection" and "getKeys(false)". This is what I did:
    Code:
    public static void load() {
                for (String huntName : plugin.getConfig().getConfigurationSection("Hunts").getKeys(false)) {
                    System.out.println(huntName);
                    for (String pressurePlateNumber : plugin.getConfig().getConfigurationSection("Hunts." + huntName).getKeys(false)) {
                        System.out.println(pressurePlateNumber);
                        System.out.println(plugin.getConfig().getString("Hunts." + huntName + "." + pressurePlateNumber + ".Location"));
                        System.out.println(plugin.getConfig().getList("Hunts." + huntName + "." + pressurePlateNumber + ".ActivatedBy"));
                    }
                }
        }
     
  16. Offline

    Machine Maker

    @xX4w3s0m3Xx
    I'm glad you were able to figure it out.

    Please mark this thread as Solved if your issue has been resolved.
     
Thread Status:
Not open for further replies.

Share This Page