每次运行没有任何其他参数的命令时,我都会收到内部错误。内部错误: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;
最佳答案
我看到您正在使用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;