13
0
geforkt von Mirrors/Paper

[Bleeding] Added ChatColor.translateAlternateColorCodes(). Addresses BUKKIT-1191

By: rmichela <deltahat@gmail.com>
Dieser Commit ist enthalten in:
Bukkit/Spigot 2012-03-16 00:40:47 -04:00
Ursprung 41c45c2342
Commit a7679c24c3
2 geänderte Dateien mit 28 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -150,6 +150,26 @@ public enum ChatColor {
return STRIP_COLOR_PATTERN.matcher(input).replaceAll(""); return STRIP_COLOR_PATTERN.matcher(input).replaceAll("");
} }
/**
* Translates a string using an alternate color code character into a string that uses the internal
* ChatColor.COLOR_CODE color code character. The alternate color code character will only be replaced
* if it is immediately followed by 0-9, A-F, or a-f.
*
* @param altColorChar The alternate color code character to replace. Ex: &
* @param textToTranslate Text containing the alternate color code character.
* @return Text containing the ChatColor.COLOR_CODE color code character.
*/
public static String translateAlternateColorCodes(char altColorChar, String textToTranslate) {
char[] b = textToTranslate.toCharArray();
for (int i = 0; i < b.length - 1; i++) {
if (b[i] == altColorChar && "0123456789AaBbCcDdEeFfKk".indexOf(b[i+1]) > -1) {
b[i] = ChatColor.COLOR_CHAR;
b[i+1] = Character.toLowerCase(b[i+1]);
}
}
return new String(b);
}
static { static {
for (ChatColor color : values()) { for (ChatColor color : values()) {
BY_ID.put(color.intCode, color); BY_ID.put(color.intCode, color);

Datei anzeigen

@ -62,4 +62,12 @@ public class ChatColorTest {
assertThat(String.format("%c%c", ChatColor.COLOR_CHAR, color.getChar()), is(color.toString())); assertThat(String.format("%c%c", ChatColor.COLOR_CHAR, color.getChar()), is(color.toString()));
} }
} }
@Test
public void translateAlternateColorCodes() {
String s = "&0&1&2&3&4&5&6&7&8&9&A&a&B&b&C&c&D&d&E&e&F&f&K&k & more";
String t = ChatColor.translateAlternateColorCodes('&', s);
String u = ChatColor.BLACK.toString() + ChatColor.DARK_BLUE + ChatColor.DARK_GREEN + ChatColor.DARK_AQUA + ChatColor.DARK_RED + ChatColor.DARK_PURPLE + ChatColor.GOLD + ChatColor.GRAY + ChatColor.DARK_GRAY + ChatColor.BLUE + ChatColor.GREEN + ChatColor.GREEN + ChatColor.AQUA + ChatColor.AQUA + ChatColor.RED + ChatColor.RED + ChatColor.LIGHT_PURPLE + ChatColor.LIGHT_PURPLE + ChatColor.YELLOW + ChatColor.YELLOW + ChatColor.WHITE + ChatColor.WHITE + ChatColor.MAGIC + ChatColor.MAGIC + " & more";
assertThat(t, is(u));
}
} }