|
| 1 | +package taboolib.module.chat; |
| 2 | + |
| 3 | +import net.md_5.bungee.api.ChatColor; |
| 4 | +import org.jetbrains.annotations.NotNull; |
| 5 | + |
| 6 | +import java.awt.*; |
| 7 | +import java.util.Optional; |
| 8 | + |
| 9 | +/** |
| 10 | + * @author sky |
| 11 | + * @since 2021/1/18 2:02 下午 |
| 12 | + */ |
| 13 | +public class HexColor { |
| 14 | + |
| 15 | + private static boolean isLegacy = false; |
| 16 | + |
| 17 | + static { |
| 18 | + try { |
| 19 | + ChatColor.of(Color.BLACK); |
| 20 | + } catch (NoSuchMethodError ignored) { |
| 21 | + isLegacy = true; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * 对字符串中的特殊颜色表达式进行转换<br> |
| 27 | + * 可供转换的格式有: |
| 28 | + * <p> |
| 29 | + * &{255-255-255} —— RGB 代码 |
| 30 | + * <p> |
| 31 | + * &{255,255,255} —— RGB 代码 |
| 32 | + * <p> |
| 33 | + * &{#FFFFFF} —— HEX 代码 |
| 34 | + * <p> |
| 35 | + * &{BLUE} —— 已知颜色(英文) |
| 36 | + * <p> |
| 37 | + * &{蓝} —— 已知颜色(中文) |
| 38 | + * |
| 39 | + * @param in 字符串 |
| 40 | + * @return String |
| 41 | + */ |
| 42 | + @SuppressWarnings("CallToPrintStackTrace") |
| 43 | + @NotNull |
| 44 | + public static String translate(String in) { |
| 45 | + if (isLegacy) { |
| 46 | + return ChatColor.translateAlternateColorCodes('&', in); |
| 47 | + } |
| 48 | + StringBuilder builder = new StringBuilder(); |
| 49 | + char[] chars = in.toCharArray(); |
| 50 | + for (int i = 0; i < chars.length; i++) { |
| 51 | + if (i + 1 < chars.length && chars[i] == '&' && chars[i + 1] == '{') { |
| 52 | + ChatColor chatColor = null; |
| 53 | + char[] match = new char[0]; |
| 54 | + for (int j = i + 2; j < chars.length && chars[j] != '}'; j++) { |
| 55 | + match = arrayAppend(match, chars[j]); |
| 56 | + } |
| 57 | + if (match.length == 11 && (match[3] == ',' || match[3] == '-') && (match[7] == ',' || match[7] == '-')) { |
| 58 | + chatColor = ChatColor.of(new Color(toInt(match, 0, 3), toInt(match, 4, 7), toInt(match, 8, 11))); |
| 59 | + } else if (match.length == 7 && match[0] == '#') { |
| 60 | + try { |
| 61 | + chatColor = ChatColor.of(toString(match)); |
| 62 | + } catch (IllegalArgumentException ignored) { |
| 63 | + } |
| 64 | + } else { |
| 65 | + Optional<StandardColors> knownColor = StandardColors.match(toString(match)); |
| 66 | + if (knownColor.isPresent()) { |
| 67 | + chatColor = knownColor.get().toChatColor(); |
| 68 | + } |
| 69 | + } |
| 70 | + if (chatColor != null) { |
| 71 | + builder.append(chatColor); |
| 72 | + i += match.length + 2; |
| 73 | + } |
| 74 | + } else { |
| 75 | + builder.append(chars[i]); |
| 76 | + } |
| 77 | + } |
| 78 | + String colorString = builder.toString(); |
| 79 | + // 1.20.4 不再支持该写法,该模块无法判断版本,因此全部替换为白色 |
| 80 | + // 若需要恢复默认色请使用 SimpleComponent 中的 reset 属性 |
| 81 | + colorString = colorString.replace("&r", "&f").replace("§r", "§f"); |
| 82 | + return ChatColor.translateAlternateColorCodes('&', colorString); |
| 83 | + } |
| 84 | + |
| 85 | + public static String getColorCode(int color) { |
| 86 | + return ChatColor.of(new Color(color)).toString(); |
| 87 | + } |
| 88 | + |
| 89 | + private static char[] arrayAppend(char[] chars, char in) { |
| 90 | + char[] newChars = new char[chars.length + 1]; |
| 91 | + System.arraycopy(chars, 0, newChars, 0, chars.length); |
| 92 | + newChars[chars.length] = in; |
| 93 | + return newChars; |
| 94 | + } |
| 95 | + |
| 96 | + private static String toString(char[] chars) { |
| 97 | + StringBuilder builder = new StringBuilder(); |
| 98 | + for (char c : chars) { |
| 99 | + builder.append(c); |
| 100 | + } |
| 101 | + return builder.toString(); |
| 102 | + } |
| 103 | + |
| 104 | + private static int toInt(char[] chars, int start, int end) { |
| 105 | + StringBuilder builder = new StringBuilder(); |
| 106 | + for (int i = start; i < end; i++) { |
| 107 | + builder.append(chars[i]); |
| 108 | + } |
| 109 | + return Integer.parseInt(builder.toString()); |
| 110 | + } |
| 111 | +} |
0 commit comments