I haven't really seen this anywhere, but this is a very basic method I've made to convert the & color codes to the respective ChatColor constants for messages. PHP: public String colorizeText(String string) { string = string.replaceAll("&0", ChatColor.BLACK+""); string = string.replaceAll("&1", ChatColor.DARK_BLUE+""); string = string.replaceAll("&2", ChatColor.DARK_GREEN+""); string = string.replaceAll("&3", ChatColor.DARK_AQUA+""); string = string.replaceAll("&4", ChatColor.DARK_RED+""); string = string.replaceAll("&5", ChatColor.DARK_PURPLE+""); string = string.replaceAll("&6", ChatColor.GOLD+""); string = string.replaceAll("&7", ChatColor.GRAY+""); string = string.replaceAll("&8", ChatColor.DARK_GRAY+""); string = string.replaceAll("&9", ChatColor.BLUE+""); string = string.replaceAll("&a", ChatColor.GREEN+""); string = string.replaceAll("&b", ChatColor.AQUA+""); string = string.replaceAll("&c", ChatColor.RED+""); string = string.replaceAll("&d", ChatColor.LIGHT_PURPLE+""); string = string.replaceAll("&e", ChatColor.YELLOW+""); string = string.replaceAll("&f", ChatColor.WHITE+""); return string;}
PHP: public String colorizeText(String string) { for (ChatColor color : ChatColor.values()) { string = string.replace(String.format("&%x", color.getCode()), color.toString()); } return string;}
Code: public class Utility { private final static Pattern COLOR = Pattern.compile("(?i)&([0-F])"); public static String color(String line) { return COLOR.matcher(line).replaceAll("\u00A7$1"); } }
I like this most: Code: /** * Convert color hex values * * @param msg * @return */ public static String parseColors(String msg) { return msg.replace("&", "\u00a7"); }
I personally use this: Code: public String colorize(String message) { return message.replaceAll("&([a-f0-9])", ChatColor.COLOR_CHAR + "$1"); }
Thought I would post this, we use this in CommandsEX Code: public static String replaceChatColors(String s) { for (ChatColor c : ChatColor.values()) { s = s.replaceAll("&" + c.getChar(), ChatColor.getByChar(c.getChar()) + ""); } return s; } Never knew there were so many ways to do something simple.
Am I the only one who stopped using Regex? because in some scenarios if you use it a lot it's stupid slow (in my case it was). it also replaces bold/other escape sequences now supported (not just colors despite the name) Code: /** * The escape sequence for minecraft special chat codes */ public static final char ESCAPE = '\u00A7'; /** * Replace all of the color codes (prepended with &) with the corresponding color code. * This uses raw char arrays so it can be considered to be extremely fast. * * @param text * @return */ public static String replaceColors(String text) { char[] chrarray = text.toCharArray(); for (int index = 0; index < chrarray.length; index ++) { char chr = chrarray[index]; // Ignore anything that we don't want if (chr != '&') { continue; } if ((index + 1) == chrarray.length) { // we are at the end of the array break; } // get the forward char char forward = chrarray[index + 1]; // is it in range? if ((forward >= '0' && forward <= '9') || (forward >= 'a' && forward <= 'f') || (forward >= 'k' && forward <= 'r')) { // It is! Replace the char we are at now with the escape sequence chrarray[index] = ESCAPE; } } // Rebuild the string and return it return new String(chrarray); }
Nice and simple approach, Hidendra. Code: // It is! Replace the char we are at now with the escape sequence chrarray[index] = ESCAPE; } else { if (forward == '&') { index++; } } This small addition would support && escapes. The .translateAlternateColorCodes method seems to be the most compatible solution, if permissions per color/formatting are not required. I'm going to take a look at it.