因此,我正在对服务器执行kick / ban命令,格式为/ kick名称。
我得到了所有的帮助,但是reason
只能是1个字,任何超出此范围的单词都可以排除,例如/kick BattleDash hello world
会说BattleDash被踢给hello
。
这是我的代码:
if (cmd.getName().equalsIgnoreCase("kick") && sender instanceof Player) {
Player player = (Player) sender;
int length = args.length;
if (length >= 2) {
boolean playerFound = false;
for (Player playerToKick : Bukkit.getServer().getOnlinePlayers()) {
if(playerToKick.getName().equalsIgnoreCase(args[0])) {
playerToKick.kickPlayer(ChatColor.RED + "Kicked by Administrator " + player.getName() + "\nReason: " + args[1]);
player.sendMessage(ChatColor.RED + "[BATTLEDASHLOGS]: Kicked player " + playerToKick.getName() + "succesfully!");
break;
}
}
if (playerFound == false) {
player.sendMessage(ChatColor.RED + "[BATTLEDASHLOGS]: " + args[0] + " was not found!");
}
} else player.sendMessage(ChatColor.RED + "[BATTLEDASHLOGS]: Incorrect arguments!" + ChatColor.RED + " /kick <PlayerName> <reason>");
return true;
}
(此外,如果您未提供原因,则聊天中会出现内部错误,并且控制台中也会发生异常)
答案 0 :(得分:3)
由于代码的编程方式,您的代码正在执行此操作。
如果我们看一下带有参数的行:
playerToKick.kickPlayer(ChatColor.RED + "Kicked by Administrator " + player.getName() + "\nReason: " + args[1]);
我们可以看到您仅使用args[1]
。如果我们将消息视为数组,则问题将很明显:
在位置0(记住数组从0开始):BattleDash
1:你好
2:世界
当您仅在邮件中使用args[1]
时,将仅使用hello
!我们需要做的是使用所有参数。一种方法是这样的:
// First, transform your array into a list, so it's easier to understand
List<String> arguments = new ArrayList<>(args);
String playerName = arguments.get(0); // Get the player name
arguments.remove(0); // Remove the player name from our arguments list, we don't want it in the message
String message = String.join(" ", arguments); // Combine all the other arguments into a message