[Util] Coloring text with & symbol in code

Discussion in 'Resources' started by xSummit, Aug 30, 2013.

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

    xSummit

    So some people have been asking me on Skype how do I detect when a string contains color codes like &5, &9, &3, ect. and how to colorize it. So here's the code:

    Code:java
    1. public String colorize(String message){
    2. return message.replaceAll("&([a-z0-9])", ChatColor.COLOR_CHAR + "$1");
    3. }


    So what it does is detect & markers and colorizes the letters after. It takes a string and that string can be pretty much anything. This also supports italics, bold, underlining, ect. So like when a player chats and you want to check if he or she is trying to use color codes:

    Code:java
    1. @EventHandler
    2. public void OPC(AsyncPlayerChatEvent e){
    3.  
    4. if(e.getPlayer().hasPermission("chat.color")){
    5.  
    6. e.setMessage(colorize(e.getMessage()));
    7.  
    8. }
    9.  
    10. }


    And also reading from a config file:

    Code:java
    1.  
    2. String line1 = plugin.config.getString("messages.donate.message.line1");
    3. String line2 = plugin.config.getString("messages.donate.message.line2");
    4. String line3 = plugin.config.getString("messages.donate.message.line3");
    5. String line4 = plugin.config.getString("messages.donate.message.line4");
    6.  
    7. p.sendMessage(colorize(line1));
    8. p.sendMessage(colorize(line2));
    9. p.sendMessage(colorize(line3));
    10. p.sendMessage(colorize(line4));
    11.  
    12. }


    Images:

    [​IMG]

    [​IMG]

    Config:
    messages:
    donate:
    message:
    line1: '&4Hi &6this &8is &3in &bcolor!'

    Hope this helped anyone looking for something like this.
     
  2. Offline

    Ultimate_n00b

    Code:java
    1. ChatColor.translateAlternateColorCodes('&', "&4Hi &6this &8is &3in &bcolor!'");

    No?
     
    Chinwe and bobacadodl like this.
  3. Offline

    xSummit

    Never knew about that xD. Thanks for pointing that out.
     
  4. Offline

    CeramicTitan


    Code:java
    1. public String colorize(String message){
    2. return message = message.replaceAll("&([a-z0-9])", ChatColor.COLOR_CHAR + "$1");
    3. }


    OR

    Code:java
    1. public String colorize(String message){
    2. return message = ChatColor.translateAlternateColorCodes('&', message);
    3. }
     
  5. Offline

    Minecrell

    No? You are only replacing the color codes "a-z" (actually there is only "a-f"?) and "0-9" but the formatting codes for bold, italic and underline are actually "k-o" ("magic", bold, strikethrough, underline, italic) and "r" for reset. So you are only replacing the color codes in this method, not the formatting codes.
    The correct method for replacing formatting codes too would be something like that:
    Code:java
    1. public String colorize(String message){
    2. return message.replaceAll("&([0-9a-fk-or])", ChatColor.COLOR_CHAR + "$1");
    3. }
     
  6. Offline

    xSummit

    Hey buddy, mind testing out the code before you go all out on a rant?
    http://icap.me/i/VXPTLhKfrF.png
    http://icap.me/i/pRABegnIHX.png
     
    Minecrell likes this.
  7. Offline

    kreashenz

    Or how I like to do it!

    Code:java
    1. public String format(String string){
    2. return string.replace('&', 'ยง');
    3. }


    Haha, I know it's bad, but it works :D
     
  8. Offline

    Minecrell

    Ah sorry, you are right. :)
    Missed that because I thought you would just replace "a-f" using "a-z" in your method, but you are also replacing the formatting codes using it.
    However, there is no need to replace all & with the color char, even if it isn't a color code. The client will remove all color codes, even if they don't exist (the client will use them like the reset color code &r). This can lead to some problems like in the picture below:

    [​IMG]

    First line is always your method, second line my method and the third line is the message I've sent.

    The Bukkit implementation translateAlternateColorCodes is using a complete different method, I don't know if it is better than using a regex to replace the color codes, but I think they have a reason not to use one.

    Tagging kreashenz for the same problem.
     
  9. Offline

    Ultimate_n00b

    Code:java
    1. public static String translateAlternateColorCodes(char altColorChar, String textToTranslate) {
    2. char[] b = textToTranslate.toCharArray();
    3. for (int i = 0; i < b.length - 1; i++) {
    4. if (b[i] == altColorChar && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[i+1]) > -1) {
    5. b[i] = ChatColor.COLOR_CHAR;
    6. b[i+1] = Character.toLowerCase(b[i+1]);
    7. }
    8. }
    9. return new String(b);
    10. }[/i][/i]

    That's bukkit's method. You're right, why didn't they just use regex..
     
    akabarblake and bobacadodl like this.
Thread Status:
Not open for further replies.

Share This Page