0

所以我正在为 Minecraft 制作一个插件。它基本上是一个团队插件。我需要做的是能够创建多个团队,但我似乎无法弄清楚如何将对象名称更改为玩家选择的团队名称。这就是我的意思:

if (l.equalsIgnoreCase("NewTeam")) {
    teamName= args[0]; // This is the players chosen team name
     Team newTeam = new Team(teamName, sender);
     newTeam.addPlayer(sender);

由于这是一个服务器插件,它必须处理多个团队,这意味着它会创建很多团队对象,但都使用对象名称 newTeam。有谁知道我可以做到这一点的更好方法?谢谢。

4

2 回答 2

1

您正在搜索团队名称到团队对象的映射?然后,你可以这样做:

Map<String,Team> teams = new TreeMap<String,Team>();

//Returns the team for 'teamName' or creates one, if it doesn't exist
public Team getTeam(String teamName)
{
    Team team = teams.get(teamName);
    if(team == null)
    {
        team = new Team(teamName,sender); //is 'sender' specific for a team??
        teams.put(teamName,team);
    }
    return team;
}
于 2013-02-03T13:01:49.333 回答
0

我假设您正在使用 Bukkit API,而且这是在 CommandExecutor 的 onCommand 方法中完成的,所以这应该是这样的:

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
   if(cmd.getName().equalsIgnoreCase("newteam")){ //Make sure this is the right command
      if(args.length == 0) //If the sender hasn't included a name...
         sender.sendMessage("You need to include a team name!"); //Tell them they need to
      else{ //Otherwise...
         Team newTeam = new Team(args[0]); //Create a new team with the designated name
         if(sender instanceof Player) //If the sender is a player (could be console)...
            newTeam.addPlayer((Player) sender); //Add them to the team
         /*
          * Insert some code to save/store the newly created team here
          */
      }
      return true; //Return (for Bukkit's benefit)
   }
}
于 2013-02-05T17:44:53.127 回答