Reputation:
I would like to make a broadcast command. When the /broadcast This is a test
is run, it would broadcast This is a test (with spaces) in-game. I've tried using:
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]);
But I know it is wrong. How can I fix this?
Upvotes: 2
Views: 615
Reputation:
Here is the code that ended up working:
if (args.length > 0) {
String broadcast = "";
for (String message : args) {
broadcast = (broadcast + message + " ");
}
Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "BROADCAST" + ChatColor.WHITE + "]" + ChatColor.RED + broadcast);
}
Upvotes: 1
Reputation: 11051
If you're using Java 8, you can use a handy method with Collectors
:
Bukkit.getServer().broadcastMessage(
ChatColor.RED + Arrays.stream(args)
.collect(Collectors.joining(" "))
);
Otherwise, iterate through the array and use a StringBuilder
to concatenate the elements:
if (args.length == 0)
// Premature return instead of using if-else
// to reduce cyclomatic complexity.
return false;
if (args.length == 1) {
// fast path
Bukkit.getServer().broadcastMessage(ChatColor.RED + args[0]);
return false;
}
StringBuilder message = new StringBuilder(args[0]);
for (String each : args)
message.append(" ").append(each);
String broadcast = message.deleteCharAt(0).toString();
Bukkit.getServer().broadcastMessage(ChatColor.RED + broadcast);
Upvotes: 3
Reputation: 23616
Using
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]);
Will only work if there are exactly 15 arguments. If there are less arguments, it will throw an ArrayIndexOutOfBoundsException
, because you are trying to access part of the array that does not exist. If there are more arguments, the code will only print the first 15 arguments, and the rest will be ignored.
To fix this, you need to loop through all of the arguments:
for(String argument : args)
Then, you need to add the argument to the broadcast message, along with a space:
message+=argument;
message+=" ";
To avoid an ArrayIndexOutOfBoundsException
, you should also check if there is at least 1 argument:
if(args.length >= 1)
So, here's what your code could look like:
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
}
Upvotes: 2