Reading configuration file

Discussion in 'Spigot Plugin Development' started by spinnekoppie, May 20, 2016.

  1. I am trying to get the coordinates for my spawn plains from the configuration file. This is the content of my configuration file:
    Code (Text):

    # SPAWNPLAIN CENTRES
    spawnplaincenters:
      'Durga':
      Spawn:
        X:925
        Y:63
        Z:1066
      'Wasteland':
      Spawn:
        X:925
        Y:63
        Z:1066
     
    This is my code:
    Code (Text):

        ConfigurationSection configuration = ScavengerPlugin.plugin.getConfig();
         for (String key : configuration.getConfigurationSection("spawnplaincenters").getKeys(false)) {
           ConfigurationSection arenaSection = configuration.getConfigurationSection("spawnplaincenters." + key);
           int x = arenaSection.getInt("Spawn.X");
           Bukkit.getLogger().info(key + ": " + x);

         }
     
    The logger prints:
    Code (Text):

    [17:59:11 INFO]: Durga: 0
    [17:59:11 INFO]: Wasteland: 0
     
    I expect the x coordinate value to be printed. Am I missing or misunderstanding something here?
     
  2. In the getConfiugrationSection.getKeys(false) you tell it to go into the spawnplaincenters and get every key only inside that section, not subsections etc.. Also why are you having 4 keys on same row but with identifiers? It should be 'Durga'.Spawn.X etc, instead of 'Durga' Spawn.X
    If you want keys use getConfigurationSection from Spawn, but you have 2 copies on same row so get that fixed.
     
  3. I searched online to find out how to read the config file and my code is based on https://bukkit.org/threads/getting-a-list-of-arenas-from-config.343032/.

    What do you mean by "4 keys on the same row with identifiers". I'm not sure of the terminology here. Which are the keys and which are the identifiers.
     
  4. Code (Text):
    ConfigurationSection section = getConfig().getConfigurationSection("spawnplaincenters");
      for (String key : section.getKeys(false)) {
        System.out.println(key + ", x: " + getConfig().getInt("spawnplaincenters." + key + ".X"));
        System.out.println(key + ", y: " + getConfig().getInt("spawnplaincenters." + key + ".Y"));
        System.out.println(key + ", z: " + getConfig().getInt("spawnplaincenters." + key + ".Z"));;
        System.out.println(" ");
    }
    EDIT:
    It's quite simple... You can also use
    Code (Text):
    String key : section.getKeys(true) {
      //...
    }
    to get any children keys as, but if the format is the same for all the keys you're looping through I suggest just going with the
    Code (Text):
    String key : section.getKeys(false) {
      //... then finding it
    }
     
  5. I still can't get it to work and I can't see what I'm doing wrong. I now have this:
    # SPAWNPLAIN CENTRES
    Code (Text):

    spawnplaincenters:
      Durga:
        X:925
        Y:63
        Z:1066
      Wasteland:
        X:925
        Y:63
        Z:1066
     
    and
    Code (Text):

        // loading spawnplains
         ConfigurationSection spawnsection = getConfig().getConfigurationSection("spawnplaincenters");
         for (String key : spawnsection.getKeys(false)) {
           int x = spawnsection.getInt("spawnplaincenters." + key + ".X");
           int y = spawnsection.getInt("spawnplaincenters." + key + ".Y");
           int z = spawnsection.getInt("spawnplaincenters." + key + ".Z");
           Bukkit.getLogger().info(key + ": " + x + " " + y + " " + z);

         }
     
    I also tried a few variations on the above but with no difference to the outcome. I obviously don't quite understand how it works and would appreciate it if someone can give some kind of explanation.

    I guess we first load the spawnplaincenters into spawnsection. But what exactly does spawnsection then contain? Does it contain the whole tree defined below spawnplaincenters?

    I also tried:
    Code (Text):

           int x = spawnsection.getInt("spawnplaincenters." + key + ".X");
     
    and
    Code (Text):

           int x = getConfig().getInt("spawnplaincenters." + key + ".X");
     
    And now I am at a loss ...
     
  6. Try doing
    Code (Text):
    ConfigurationSection spawnsection = getConfig().getConfigurationSection("spawnplaincenters");
    for (String key : spawnsection.getKeys(false)) {
      System.out.println(s);
    }
    To see if your keys even exist or is correctly formed. If there's an error in console, please show it to me.
     
  7. I guess you meant:
    Code (Text):

    for (String key : spawnsection.getKeys(false)) {
      System.out.println(key);
    }
     
    ... as there is no s. If that is what you meant then I can tell you that it finds the section keys because the line
    Code (Text):

    Bukkit.getLogger().info(key + ": " + x + " " + y + " " + z);
     
    in my code produced the following in the console:
    Code (Text):

    [22:50:39 INFO]: Durga: 0 0 0
    [22:50:39 INFO]: Wasteland: 0 0 0
     
     
  8. That means that the value of X Y and Z is 0. You've done something wrong in the config. try using space. Y: 22 instead of Y:22
     
  9. ... and that did it! Thank you so much but I now feel like banging my head again the wall!! This took me forever to find!
     
  10. Let me compare the 2 configs from the link you provided to the config you had:
    Code (Text):
    Arenas:
      '1':
        Spawn: // Notice that this is UNDER '1'
          X: -20.51898431238692
          Y: 4.0
          Z: -3.534145837262486
          World: world
      '2':
        Spawn:// Notice that this is UNDER '2'
          X: -12.4425497847055
          Y: 4.0
          Z: 0.513421916749869
          World: world
      '3':// Notice that this is UNDER '3'
        Spawn:
          X: -12.222858275141215
          Y: 4.0
          Z: -6.603315924513339
          World: world
    That was the correct one, now yours:
    Code (Text):
    spawnplaincenters:
      'Durga':
      Spawn: // Notice that this is NOT under 'Durga'
        X:925
        Y:63
        Z:1066
      'Wasteland':
      Spawn:// Notice that this is NOT under 'Wasteland'
        X:925
        Y:63
        Z:1066
    As you can see, Durga and Wasteland are just there, they have no children sections because they are on same level as spawn. I see you figured out how to do it so glad you did, this is just what i meant about 4 keys at same level.
     
  11. They weren't really on the same level. That was an artifact of this editor, some of my spaces disappeared when I cut and paste and I didn't notice. However, I removed Spawn and just ended up with this working. The problem was the fact that there wasn't a space between X: and its coordinate:

    Code (Text):

    # SPAWNPLAIN CENTRES
    spawnplaincenters:
      Durga:
        X: 925
        Y: 63
        Z: 1066
      Wasteland:
        X: 925
        Y: 63
        Z: 1066

     
    Code (Text):

        ConfigurationSection spawnsection = getConfig().getConfigurationSection("spawnplaincenters");
         for (String key : spawnsection.getKeys(false)) {
           int x = getConfig().getInt("spawnplaincenters." + key + ".X");
           int y = getConfig().getInt("spawnplaincenters." + key + ".Y");
           int z = getConfig().getInt("spawnplaincenters." + key + ".Z");
           spawnplains.put(point, new SpawnPlains(key, x, y, z));
           Bukkit.getLogger().info(key + ": " + x + " " + y + " " + z);
         }
     
     
    • Like Like x 1