Bukkit - 如何从 YML 文件中读取并在 if 语句中使用

Bukkit - How to read from a YML file and use in an if statements

我想从我的 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");

如果您有任何其他问题,请告诉我!

提示:如果您将玩家姓名存储在列表中,您可以获得他们的列表!

plugin.getConfig().getInt(playerName).