Getting color from config for multiple keys

Discussion in 'Plugin Development' started by xDeeKay, Feb 7, 2015.

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

    xDeeKay

    What I'm trying to achieve here is pretty specific, so I haven't been able to find much info about it.
    I'm fairly comfortable with file configuration, but this is causing me problems.

    This is what my custom config looks like:
    Code:
    worlds:
      normal:
        world1:
          name: world1
          color: RED
          hidden: false
          type: FLAT
          border: 20
          spawn:
            x: 0.0
            y: 4.0
            z: 0.0
            yaw: 180.0
            pitch: 0.0
        world2:
          name: world2
          color: GREEN
          hidden: false
          type: FLAT
          border: 20
          spawn:
            x: 0.0
            y: 4.0
            z: 0.0
            yaw: 180.0
            pitch: 0.0
    On a command, I need to list all world names from this config, but also displayed in the respected color defined under the "color:" key for that world. For example, /worlds would give me: world1, world2

    Here's what I've got so far:
    Code:
                    File worldFile = new File(Bukkit.getPluginManager().getPlugin("Opticore").getDataFolder(), "worlds.yml");
                    FileConfiguration worldConfig = null;
                    worldConfig = YamlConfiguration.loadConfiguration(worldFile);
    
                    if (worldConfig.getStringList("worlds.normal") != null) {
                        StringBuilder sbNormal = new StringBuilder();
                        Set<String> normalWorlds = worldConfig.getConfigurationSection("worlds.normal").getKeys(false);
                        for (String list : normalWorlds) {
                            sbNormal.append(list).append(", ");
                        }
                        String color = worldConfig.getString("worlds.normal." + ??? + ".color");
                        cs.sendMessage(ChatColor.GREEN + "Normal:");
                        cs.sendMessage(ChatColor.valueOf(color) + sbNormal.toString().substring(0, sbNormal.length() - 2));
                    }
     
  2. Offline

    1Rogue

    A thing to note in yaml:

    Code:
    worlds:
      normal:
        world1:
        world2:
        worldbanana:
        catworld:
    worlds.normal is not a list, nor an array, it is an object mapping. In java this is typically represented by a Map<String, Object> (As a ConfigurationSection is backed by).

    The method you are looking for is ConfigurationSection#geyKeys(false)
     
  3. Offline

    xDeeKay

    @1Rogue Makes sense. thanks for the insight. Is this method you linked for the whole colour situation, or for the object mapping?
     
  4. Offline

    1Rogue

    If you get the keys for "world.normal" it will return a List of strings from the section you can iterate over e.g. "world1", "world2".
     
  5. Offline

    xDeeKay

    @1Rogue Right, I've already got it listing the worlds, but I need to get the colours for each world and display them in said colour. Or is that what you mean?
     
  6. Offline

    1Rogue

    Right, so you can use those keys to find <your-key>.color and apply it accordingly.
     
    ZanderMan9 likes this.
  7. Offline

    xDeeKay

    @1Rogue Gotcha, I'll give it a shot.
     
Thread Status:
Not open for further replies.

Share This Page