我想从我的 config.yml 中读取数据。它保存像(playerName:value)这样的数据。我想检查一下,如果玩家的姓名值等于0,则禁止他。但仍然不知道如何读取这些值。我在 spigot 论坛上搜索,但没有任何效果。
@EventHandler
public void OnDeath(PlayerDeathEvent event) {
Player player = event.getEntity().getPlayer();
String playerName = player.getName();
int lives;
if (!livesMap.containsKey(player)) {
// Set the default amount of lives to 2. (3 minus 1, since the player already died once)
lives = 2;
plugin.getConfig().set(playerName, lives);
plugin.saveConfig();
} else {
// Subtract one from the player's lives
lives = livesMap.get(player) - 1;
// Saving playerName and lives
plugin.getConfig().set(playerName, lives);
plugin.saveConfig();
}
livesMap.put(player, lives);
我的数据是这样保存的,但我想知道我应该使用什么方法来读取值。
最佳答案
幸运的是,bukkit 已经有可以用于此目的的类!
import org.bukkit.configuration.file.FileConfiguration;
FileConfiguration config = getConfig();
此调用本质上会加载您的 config.yml 文件。
然后您可以通过执行以下操作来访问值:
config.getString("yml.object.here");
如果您还有其他问题,请告诉我!
提示:如果您将玩家姓名存储在列表中,您可以获得他们的列表!
关于java - Bukkit - 如何从 YML 文件读取并在 if 语句中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59179613/