角色系统无法正常工作

时间:2017-03-23 11:40:49

标签: bukkit

因此,我创建了一个命令,以让玩家获得最新的角色"或玩家所在的arraylist,但我有一个问题,即两个消息都被发送给玩家,告诉玩家他是,并且不在角色中。为什么会这样?

命令类:

public class RoleCommand implements CommandExecutor{

private Main main;

public RoleCommand(Main main) {
    this.main = main;

}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

    Player p = (Player) sender;
    if(cmd.getName().equalsIgnoreCase("Role")) {
        if(Roles.innocent.contains(p.getName())) {
            p.sendMessage(Main.prefix + "§cYour current game-role is: §a§lINNOCENT");
        } else {
            p.sendMessage(Main.prefix + "§cYou have not been assigned any role!");

        } 

        if(Roles.traitor.contains(p.getName())) {
            p.sendMessage(Main.prefix + "§cYour current game-role is: §c§lTRAITOR");
        } else {
            p.sendMessage(Main.prefix + "§cYou have not been assigned any role!");
        }

    }
    return true;
    }  
 }

ARRAYLIST CLASS:

public class Roles implements Listener{

private Main main;

public Roles(Main main) {
    this.main = main;

}

public static ArrayList<String> innocent = new ArrayList<String>();
public static ArrayList<String> traitor = new ArrayList<String>();
//private static List<String> detective = new ArrayList<String>();
 }

然后,当玩家加入服务器时,玩家会被分配一个角色。

3 个答案:

答案 0 :(得分:1)

因为它在返回之前评估Roles.innocentRoles.traitor表达式。对于它评估的每个表达式,您将发送一个响应。发送正确的信息后,您需要突破或返回。

答案 1 :(得分:1)

要解决您的问题,您必须正确评估RoleCommand类的条件。 看看:

使用:

,而不是当前的onCommand
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

Player p = (Player) sender;
if(cmd.getName().equalsIgnoreCase("Role")) {
    if(Roles.innocent.contains(p.getName())) {
          p.sendMessage(Main.prefix + "§cYour current game-role is: §a§lINNOCENT");
    } else if (Roles.traitor.contains(p.getName()) {
          p.sendMessage(Main.prefix + "§cYour current game-role is: §c§lTRAITOR");
    } else  {
          p.sendMessage(Main.prefix + "§cYou have not been assigned any role!");
    }


}
return true;
}  

答案 2 :(得分:0)

您遇到的错误是因为您的代码在找到了播放器所处的角色后仍然继续使用。 如果发件人不是Player的实例 - 例如,如果从控制台执行命令,那么会破坏您的代码的另一件事是。此外,此方法始终返回true,这将停止正在执行的其他命令。尝试将onCommand方法更改为以下内容:

public boolean onCommand(CommandSender sender, Command cmd, String lbl, String[] args){
    if(lbl.equalsIgnoreCase("role")){
        if(sender instanceof Player){
            String name = ((Player) sender).getName();
            if(Roles.innocent.contains(name)){
                sender.sendMessage("Current role: Innocent");
            }else if(Roles.traitor.contains(name)){
                sender.sendMessage("Current role: Traitor");
            }else{
                sender.sendMessage("You don't have a role yet");
            }
        }else{
            sender.sendMessage("only players can execute this command");
        }
        return true;
    }
    return false;
}

注意:您希望更改已发送的邮件。