简体   繁体   English

Bukkit插件踢原因仅使用第一个单词

[英]Bukkit plugin kick reason only using first word

So I'm making a kick/ban command for my server and the format is /kick name reason. 因此,我正在为服务器创建一个kick / ban命令,格式为/ kick名称。 I got it all working but the reason can only be 1 word and anything over that excludes it, for example /kick BattleDash hello world would say BattleDash was kicked for hello . 我知道这一切正常,但reason只能是1个字,并且超出此范围的任何单词都可以,例如/kick BattleDash hello world会说BattleDash被踢了个hello

Here's my code: 这是我的代码:

    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;

}

(Also if you don't include a reason it gives internal error in chat and exception occured in console) (此外,如果您不提供原因,则聊天中会出现内部错误,并且控制台中会发生异常)

Your code is doing that due to the way it was programmed. 由于代码的编程方式,您的代码正在执行此操作。

If we take a look at line that takes the argument: 如果我们看一下接受参数的行:

playerToKick.kickPlayer(ChatColor.RED + "Kicked by Administrator " + player.getName() + "\nReason: " + args[1]);

We can see that you're only using args[1] . 我们可以看到您仅使用args[1] If we see a message as an array, the problem will be clear: 如果我们将消息视为数组,则问题将很明显:

At position 0 (remember arrays start at 0): BattleDash 在位置0(记住数组从0开始):BattleDash

1: hello 1:你好

2: world 2:世界

When you take only args[1] to your message, only hello would be used! 当您仅在消息中使用args[1] ,将仅使用hello What we need to do instead, is use all the arguments. 我们需要做的是使用所有参数。 One way to do that is like this: 一种方法是这样的:

// 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM