[broadcast] args?

Discussion in 'Plugin Development' started by DigitalCookie, Feb 26, 2014.

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

    DigitalCookie

    Heres my code

    Code:java
    1. if(cmd.getName().equalsIgnoreCase("ezbroadcast")){
    2. Bukkit.broadcastMessage(prefix2 + ChatColor.WHITE + args);
    3. return true;


    When I do the command on the server I get the right prefix but then I just get a bunch of random letters.
     
  2. Offline

    xTigerRebornx

    DigitalCookie Because args is an String[], not a String. Use a StringBuilder or something of the sorts to make it into a String
     
  3. Offline

    DigitalCookie

    xTigerRebornx I researched Stringbuilders and I dont understand it? How would I apply this to my project?
     
  4. Offline

    xTigerRebornx

    DigitalCookie You would loop through args[] and append each value and a space (or whatever you want seperating each word), and then get the String from the StringBuilder and broadcast that
     
  5. Offline

    DigitalCookie

    oh ok thanks

    Ive got this but when I broadcast I just get the prefix xTigerRebornx
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("ezbroadcast")){
    2. if(args.length == 0){
    3. player.sendMessage(prefix2 + ChatColor.DARK_RED + ChatColor.BOLD + " Please use the correct format: /ezbroadcast <message>");
    4. }if(args.length > 0){
    5. StringBuilder sb = new StringBuilder();
    6. for (int i = 1; i < args.length; i++){
    7. sb.append(args[i]).append(" ");
    8. }
    9. String allArgs = sb.toString().trim();
    10. Bukkit.broadcastMessage(prefix2 + " " + ChatColor.RED + allArgs);
    11. }[/i]


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

    xTigerRebornx

    DigitalCookie Few things,
    Use a for-each loop for looping through your args
    Code:
    for(String s : args){
    sb.append(s).append(" ");
    }
    
    Use some debug in your for/for-each loop to see if it is actually using the correct thing
     
  7. Offline

    DigitalCookie

    xTigerRebornx ok thanks that works and If i want to use custom color codes what do I put after "&",
    Code:
    Bukkit.broadcastMessage(prefix2 + ChatColor.translateAlternateColorCodes("&", )) + " " + allArgs);
     
  8. Offline

    xTigerRebornx

    DigitalCookie you would put allArgs as the second argument for it and then remove the + allArgs at the end
     
  9. Offline

    DigitalCookie

    xTigerRebornx Getting error: The method translateAlternateColorCodes(char, String) in the type ChatColor is not applicable for the arguments (String,
    String)
     
  10. Offline

    xTigerRebornx

  11. Offline

    DigitalCookie

    xTigerRebornx ya
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("ezbroadcast")){
    2. if(args.length == 0){
    3. player.sendMessage(prefix2 + ChatColor.DARK_RED + ChatColor.BOLD + " Please use the correct format: /ezbroadcast <message>");
    4. }if(args.length > 0){
    5. StringBuilder sb = new StringBuilder();
    6. for(String s : args){
    7. sb.append(s).append(" ");
    8. }
    9. String allArgs = sb.toString().trim();
    10. Bukkit.broadcastMessage(prefix2 + ChatColor.translateAlternateColorCodes("&", allArgs));
    11. player.playSound(location, Sound.CHICKEN_EGG_POP, 1, 1);
     
  12. Offline

    xTigerRebornx

    DigitalCookie The first argument of it needs to be a char, try using '&' instead of "&"
     
  13. Offline

    DigitalCookie

    xTigerRebornx Thank you so much! I have learned a lot and this knowledge will increase my plugin awesomeness. Thanks.
     
  14. Offline

    Maulss

Thread Status:
Not open for further replies.

Share This Page