Hey all.. Im making a plugin that broadcasts messages. Ive done it all, except i cant figure out how to actually broadcast the player types with a command: /dc <message> I then want it to broadcast to the server: [DC] <Message player typed in command> Could anyone help me? Here is the code i have already: Code: package me.keto23.deltacraftbroadcaster; import java.util.logging.Logger; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.plugin.java.JavaPlugin; public class DeltacraftBroadcaster extends JavaPlugin { Logger log = Logger.getLogger("Minecraft"); public void onDisable() { log.info("[DCBroadcaster] is now disabled"); } public void onEnable() { log.info("[DCBroadcaster] is now enabled"); } @Override public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){ if(cmd.getName().equalsIgnoreCase("dc")){ if (sender.hasPermission("dc.broadcast")) { if (args.length < 2) { sender.sendMessage(ChatColor.RED + "Incorrect Usage:"); sender.sendMessage(ChatColor.RED + "/dc <message>"); return true; } else { // broadcast code return true; } } return true; } return true; } } EDIT by Moderator: merged posts, please use the edit button instead of double posting.
First off, we need to create a way to throw all args[] together into a single string. This can be done through creating a StringBuilder, and then a for() loop going through every arg. For example, the following method would return a single string for the args. Code: public String getJoinedArgs(String[] string) { StringBuilder builder = new StringBuilder(); for(int i = 0; i < string.length; i++) { builder.append(string[i]); builder.append(" "); } return builder.toString(); } The actual broadcasting of a message is done through getServer().broadcastMessage(String);
Using a Joiner is much more efficient. Code:java if (args.length >= 1) {String sb = Joiner.on(" ").join(args);sender.getServer().broadcastMessage(ChatColor.RED + "[Server] " + sb);
Back to his original question... Code: for(Player p : Bukkit.getServer().getOnlinePlayers()) { p.sendMessage("message"); }
You could do that aswell Either way works, yours may be just one line of code, but the upside to mine is you can have a little bit more controll on whom sees the msg