每当我运行命令“ msg”时,我都会收到内部错误

时间:2019-12-27 01:59:49

标签: java bukkit

每次运行不带任何其他参数的命令时,都会收到内部错误。内部错误:https://gyazo.com/f4c16facadf878fad6b00e645b3539c6 //我的代码:

    Player target = Bukkit.getServer().getPlayer(args[0]);

    if (cmd.getName().equalsIgnoreCase("msg")) {

        if (args.length < 1) {

            sender.sendMessage("§cUsage: /msg <player> <msg>");
            return true;
        }

        if (target != null) {

            if (args.length > 1) {

                String msg = "";

                for (int i = 1; i < args.length; i++){
                    String arg = (args[i] + " ");
                    msg = (msg + arg);
                }

                target.sendMessage("§b(From§f " + sender.getName() + "§b)§f " + msg);
                sender.sendMessage("§b(To§f " + target.getName() + "§b)§f " + msg);
                return true;
            }
        }

        if (target == null) {

            sender.sendMessage("§cThat player is offline.");
            return true;

1 个答案:

答案 0 :(得分:2)

我发现您在使用args[0]

 Player target = Bukkit.getServer().getPlayer(args[0]);

在检查长度之前,这里

if (args.length < 1) {

   sender.sendMessage("§cUsage: /msg <player> <msg>");
   return true;
}

这可能会导致the ArrayIndexOutOfBoundsException: 0

检查是否可以解决问题

        if (args.length < 1) {

            sender.sendMessage("§cUsage: /msg <player> <msg>");
            return true;
        }
Player target = Bukkit.getServer().getPlayer(args[0]);

    if (cmd.getName().equalsIgnoreCase("msg")) {

        if (target != null) {

            if (args.length > 1) {

                String msg = "";

                for (int i = 1; i < args.length; i++){
                    String arg = (args[i] + " ");
                    msg = (msg + arg);
                }

                target.sendMessage("§b(From§f " + sender.getName() + "§b)§f " + msg);
                sender.sendMessage("§b(To§f " + target.getName() + "§b)§f " + msg);
                return true;
            }
        }

        if (target == null) {

            sender.sendMessage("§cThat player is offline.");
            return true;