|
1 | 1 | package cat.nyaa.nyaacore.utils;
|
2 | 2 |
|
3 |
| -import org.bukkit.ChatColor; |
4 |
| - |
5 |
| -import java.awt.*; |
6 |
| -import java.util.EnumSet; |
7 |
| -import java.util.Locale; |
8 |
| -import java.util.Set; |
9 |
| -import java.util.regex.Matcher; |
| 3 | +import java.util.regex.MatchResult; |
10 | 4 | import java.util.regex.Pattern;
|
11 | 5 |
|
12 | 6 | public class HexColorUtils {
|
13 |
| - //Extracted from EssentialsX |
14 |
| - private static final Pattern REPLACE_ALL_RGB_PATTERN = Pattern.compile("(&)?&#([0-9a-fA-F]{6})"); |
15 |
| - private static final Pattern REPLACE_ALL_PATTERN = Pattern.compile("(&)?&([0-9a-fk-orA-FK-OR])"); |
16 |
| - private static final Pattern LOGCOLOR_PATTERN = Pattern.compile("\\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]"); |
17 |
| - |
18 |
| - private static String replaceFormat(final String input) { |
19 |
| - if (input == null) { |
20 |
| - return null; |
21 |
| - } |
22 |
| - return replaceColor(input, EnumSet.allOf(ChatColor.class), true); |
23 |
| - } |
24 |
| - |
25 |
| - //This method is used to simply strip the & convention colour codes |
26 |
| - public static String stripEssentialsFormat(final String input) { |
27 |
| - if (input == null) { |
28 |
| - return null; |
29 |
| - } |
30 |
| - return stripColor(stripColor(input, REPLACE_ALL_PATTERN), REPLACE_ALL_RGB_PATTERN); |
31 |
| - } |
32 |
| - |
33 |
| - static String stripColor(final String input, final Pattern pattern) { |
34 |
| - return pattern.matcher(input).replaceAll(""); |
35 |
| - } |
36 |
| - |
37 |
| - public static String stripLogColorFormat(final String input) { |
38 |
| - if (input == null) { |
39 |
| - return null; |
40 |
| - } |
41 |
| - return stripColor(input, LOGCOLOR_PATTERN); |
42 |
| - } |
| 7 | + // Extract from SimpleLanguageLoader |
43 | 8 |
|
44 |
| - private static String replaceColor(final String input, final Set<ChatColor> supported, boolean rgb) { |
45 |
| - StringBuffer legacyBuilder = new StringBuffer(); |
46 |
| - Matcher legacyMatcher = REPLACE_ALL_PATTERN.matcher(input); |
47 |
| - legacyLoop: |
48 |
| - while (legacyMatcher.find()) { |
49 |
| - boolean isEscaped = (legacyMatcher.group(1) != null); |
50 |
| - if (!isEscaped) { |
51 |
| - char code = legacyMatcher.group(2).toLowerCase(Locale.ROOT).charAt(0); |
52 |
| - for (ChatColor color : supported) { |
53 |
| - if (color.getChar() == code) { |
54 |
| - legacyMatcher.appendReplacement(legacyBuilder, "\u00a7$2"); |
55 |
| - continue legacyLoop; |
56 |
| - } |
57 |
| - } |
58 |
| - } |
59 |
| - // Don't change & to section sign (or replace two &'s with one) |
60 |
| - legacyMatcher.appendReplacement(legacyBuilder, "&$2"); |
61 |
| - } |
62 |
| - legacyMatcher.appendTail(legacyBuilder); |
| 9 | + /** |
| 10 | + * hex color pattern looks like <code>&#RRGGBB</code> |
| 11 | + */ |
| 12 | + public static final Pattern hexColorPattern = Pattern.compile("&#[0-9A-Fa-f]{6}"); |
63 | 13 |
|
64 |
| - if (rgb) { |
65 |
| - StringBuffer rgbBuilder = new StringBuffer(); |
66 |
| - Matcher rgbMatcher = REPLACE_ALL_RGB_PATTERN.matcher(legacyBuilder.toString()); |
67 |
| - while (rgbMatcher.find()) { |
68 |
| - boolean isEscaped = (rgbMatcher.group(1) != null); |
69 |
| - if (!isEscaped) { |
70 |
| - try { |
71 |
| - String hexCode = rgbMatcher.group(2); |
72 |
| - rgbMatcher.appendReplacement(rgbBuilder, parseHexColor(hexCode)); |
73 |
| - continue; |
74 |
| - } catch (NumberFormatException ignored) { |
75 |
| - } |
76 |
| - } |
77 |
| - rgbMatcher.appendReplacement(rgbBuilder, "&#$2"); |
78 |
| - } |
79 |
| - rgbMatcher.appendTail(rgbBuilder); |
80 |
| - return rgbBuilder.toString(); |
81 |
| - } |
82 |
| - return legacyBuilder.toString(); |
| 14 | + /** |
| 15 | + * Replace the convenient RGB color code with expanded color code in a string. |
| 16 | + * |
| 17 | + * @param text text to be proceeded |
| 18 | + * @return text with expanded color code |
| 19 | + */ |
| 20 | + public static String extractColorCode(String text) { |
| 21 | + var textParts = hexColorPattern.split(text); |
| 22 | + if (textParts.length == 0) |
| 23 | + textParts = new String[]{"", ""}; |
| 24 | + var colorCodes = hexColorPattern.matcher(text).results().map(MatchResult::group).toArray(String[]::new); |
| 25 | + var textBuilder = new StringBuilder(textParts[0]); |
| 26 | + for (int i = 0; i < colorCodes.length; i++) { |
| 27 | + textBuilder.append(convertToTraditionalColorCode(colorCodes[i])).append(textParts[i + 1]); |
| 28 | + } |
| 29 | + return convertToLegacyColorCode(textBuilder.toString()); |
83 | 30 | }
|
84 | 31 |
|
85 | 32 | /**
|
86 |
| - * @throws NumberFormatException If the provided hex color code is invalid or if version is lower than 1.16. |
| 33 | + * Expand color code looks like <code>&#RRGGBB</code> to <code>&x&R&R&G&G&B&B</code>. |
| 34 | + * |
| 35 | + * @param x convenient color code |
| 36 | + * @return expanded color code |
87 | 37 | */
|
88 |
| - private static String parseHexColor(String hexColor) throws NumberFormatException { |
89 |
| - if (hexColor.startsWith("#")) { |
90 |
| - hexColor = hexColor.substring(1); //fuck you im reassigning this. |
91 |
| - } |
92 |
| - if (hexColor.length() != 6) { |
93 |
| - throw new NumberFormatException("Invalid hex length"); |
94 |
| - } |
95 |
| - Color.decode("#" + hexColor); |
96 |
| - StringBuilder assembledColorCode = new StringBuilder(); |
97 |
| - assembledColorCode.append("\u00a7x"); |
98 |
| - for (char curChar : hexColor.toCharArray()) { |
99 |
| - assembledColorCode.append("\u00a7").append(curChar); |
100 |
| - } |
101 |
| - return assembledColorCode.toString(); |
| 38 | + private static String convertToTraditionalColorCode(String x) { |
| 39 | + var hexColorCode = x.substring(2); |
| 40 | + var colorCodeBuilder = new StringBuilder("&x"); |
| 41 | + hexColorCode.chars().forEach(c -> colorCodeBuilder.append("&").append((char) c)); |
| 42 | + return colorCodeBuilder.toString(); |
102 | 43 | }
|
103 |
| - //Extracted from EssentialsX |
104 | 44 |
|
105 |
| - //actual call |
106 |
| - public static String hexColored(String str) { |
107 |
| - try { |
108 |
| - return replaceFormat(str); |
109 |
| - } catch (Exception e) { |
110 |
| - //fallback in case an exception thrown. |
111 |
| - return ChatColor.translateAlternateColorCodes('&', str); |
112 |
| - } |
| 45 | + private static String convertToLegacyColorCode(String text) { |
| 46 | + return text.replace('&', '§').replace(String.valueOf('&') + '&', "§"); |
113 | 47 | }
|
| 48 | + |
114 | 49 | }
|
0 commit comments