Simplest way to color a message?

Discussion in 'Plugin Development' started by RobotA69, Feb 26, 2012.

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

    RobotA69

    What's the simplest way to make this command /c [color] [message]???
     
  2. Offline

    Gravity

    Well....

    Code:
    String a;
    if (args[0].equalsIgnoreCase("red"))
    {
        a = ChatColor.RED+"";
    }
    else if (args[0].equalsIgnoreCase("yellow"))
    {
        a = ChatColor.YELLOW+"";
    }
    //So on and so forth
    Then append the message to a, and send it. Also, most plugins are open-source here, make sure you look around to see what other kinds of stuff has what you are looking for and take a snip from them or something.
     
  3. Code:java
    1. String message = ChatColor.valueof(args[0].toUpperCase(java.util.Locale.ENGLISH)).toString().concat(message);
     
    RobotA69 likes this.
  4. Offline

    Windows_i7_920

    Personally, I prefer using ยง directly rather than Bukkit's ChatColor constants.
     
  5. Offline

    Njol

    better:
    Code:java
    1. try {
    2. String message = ChatColor.valueof(args[0].toUpperCase(java.util.Locale.ENGLISH)).toString().concat(message);
    3. //send message
    4. sender.sendMessage("invalid color!");
    5. }
     
    RobotA69 likes this.
  6. Offline

    Smex

    Code:java
    1. public static String replaceColors(String string){
    2. return string.replaceAll("(?i)&([a-k0-9])", "\u00A7$1");
    3. }


    Just put your message in the parameter and then you can use color codes with '&'.
     
    iPhysX likes this.
  7. Offline

    RobotA69

    The local variable message may not have been initialized :eek:
     
  8. Offline

    Njol

    You should send the message from within the try block, as the variable is only defined for this block, just where I put //send message.
     
  9. Offline

    RobotA69

    This doesnt work
    Code:
     try {
                        String message = ChatColor.valueOf(args[0].toUpperCase(java.util.Locale.ENGLISH)).toString().concat(message);
                        Bukkit.getServer().broadcastMessage(message);
                        } catch (IllegalArgumentException e) {
                        sender.sendMessage("invalid color!");
                        }
     
  10. Offline

    Njol

    @ferrybig's code was incorrect and I didn't notice it: The 'message' parameter at the end of the line should be another variable, e.g.:
    Code:java
    1. StringBuilder b = new StringBuilder(args[1]);
    2. for (int i = 2; i < args.length; i++) {
    3. b.append(" ");
    4. b.append(args[i ]);
    5. }
    6. String message = ChatColor.valueOf(args[0].toUpperCase(java.util.Locale.ENGLISH)).toString() + b.toString();

    Make sure that the user typed 2 arguments or more before this code is reached or you'll get an ArrayIndexOutOfBoundsException.
     
  11. Offline

    RobotA69

    Is this correct?
    Code:
      if(cmd.getName().equalsIgnoreCase("cb"))
                if(sender.hasPermission("Colors.broadcast")) {
                    if(args.length < 2);
                    return false;
                }
                    StringBuilder b = new StringBuilder(args[1]);
                    for (int i = 2; i < args.length; i++) {
                    b.append(" ");
                    b.append(args[i ]);
                    }
                    String message = ChatColor.valueOf(args[0].toUpperCase(java.util.Locale.ENGLISH)).toString() + b.toString();
                    Bukkit.getServer().broadcastMessage(message);
                return true;
                }
            return false;
            }
     
  12. Offline

    theguynextdoor

    Correct me if im wrong, but you're missing { at the end of your first if statement. And also, you want to return false and stop the command if they have the permission?

    Also with your string builder you don't need to args[1] in the brackets, just
    StringBuilder b = new StringBuilder();
    will do.
     
  13. Offline

    RobotA69

    PHP:
    if(cmd.getName().equalsIgnoreCase("cb")){
                    if(
    args.length 2);
                    return 
    false;
                }
                if(
    sender.hasPermission("Colors.broadcast")) {
                    
    StringBuilder b = new StringBuilder();
                    for (
    int i 2args.lengthi++) {
                    
    b.append(" ");
                    
    b.append(args[]);
                    }
                    
    String message ChatColor.valueOf(args[0].toUpperCase(java.util.Locale.ENGLISH)).toString() + b.toString();
                    
    Bukkit.getServer().broadcastMessage(message);
                return 
    true;
                }
            return 
    false;
            }
            return 
    false;
    }
    correct right?
     
  14. Offline

    theguynextdoor

    Well, with your first if statement, you added the open { which is good, but with your second if statement, you have a ; which makes it useless.


    Code:java
    1. if (cmd.getName().equalsIgnoreCase("cb")) {
    2. if (args.length < 2) {
    3. return false;
    4. }
    5.  
    6. if (sender.hasPermission("colors.broadcast")) {
    7. String msg;
    8.  
    9. StringBuilder sb = new StringBuilder();
    10. for (int i = 1; i < args.length; i++) {
    11. if (i != 1)
    12. sb.append(' ');
    13. sb.append(args[i]);
    14. }
    15. msg = sb.toString();
    16.  
    17. Bukkit.getServer().broadcastMessage(ChatColor.valueOf(args[0].toUpperCase(java.util.Locale.ENGLISH)).toString().concat(msg));
    18. return true;
    19. }
    20. else {
    21. sender.sendMessage("You don't have permission for that command.");
    22. }
    23. return false;
    24. }[/i]


    Try that, im not sure about the chat colour bit, but it's what someone said at the start of the thread, so i used that.
    You will soon learn where the { and } go and what they do, but with if statements, if you have a { and } at the end of the if statement, the code inside the if statement will only 'work' if the if statement is fulfilled. The { and } are always in pairs, you will never have an odd one.
     
  15. Offline

    Njol

    Code:java
    1. if (cmd.getName().equalsIgnoreCase("cb")) {
    2. if (args.length < 2) {
    3. return false;// this will send the usage defined in the plugin.yml to the player
    4. }
    5.  
    6. if (sender.hasPermission("colors.broadcast")) {
    7.  
    8. String color;
    9.  
    10. try {
    11. color = ""+ChatColor.valueOf(args[0].toUpperCase(java.util.Locale.ENGLISH));
    12. sender.sendMessage("invalid colour");
    13. return true;
    14. }
    15.  
    16. StringBuilder sb = new StringBuilder();
    17. for (int i = 1; i < args.length; i++) {
    18. if (i != 1)
    19. sb.append(' ');
    20. sb.append(args[ i ]);
    21. }
    22.  
    23. Bukkit.getServer().broadcastMessage(color + sb.toString());
    24.  
    25. return true;
    26.  
    27. } else {
    28. sender.sendMessage("You don't have permission to use this command.");
    29. }
    30. return true;
    31. }
     
Thread Status:
Not open for further replies.

Share This Page