Broadcaster?!?!?!?!

Discussion in 'Plugin Development' started by keto23, Feb 18, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    keto23

    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.
     
    Last edited by a moderator: May 24, 2016
  2. Offline

    Superkabii

    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);
     
  3. Offline

    messageofdeath

  4. Using a Joiner is much more efficient.
    Code:java
    1. if (args.length >= 1) {
    2. String sb = Joiner.on(" ").join(args);
    3. sender.getServer().broadcastMessage(ChatColor.RED + "[Server] " + sb);
     
  5. Offline

    dillyg10

    Back to his original question...

    Code:
    for(Player p : Bukkit.getServer().getOnlinePlayers())
    {
    p.sendMessage("message");
      }
     
  6. Offline

    Njol

    Bukkit.broadcastMessage("[DC] "+message)
     
  7. Offline

    dillyg10

    You could do that aswell :D

    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 :)
     
Thread Status:
Not open for further replies.

Share This Page