在某些情况下,HashMap显示为空

时间:2015-10-28 15:14:36

标签: java bukkit

我正在为Minecraft服务器软件Bukkit开发一个插件。有一个类处理用户输入的命令。我想允许用户创建和加入现有团队。团队保存在HashMap中。问题是它在一个函数中工作得很好(/ createteam),而Map在/ jointeam中显示为空。如果我再次调用/ createteam,则条目仍然存在,因此它们不会被意外删除。这是我的代码:

package de.hoffmann.mcwar;

import java.util.HashMap;
...
import org.bukkit.entity.Player;

public class MCWarCommandExecutor implements CommandExecutor {

private final MCWar plugin;

public Map<String, MCWarTeam> teamList = new HashMap<>();

public MCWarCommandExecutor(MCWar plugin) {
    this.plugin = plugin; // Store the plugin in situations where you need
                            // it.
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    String playerName = sender.getName();

    if (cmd.getName().equalsIgnoreCase("launch")) {
        if (!(sender instanceof Player)) {
            sender.sendMessage("This command can only be run by a player.");
        } else {
            Player player = (Player) sender;
            if (args.length != 1) {
                return false;
            }

            int height = Integer.parseInt(args[0]);
            Location playerLocation = player.getLocation();

            Location newLocation = playerLocation;
            newLocation.setY(newLocation.getY() + height);

            String message = "Launching you up to height " + newLocation.getY();
            player.sendMessage(message);
            player.teleport(newLocation);

        }
        return true;
    }

    else if (cmd.getName().equalsIgnoreCase("createTeam")) {
        sender.sendMessage("createTeam was called! Team list:");
        for (String key : teamList.keySet()) {
            sender.sendMessage(key);
        }
        if (args.length > 0 && args.length < 3) {
            String joinKey = "";
            if (args.length > 1)
                joinKey = args[1];

            String teamName = args[0];

            MCWarTeam newTeam = new MCWarTeam(teamName, joinKey);
            if (!teamList.containsKey(teamName)) {
                teamList.put(teamName, newTeam);
                sender.sendMessage("Created new team \"" + teamName + "\" with join key \"" + joinKey + "\" successfully! Teams:");

                sender.sendMessage("All teams:");
                for (String key : teamList.keySet()) {
                    sender.sendMessage(key);
                }

            } else
                sender.sendMessage("Team already exists!");
            return true;
        }
        return false;
    }

    else if (cmd.getName().equalsIgnoreCase("joinTeam")) {
        sender.sendMessage("joinTeam was called! Team list:");
        for (String key : teamList.keySet()) {
            sender.sendMessage(key);
        }

        if (args.length > 0 && args.length < 3) {
            String joinKey = "";
            if (args.length > 1)
                joinKey = args[1];

            String teamName = args[0];

            if (teamList.containsKey(teamName)) {
                String teamKey = teamList.get(teamName).getJoinKey();
                if (joinKey == teamKey) {
                    teamList.get(teamName).addPlayer(playerName);
                    Bukkit.broadcastMessage("MCWar: " + playerName + " joined Team \"" + teamName + "\" successfully!");
                } else
                    sender.sendMessage("Join key incorrect!");
            } else {
                sender.sendMessage("Team doesn't exist! Teams:");
                for (String key : teamList.keySet()) {
                    sender.sendMessage(key);
                }

            }
            return true;
        }
        return false;
    }

    // else if (cmd.getName().equalsIgnoreCase("leaveTeam")) {
    //
    // }
    //
    // else if (cmd.getName().equalsIgnoreCase("ready")) {
    //
    // }
    //
    // else if (cmd.getName().equalsIgnoreCase("notready")) {
    //
    // }
    //
    // else if (cmd.getName().equalsIgnoreCase("surrender")) {
    //
    // }
    //
    // else if (cmd.getName().equalsIgnoreCase("spectate")) {
    //
    // }
    //
    // else if (cmd.getName().equalsIgnoreCase("setspawn")) {
    //
    // }
    //
    // else if (cmd.getName().equalsIgnoreCase("kit")) {
    //
    // }
    //
    // else if (cmd.getName().equalsIgnoreCase("showkits")) {
    //
    // }

    return false;
}
}

格式有点偏,所以我也把它放在这里:http://pastebin.com/zmdqFa3u

我尝试更改有问题的两个功能的位置,但它没有帮助。我在两个函数的开头调用一个循环,它应该打印teamList的所有元素。它仅适用于createTeam tho,没有在joinTeam中显示相同的循环。我担心这个问题可能是Bukkit如何存储数据的问题。

1 个答案:

答案 0 :(得分:2)

用Layman的术语解释这一点,在注册命令时,你可能会这样做:

@Override
public void onEnable()
{
    getCommand("foo").setExecutor(new Bar());
    getCommand("baz").setExecutor(new Bar());
}

如果是这种情况,则为每个命令创建这些类的单独实例,这意味着您还有单独的Map实例,这意味着它们的内容不同。

制作地图static或更改注册命令的方式:

Bar bar = new Bar();
getCommand("foo").setExecutor(bar);
getCommand("baz").setExecutor(bar);

请记住,这仅适用于注册顶部代码段中显示的命令。