Search code examples
javaconfigbukkit

Java config, need return function7


I have a config.yml file like this below: How do I write a function that returns spawn, lol, xd, Warpa, Warphei? I know that for example if I want to get X inside spawn I can do this plugin.getConfig().getString("spawn.X"); I just don't know how to list all of the names.

spawn:
  World: world
  X: 80.96770114181192
  Y: 72.0
  Z: -377.6790770077272
  Pitch: 14.682037
  Yaw: 113.62133
lol:
  World: world
  X: 109.60479547630788
  Y: 71.0
  Z: -353.9986813646272
  Pitch: 2.6992812
  Yaw: 102.31512
xd:
  World: world
  X: 106.59929856356823
  Y: 71.0
  Z: -354.65178849406584
  Pitch: 2.6992812
  Yaw: 102.31512
Warpa:
  World: world
  X: 188.43198209818965
  Y: 70.5998614967549
  Z: -331.69999998807907
  Pitch: 15.525846
  Yaw: -129.82843
Warphei:
  World: world
  X: 190.8221960506558
  Y: 70.5998614967549
  Z: -341.26133473481616
  Pitch: 15.525846
  Yaw: -129.82843

Solution

  • I'm assuming config.yml is already available on the server. As you know, you can get the FileConfiguration instance of the default config by calling JavaPlugin#getConfig().

    To get all elements in a configuration section, you can call ConfigurationSection#getKeys(boolean deep). As FileConfiguration implements ConfigurationSection, you can call the getkeys method on your FileConfiguration instance. The boolean indicates whether you want to get all deep paths as well.

    Examples:

    FileConfiguration#getKeys(false)

    • "spawn"
    • "lol"
    • "xd"
    • ...

    FileConfiguration#getKeys(true)

    • "spawn"
    • "spawn.World"
    • "spawn.X"
    • ...
    • "lol"
    • "lol.World"
    • ...