/broadcast Command Sending Multiple Messages!

Discussion in 'Plugin Development' started by SilverOlympus, Dec 7, 2014.

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

    SilverOlympus

    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3.  
    4. if (cmd.getName().equalsIgnoreCase("broadcast")){
    5. if (sender instanceof Player){
    6. if (sender.hasPermission("se.broadcast")){
    7. if (args.length == 0){
    8. sender.sendMessage(ChatColor.DARK_PURPLE+ "Whoops! You need to add more arguments for this to work!");
    9. }
    10. else{
    11. if (args.length >= 1){
    12. StringBuilder x = new StringBuilder();
    13. for (int i = 1; i < args.length; i++) {
    14. x.append(args[i] + " ");
    15. sender.sendMessage(ChatColor.GOLD+ "Done! Broadcast sent!");
    16. Bukkit.broadcastMessage(ChatColor.DARK_RED+ ""+ ChatColor.BOLD+ "ALERT (FROM " + sender.getName()+ "): "+ ChatColor.RED+ "" + ChatColor.ITALIC+ x.toString().trim());
    17.  
    18. }
    19. }
    20. }
    21. }
    22. else{
    23. //no perm player
    24. }
    25. }
    26. else{
    27. //console
    28. }
    29. }
    30.  
    31. return false;
    32. }[/i]


    Hello! When I use the code above to send a message, it will not say the first word, ex:

    '/broadcast hello im testing this'
    sends the message
    'ALERT (FROM Silver_Olympus): im testing this'

    It also does it like this,

    [​IMG]
     
  2. Offline

    Tehmaker

    Your for loop starts at the args[1], when it should start at args[0]...
     
  3. Offline

    SilverOlympus

    Tehmaker
    Ok, thanks, that fixed it not showing the first word, but do you know how to fix the message showing up three times and done with each one adding a word?
     
  4. Offline

    Tehmaker

    You have the messages in the for loop. It sends them 3 times because the loop runs three times. Take them outside the loop.
     
  5. Offline

    SilverOlympus

    Tehmaker
    Maybe im not understanding... Say I did the command

    /broadcast hi im testing this command for you

    it would send

    ALERT (FROM Silver_Olympus): hi
    ALERT (FROM Silver_Olympus): hi im
    ALERT (FROM Silver_Olympus): hi im testing
    ALERT (FROM Silver_Olympus): hi im testing this
    ALERT (FROM Silver_Olympus): hi im testing this command
    ALERT (FROM Silver_Olympus): hi im testing this command for
    ALERT (FROM Silver_Olympus): hi im testing this command for you
     
  6. Offline

    Unica

    SilverOlympus

    Quite obvious really..

    SilverOlympus

    Code:java
    1. int x = 1; //Change this
    2. StringBuilder sb = new StringBuilder();
    3. for(int i= x; i<args.length; i++){
    4. sb.append(args).append(" ");
    5. }
    6. String message = sb.toString();
    7. broadcast(message); //Yay
    8.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  7. Offline

    SilverOlympus

    [​IMG]@Unica

    When using that code, and sending the message I put in the chat this happens:
     
  8. Offline

    SuperOriginal

    SilverOlympus append args[i not args

    I can't close the bracket because Bukkit makes it italic, but you know what I mean.
     
Thread Status:
Not open for further replies.

Share This Page