英文:
Java config, need return function7
问题
我有一个类似下面这样的 config.yml 文件:
如何编写一个函数,返回 spawn、lol、xd、Warpa、Warphei?
我知道,例如,如果我想获取 spawn 内部的 X,我可以这样做 plugin.getConfig().getString("spawn.X");
我只是不知道如何列出所有的名称。
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
英文:
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
答案1
得分: 1
我假设 config.yml 在服务器上已经可用。如你所知,你可以通过调用 JavaPlugin#getConfig()
来获得默认配置的 FileConfiguration
实例。
要获取配置部分中的所有元素,可以调用 ConfigurationSection#getKeys(boolean deep)
方法。由于 FileConfiguration
实现了 ConfigurationSection
接口,因此可以在你的 FileConfiguration
实例上调用 getKeys 方法。布尔值表示是否希望获取所有深层路径。
示例:
FileConfiguration#getKeys(false)
- "spawn"
- "lol"
- "xd"
- ...
FileConfiguration#getKeys(true)
- "spawn"
- "spawn.World"
- "spawn.X"
- ...
- "lol"
- "lol.World"
- ...
英文:
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"
- ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论