Changing '&f' to ChatColor.WHITE

Discussion in 'Plugin Development' started by Nineza, Jun 9, 2011.

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

    Nineza

    Okay, well, I wanted to make a plugin which would use Permissions' prefixes and suffixes on users, so I made one which would cancel the chat event, then broadcast a message to the server with the player's prefix, their display name, their suffix, a semicolon, then the stuff they chatted.

    I made it so it cancelled the chat event, and got the prefix and suffix from Permissions, and then I realised it doesn't colour it correctly. It displays the &f, it doesn't actually change its colour.

    I thought of having something along the lines of:
    Code:
    public String handleColours(String msg) {
        String finmsg = msg;
        finmsg = finmsg.replaceAll("&0", "" + ChatColor.BLACK);
        finmsg = finmsg.replaceAll("&1", "" + ChatColor.DARK_BLUE);
        finmsg = finmsg.replaceAll("&2", "" + ChatColor.DARK_GREEN);
        finmsg = finmsg.replaceAll("&3", "" + ChatColor.DARK_AQUA);
        finmsg = finmsg.replaceAll("&4", "" + ChatColor.DARK_RED);
        finmsg = finmsg.replaceAll("&5", "" + ChatColor.DARK_PURPLE);
        finmsg = finmsg.replaceAll("&6", "" + ChatColor.GOLD);
        finmsg = finmsg.replaceAll("&7", "" + ChatColor.GRAY);
        finmsg = finmsg.replaceAll("&8", "" + ChatColor.DARK_GRAY);
        finmsg = finmsg.replaceAll("&9", "" + ChatColor.BLUE);
        finmsg = finmsg.replaceAll("&a", "" + ChatColor.GREEN);
        finmsg = finmsg.replaceAll("&b", "" + ChatColor.AQUA);
        finmsg = finmsg.replaceAll("&c", "" + ChatColor.RED);
        finmsg = finmsg.replaceAll("&d", "" + ChatColor.LIGHT_PURPLE);
        finmsg = finmsg.replaceAll("&e", "" + ChatColor.YELLOW);
        finmsg = finmsg.replaceAll("&f", "" + ChatColor.WHITE);
        return finmsg;
    }
    Which would change the string you sent to it, so all &0, all &1, etc would change into the ChatColor. It works, but what I'm wondering... Is there a built-in method, or an easier way to do this?

    Sorry if there's already a thread with this in, but I couldn't find anything about it when I used the search.

    Thanks in advance!
     
  2. Offline

    Shamebot

    I need to guess,
    PHP:
    finmsg.replaceAll("(?i)&([0-F])","§$1"); //Edit: corrected index, added case insensitive flag
    //you can use \u00A7 instead of §
    I'm not sure at all, google for java regex; Another thing, there's a method to get the chatcolor by a number you could use too.

    Edit2: A good regex engine to quickly test something: http://gskinner.com/RegExr/
     
  3. Offline

    desht

    I use something like (and I took this from someone else's plugin):

    Code:
    String parseColourSpec(Player player, String spec) {
    	String res = spec.replaceAll("&(?<!&&)(?=[0-9a-fA-F])", "\u00A7");
    	return res.replace("&&", "&");
    }
    
    It's not bulletproof, but works well. It also allows for the use of '&&' as a literal '&'.
     
  4. Offline

    Nineza

    Um, first of all, how do you create that § sign other than copy & paste? My keyboard doesn't have it... Is there an alt code?
    Also, how do they work? How does that \u00A7 do the applicable colour, and how would the §$2 thing work?

    Sorry, I'm just curious since I like to know how the things I use work. It might be useful to know if I want to do something similar, but not the same. :D
     
  5. Offline

    Shamebot

    \u00A7 is the same as §, just search for a regex tutorial, would go beyond the scope of this thread to try to explain it.
     
  6. Umm, I don't see why everyone is making stuff complicated ;)
    This is what I use, and it works perfectly fine and only affects &x-patterns that are acutally a color (so the & needs to be followed by 0-9 or a-f).

    s.replaceAll("&([0-9a-f])", "\u00A7$1")

    You can imagine \u00A7 as a "control character" that tells the client to display stuff in the following color.
     
    Trc202 likes this.
  7. Offline

    Trc202

    Thanks. That saved me a great deal of time.
     
  8. Offline

    Shamebot

    Lol, that's actually exactly the same as mine except I didn't knew the correct index. But I think we both should add a "(?i)".
     
  9. I think there was something in your post that irritated me yesterday. Dunno, it just seemed weird to me. Sorry if it was similar, then I just overlooked it. ;)
     
  10. Offline

    desht

    Great! I'll just create this sign for my storage chest labeled 'stone&dirt'...

    Oops :)
     
  11. Offline

    iffa

    'Stone & Dirt'

    No more oops :)
     
Thread Status:
Not open for further replies.

Share This Page