Archived
0

Chat style

This commit is contained in:
2018-04-30 13:08:01 +03:00
parent 9a346c2081
commit 682f80bdb7
2 changed files with 59 additions and 2 deletions

View File

@@ -0,0 +1,58 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-04-30
*/
package mc.core;
import java.util.regex.Pattern;
public enum ChatStyle {
BLACK ('0'),
DARK_BLUE ('1'),
DARK_GREEN('2'),
DARK_CYAN ('3'),
DARK_RED ('4'),
PURPLE ('5'),
GOLD ('6'),
GRAY ('7'),
DARK_GRAY ('8'),
BLUE ('9'),
GREEN ('a'),
CYAN ('b'),
RED ('c'),
PINK ('d'),
YELLOW('e'),
WHITE ('f');
private static final char COLOR_CHAR = '\u00a7'; // §
private static final String codes = "0123456789aAbBcCdDeEfF";
private static final Pattern EXCAPE_PATTERN = Pattern.compile(COLOR_CHAR + "[0-9a-f]", Pattern.CASE_INSENSITIVE);
public static String format(char colorChar, String message) {
char[] chars = message.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (chars[i] == colorChar && codes.indexOf(chars[i+1]) > -1) {
chars[i] = COLOR_CHAR;
chars[i+1] = Character.toLowerCase(chars[i+1]);
i++;
}
}
return String.valueOf(chars);
}
public static String escapeStyle(String message) {
return EXCAPE_PATTERN.matcher(message).replaceAll("");
}
private char[] toString;
ChatStyle(char ch) {
toString = new char[]{ COLOR_CHAR, ch };
}
@Override
public String toString() {
return String.valueOf(toString);
}
}