[TUT] Broadcast Command

Discussion in 'Resources' started by emericask8ur, Nov 23, 2011.

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

    emericask8ur

    Code:
    if (cmdLabel.equalsIgnoreCase("broadcast")){
    			String message = "";
    			for (String part : args) {
    			    if (message != "") message += " ";
    			    message += part;
    			}
    			Bukkit.getServer().broadcastMessage("[Broadcast]" + message);
    			
    		}
    
     
    Phasesaber likes this.
  2. Offline

    Jaker232

    Oh really.. you just like pulling little code out of my project? *laughs* My code is a little different.
     
  3. Offline

    emericask8ur

    This has been in Sidekick for Months
     
    r3Fuze likes this.
  4. Please tell me that was a joke.
    Ontopic: nice tutorial. I've seen alot of people needing something like this.
     
  5. Offline

    Jaker232

    My code is

    Code:java
    1.  
    2. String message = (args[0]);
    3. Server server = Bukkit.getServer();
    4. String format = config.getString("message.broadcast-prefix");
    5. server.broadcastMessage(ChatColor.AQUA + format + message);
    6.  
     
  6. That will only broadcast the first argument and the stuff from your config. And why not use Bukkit.getServer().broadcastMessage(""); instead of what you did?
     
  7. Offline

    Jaker232

    I already imported Server server. Either way works.
     
  8. Offline

    emericask8ur

    Keep it easy fellas
     
    bobacadodl, Jaker232 and r3Fuze like this.
  9. Offline

    arnie231

    dont really see the point in argueing ive seen posts asking for the broadcast method and here it is

    its not like hes posted a huge code snippet direct from someones plugin
     
  10. And for efficency, we would rather do:
    Code:java
    1.  
    2. if (cmdLabel.equalsIgnoreCase("broadcast")) {
    3. StringBuilder message = new StringBuilder("");
    4. for (String part : args) {
    5. if (!message.toString().equals(""))
    6. message.append(" ");
    7.  
    8. message.append(part);
    9. }
    10.  
    11. Bukkit.getServer().broadcastMessage("[Broadcast]" + message.toString());
    12. }


    And if you want to, add final to the variables.
     
  11. Offline

    desht

    This. StringBuilder (or indeed Joiner) is the way to go here. Don't use string concatenation inside a for loop, it's seriously inefficient.

    http://kaioa.com/node/59
     
    Wingzzz likes this.
  12. Offline

    Alanox

    I did it!
    a usage message! :)
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("alert")){
    2. if(args.length == 0){
    3. player.sendMessage(ChatColor.RED +"Usage: /alert [message]");
    4. player.sendMessage(ChatColor.BLUE+"You can use this command to alert every user in the server");
    5. }else if(args.length >= 1){
    6. String message = "";
    7. for (String part : args) {
    8. if (message != "") message += " ";
    9. message += part;
    10. }
    11. Bukkit.getServer().broadcastMessage(ChatColor.DARK_BLUE + "[" + ChatColor.BLUE + "Superizee Alert" + ChatColor.DARK_BLUE + "]" + ChatColor.RESET + " " + message);
    12.  
    13. }
    14. }
     
    emericask8ur likes this.
  13. Offline

    Langur

    Cool! Your pic helped me fix my problem xD!
     
    emericask8ur likes this.
  14. Offline

    Skyost

    emericask8ur
    My eyes...
    Code:
    if(cmdLabel.equalsIgnoreCase("broadcast")) {
    if(args.length == 0) {
    return false;
    }
    String message;
    final StringBuilder stringBuilder = new StringBuilder();
    for(String part : args) {
    stringBuilder.append(part);
    stringBuilder.append(" ");
    }
    message = stringBuilder.toString();
    Bukkit.broadcastMessage(ChatColor.PURPLE + "[Broadcast] " + ChatColor.RESET + message.substring(0, message.length - 1));
    }
    
     
    Goblom likes this.
  15. Offline

    PunKeel


    My eyes, second edition

    Code:
    if(cmdLabel.equalsIgnoreCase("broadcast")) {
        if(args.length == 0) {
            return false;
        }
        Bukkit.broadcastMessage(ChatColor.PURPLE + "[Broadcast] " + ChatColor.RESET + Joiner.on(" ").join(args));
    }
    
     
    Skyost likes this.
  16. Offline

    Skyost

    PunKeel The Joiner is not available by default in Java. You need Google Collections Library.
    I have prodided a way which is available for anyone.
     
  17. Offline

    DarkBladee12

    Skyost You could also use StringUtils.join(...) which is available if you've imported Bukkit ;)
     
  18. Offline

    Skyost

    DarkBladee12 Thanks, but I have provided a way which is available without any lib(s) ;)
     
  19. Offline

    macguy8

    Skyost
    What's the harm in using libs? It's already IN Bukkit, so there's 0 hassle in getting your users to use it, AND libs are also better for a few other reasons. If I see com.somedeveloper.someplugin.StringUtils.capitalize(String param), I have no clue what it does, if it's efficient, what errors it can throw, etc. If I see a common library (eg Google Commons), I immediately know exactly what that method does, and that it's efficient enough that I can eliminate it as a possible problem.
     
  20. Offline

    PunKeel

    AFAIK Guava is provided by Bukkit, and we're on a forum about ... Bukkit. So, I don't get the point of being Library-independant ...
    Plus, using a lib. is better than making [bad] code, to keep it DRY ! :)
     
  21. Offline

    xNamesi

    Code:java
    1. //Broadcast Message
    2. if(label.equalsIgnoreCase("bc")){
    3. if(!sender.hasPermission("ezcmd.bc")) {
    4. sender.sendMessage(ChatColor.DARK_RED + "eZCommand" + ChatColor.DARK_GRAY + ChatColor.BOLD + ">" + ChatColor.RED + " You Don't Have Permission!");
    5. return true;
    6. }
    7. if(args.length == 0) {
    8. sender.sendMessage(ChatColor.DARK_RED + "eZCommand" + ChatColor.DARK_GRAY + ChatColor.BOLD + ">" + ChatColor.RED + " Usage:" + ChatColor.GREEN + " /bc {Message}");
    9. }else {
    10. String message = "";
    11. for (String part : args) {
    12. if (message != "") message += " ";
    13. message += part;
    14. }
    15. Bukkit.getServer().broadcastMessage(ChatColor.GRAY + "[" + ChatColor.AQUA + "Broadcast" + ChatColor.GRAY + "]" + " " + ChatColor.YELLOW + message);
    16. }
    17.  
    18.  
    19. }

    Thats mine and its good enough for me :p
     
  22. Offline

    HeavyMine13

    This is my way :)

    Code:java
    1. StringBuilder message = new StringBuilder();
    2. if (args.length > 0) {
    3. message.append(args[0]);
    4. for (int i = 1; i < args.length; i++) {
    5. message.append(" ");
    6. message.append(args[i]);
    7. }
    8. }
    9.  
    10. Bukkit.broadcastMessage(ChatColor.DARK_PURPLE +
    11. " [Alert] " + ChatColor.YELLOW + message );
    12. }
    13. else {
    14. StringBuilder message = new StringBuilder();
    15. if (args.length > 0) {
    16. message.append(args[0]);
    17. for (int i = 1; i < args.length; i++) {
    18. message.append(" ");
    19. message.append(args[i]);
    20. }
    21. }[/i][/i]
     
  23. Offline

    calebbfmv

    When I needed this, it wasn't here, why didn't you read my mind and post it sooner.....
     
  24. Offline

    Cirno

    My eyes, third edition (Requires JRE8)

    Code:java
    1. if(cmd.getName().equalsIgnoreCase("broadcast"){
    2. if(args.length == 0){
    3. cs.sendMessage(ChatColor.RED + "Usage: /broadcast <message>");
    4. return true; // You can lecture me about how I'm supposed to use "false" later.
    5. }
    6. Bukkit.broadcastMessage(ChatColor.PURPLE + "[Broadcast] " + ChatColor.RESET + ChatColor.translateAlternateColorCodes('&', String.join(" ", args));
    7. }
     
    Skyost likes this.
  25. Offline

    BigAl512

    No wounder my plugin replacing the join messages never worked. I was sending a message TO the PLAYER who JOINED instead of the entire server.
    Thanks :)
     
Thread Status:
Not open for further replies.

Share This Page