[TUT] Simple color replacement

Discussion in 'Resources' started by r3Fuze, Nov 28, 2011.

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

    r3Fuze

    Some people have a hard time figuring out the &<colorcode> stuff in configs, so if you want your configs to be a bit simpler and easier to understand then you can use this code.

    With this you can put something like <red> in your text and it will be replaced with red. It's not case sensitive so it will work for <red>, <RED> and even <rEd>

    Heres a list of the Strings it will replace (not case sensitive ofcouse):
    Code:
    <black>
    <dark_blue>
    <dark_green>
    <dark_aqua>
    <dark_red>
    <dark_purple>
    <gold>
    <gray>
    <dark_gray>
    <blue>
    <green>
    <aqua>
    <red>
    <light_purple>
    <yellow>
    <white>
    Now for the code!
    Code:java
    1. public static String format(String string) {
    2. String s = string;
    3. for (ChatColor color : ChatColor.values()) {
    4. s = s.replaceAll("(?i)<" + color.name() + ">", "" + color);
    5. }
    6. return s;
    7. }


    How to use it:
    Code:java
    1. String myString = "This <red>message <yellow>is <light_purple>AWESOME!";
    2. player.sendMessage(format(myString));


    Here is how it works: It loops through every ChatColor and checks your String if it has the color with < and > surrounding it, if it has, then it replaces it with the ChatColor it matches. The (?i) part just makes sure it's not case sensitive.


    You can use this as you wish and you dont have to credit me or anything(I dont mind if you do though)
     
    astbis, surtic and Tauryuu like this.
  2. Offline

    Sleaker

    Code:
    	/**
    	 * Takes a string and replaces &# color codes with ChatColors
    	 * 
    	 * @param message
    	 * @return
    	 */
    	private String replaceColors (String message) {
    		return message.replaceAll("(?i)&([a-f0-9])", "\u00A7$1");
    	}
    http://www.minecraftwiki.net/wiki/Classic_server_protocol#Color_Codes

    I used to use a method similar to the OP - but it's not very optimal as it requires looping/regexing 16 times. the above works for all color codes (case insensetive) using the &# format as seen on the wiki. I'd suggest using something similar to this as it's about 16x faster.
    t3hk0d3 mentioned using this on irc after I looked into just doing standard character checking, and after doing some time tests (and fixing the original method) this one has the best time results.

    I realize your code post was more going for legibility, and it will work in the end, just wanted to show a way to do it that is very quick.
     
    nikosgram17, mavis and Tauryuu like this.
  3. Offline

    jacklin213

    how do i use that with this string builder
    Code:java
    1. public static String format(String string) {
    2. String s = string;
    3. for (ChatColor color : ChatColor.values()) {
    4. s = s.replaceAll("(?i)<" + color.name() + ">", "" + color);
    5. }
    6. return s;
    7. }
     
  4. Offline

    QuantumDev

    Is it really that hard to replace & with §?
     
  5. Offline

    Icyene

    But this has a neat way of doing it... Instead of doing &e you'd do <yellow> or something. More user friendly, IMO :)
     
Thread Status:
Not open for further replies.

Share This Page