使用
Bukkit.getServer().broadcastMessage(ChatColor.RED + args[0] + " " + args[1] + " " + args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " + args[6] + " " + args[7] + " " + args[8] + " " + args[9] + " " + args[10] + " " + args[11] + " " + args[12] + " " + args[13] + " " + args[14]);
如果恰好有15参数才有效。如果参数较少,则会抛出一个ArrayIndexOutOfBoundsException
,因为您试图访问不存在的数组的一部分。如果有更多的参数,代码将只打印前15个参数,其余的将被忽略。
要解决这个问题,就需要通过所有的参数循环:
for(String argument : args)
然后,你需要将参数添加到广播消息,与空间一起:
message+=argument;
message+=" ";
为避免ArrayIndexOutOfBoundsException
,您还应该检查是否至少有一个参数:
if(args.length >= 1)
所以,这里是你的代码可能是什么样子:
if (args.length >= 1) { // make sure there is at least 1 argument to avoid an ArrayOutOfBoundsException
String message = ""; // initialize the "message" variable
for (String argument : args) { // loop through all of the arguments
message += argument; // add the argument to the message
message += " "; // add a space to the message
}
Bukkit.getServer().broadcastMessage(ChatColor.RED + message); // broadcast the message
}