[YAML] Select configuration section based on position, as oppose to key?

Discussion in 'Plugin Development' started by RastaLulz, Apr 3, 2014.

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

    RastaLulz

    Hello,

    I'm working on a plugin that'll be able to run different game modules. With that being said, I need to be able get spawn points in chronological order, so that I can switch from one to the next. An example of this would be survival games, where there's multiple points where users have to spawn, and you'd go chronologically through the points, and you'd loop through the points again if there were more players than points.

    So this is what my (YAML) config file would look like:
    Code:
    spawns:
      players:
        locations:
          point:
            xyz: "-505.5,40,-117.5"
            yaw: 60
          point:
            xyz: "-506.5,40,-117.5"
            yaw: 100
          point:
            xyz: "-507.5,40,-117.5"
            yaw: 130
          point:
            xyz: "-508.5,40,-117.5"
            yaw: 180
    There'd also be room for other spawn types, like a cuboid:
    Code:
    spawns:
    players:
        locations:
          point:
            xyz: "-505.5,40,-117.5"
            yaw: 60
          cuboid:
            min: "-506.5,40,-117.5"
            max: "-509.5,40,-120.5"
            yaw: 100
    So, long story short, I'd keep track of the total number of spawns, the position of the last spawn that was used, and then move to the next one. So, is there any way you can select a configuration section based on the position it is within the group, as opposed to the specific key?

    By that I mean I'd like to select "spawns.locations.2", because a player just used the first location, assuming it starts at 1, and not 0.

    I suppose I could list them in numerical order, and list spawn type within the configuration section, but I'd prefer to do it this way if possible.

    An example of the code would be:
    Code:java
    1. public Integer totalSpawns, currentSpawnPos;
    2.  
    3. public void initSpawns() {
    4. this.totalSpawns = MapConfig.getConfig().getConfigurationSection("spawns.players.locations").getKeys(false).size();
    5. this.currentSpawnPos = 0;
    6. }
    7.  
    8. public ConfigurationSection getNextSpawn() {
    9. this.currentSpawnPos = (this.currentSpawnPos >= this.totalSpawns ? 0 : this.currentSpawnPos + 1);
    10. return MapConfig.getConfig().getConfigurationSection("spawns.locations." + this.currentSpawnPos);
    11. }
     
Thread Status:
Not open for further replies.

Share This Page