Solved Alternating Maps?

Discussion in 'Plugin Development' started by chasechocolate, Dec 31, 2012.

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

    chasechocolate

    Hi, I have my maps stored in a config.yml, and it looks somewhat like this:
    Code:
    map1:
      x: 342
      y: -135
      z: 604
    map2:
      x: -452
      y: -834
      z: 362
    map3:
      x: 738
      y: -244
      z: 147
    How in my plugin would I choose a random map? I have:
    Code:java
    1. Random random = new Random();
    2. int mapNum = random.nextInt(/* how would I get the length of the maps in the config? */);
     
  2. Offline

    ImDeJay

    you could do this i suppose

    *note*
    may not be the best way to do this.

    in your config do:

    Code:
    maps:
      map1:
        x: 342
        y: -135
        z: 604
      map2:
        x: -452
        y: -834
        z: 362
      map3:
        x: 738
        y: -244
        z: 147
    then in code you can do.

    Code:
    ConfigurationSection mapSection = getConfig().getConfigurationSection("maps");
     
    int mapCount = 0;
     
    for(String maps : mapSection.getKeys(false)) {
     
    mapCount++;
     
    }
     
    System.out.println(mapCount);
     
    chasechocolate likes this.
  3. Offline

    fireblast709

    Code:java
    1. Random r = new Random();
    2. int mapNumber = r.nextInt(getConfig().getKeys(false).size())+1;
    3. ConfigurationSection map = getConfig().getConfigurationSection("map"+mapNumber);
    4. int x = map.getInt("x");
    5. int y = map.getInt("y");
    6. int z = map.getInt("z");
     
    chasechocolate and suckycomedian like this.
  4. Offline

    chasechocolate

    Ok, thanks ImDeJay and fireblast709 for the helpful replies!

    Oh, just a quick question fireblast709 how would I do the random maps if I wanted my maps to have a specified name? Example:
    Code:
    maps:
      windmill:
        x: 342
        y: -135
        z: 604
      warehouse:
        x: -452
        y: -834
        z: 362
      office:
        x: 738
        y: -244
        z: 147
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  5. Offline

    fireblast709

    Code:java
    1. Set<String> maps = getConfig().getConfigurationSection("maps").getKeys(false);
    2. int r = new Random().nextInt(maps.size());
    3. String map = maps.toArray(new String[0])[r];
     
    chasechocolate likes this.
  6. Offline

    chasechocolate

    Cool! Thanks :) Just a quick question, what does the .getKeys(false) do at the end?
     
  7. Offline

    slater96

Thread Status:
Not open for further replies.

Share This Page