Fix further issues with rgb text pattern matching

This commit is contained in:
Josh Roy 2021-02-25 13:27:15 -05:00 committed by Mariell Hoversholm
parent 89be8185db
commit 63fe5e4b77

View file

@ -7,13 +7,12 @@ Converts upstream's hex color code legacy format into actual hex color codes in
diff --git a/src/main/java/io/papermc/paper/console/HexFormattingConverter.java b/src/main/java/io/papermc/paper/console/HexFormattingConverter.java diff --git a/src/main/java/io/papermc/paper/console/HexFormattingConverter.java b/src/main/java/io/papermc/paper/console/HexFormattingConverter.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..362afb369ed8b00b0682237a6f9ca710391c9df8 index 0000000000000000000000000000000000000000..a4315961b7a465fb4872a4d67e7c26d4b4ed1fb9
--- /dev/null --- /dev/null
+++ b/src/main/java/io/papermc/paper/console/HexFormattingConverter.java +++ b/src/main/java/io/papermc/paper/console/HexFormattingConverter.java
@@ -0,0 +1,187 @@ @@ -0,0 +1,178 @@
+package io.papermc.paper.console; +package io.papermc.paper.console;
+ +
+import net.kyori.adventure.text.format.TextColor;
+import net.minecrell.terminalconsole.TerminalConsoleAppender; +import net.minecrell.terminalconsole.TerminalConsoleAppender;
+import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.Configuration;
@ -46,6 +45,7 @@ index 0000000000000000000000000000000000000000..362afb369ed8b00b0682237a6f9ca710
+ private static final String LOOKUP = "0123456789abcdefklmnor"; + private static final String LOOKUP = "0123456789abcdefklmnor";
+ +
+ private static final String RGB_ANSI = "\u001B[38;2;%d;%d;%dm"; + private static final String RGB_ANSI = "\u001B[38;2;%d;%d;%dm";
+ private static final Pattern NAMED_PATTERN = Pattern.compile(COLOR_CHAR + "[0-9a-fk-orA-FK-OR]");
+ private static final Pattern RGB_PATTERN = Pattern.compile(COLOR_CHAR + "x(" + COLOR_CHAR + "[0-9a-fA-F]){6}"); + private static final Pattern RGB_PATTERN = Pattern.compile(COLOR_CHAR + "x(" + COLOR_CHAR + "[0-9a-fA-F]){6}");
+ +
+ private static final String[] ansiCodes = new String[] { + private static final String[] ansiCodes = new String[] {
@ -83,7 +83,7 @@ index 0000000000000000000000000000000000000000..362afb369ed8b00b0682237a6f9ca710
+ * @param strip If true, the converter will strip all formatting codes + * @param strip If true, the converter will strip all formatting codes
+ */ + */
+ protected HexFormattingConverter(List<PatternFormatter> formatters, boolean strip) { + protected HexFormattingConverter(List<PatternFormatter> formatters, boolean strip) {
+ super("minecraftFormatting", null); + super("paperMinecraftFormatting", null);
+ this.formatters = formatters; + this.formatters = formatters;
+ this.ansi = !strip; + this.ansi = !strip;
+ } + }
@ -112,10 +112,10 @@ index 0000000000000000000000000000000000000000..362afb369ed8b00b0682237a6f9ca710
+ StringBuffer buffer = new StringBuffer(); + StringBuffer buffer = new StringBuffer();
+ while (matcher.find()) { + while (matcher.find()) {
+ String s = matcher.group().replace(String.valueOf(COLOR_CHAR), "").replace('x', '#'); + String s = matcher.group().replace(String.valueOf(COLOR_CHAR), "").replace('x', '#');
+ TextColor color = TextColor.fromHexString(s); + int hex = Integer.decode(s);
+ int red = color.red(); + int red = (hex >> 16) & 0xFF;
+ int blue = color.blue(); + int green = (hex >> 8) & 0xFF;
+ int green = color.green(); + int blue = hex & 0xFF;
+ String replacement = String.format(RGB_ANSI, red, green, blue); + String replacement = String.format(RGB_ANSI, red, green, blue);
+ matcher.appendReplacement(buffer, replacement); + matcher.appendReplacement(buffer, replacement);
+ } + }
@ -133,39 +133,30 @@ index 0000000000000000000000000000000000000000..362afb369ed8b00b0682237a6f9ca710
+ return buffer.toString(); + return buffer.toString();
+ } + }
+ +
+ static void format(String s, StringBuilder result, int start, boolean ansi) { + static void format(String content, StringBuilder result, int start, boolean ansi) {
+ int next = s.indexOf(COLOR_CHAR); + int next = content.indexOf(COLOR_CHAR);
+ int last = s.length() - 1; + int last = content.length() - 1;
+ if (next == -1 || next == last) { + if (next == -1 || next == last) {
+ result.setLength(start); + result.setLength(start);
+ result.append(s); + result.append(content);
+ if (ansi) { + if (ansi) {
+ result.append(ANSI_RESET); + result.append(ANSI_RESET);
+ } + }
+ return; + return;
+ } + }
+ +
+ result.setLength(start + next); + Matcher matcher = NAMED_PATTERN.matcher(content);
+ + StringBuffer buffer = new StringBuffer();
+ int pos = next; + while (matcher.find()) {
+ do { + int format = LOOKUP.indexOf(Character.toLowerCase(matcher.group().charAt(1)));
+ int format = LOOKUP.indexOf(Character.toLowerCase(s.charAt(next + 1)));
+ if (format != -1) { + if (format != -1) {
+ if (pos != next) { + matcher.appendReplacement(buffer, ansi ? ansiCodes[format] : "");
+ result.append(s, pos, next);
+ } + }
+ if (ansi) {
+ result.append(ansiCodes[format]);
+ }
+ pos = next += 2;
+ } else {
+ next++;
+ } + }
+ matcher.appendTail(buffer);
+ +
+ next = s.indexOf(COLOR_CHAR, next); + result.setLength(start);
+ } while (next != -1 && next < last); + result.append(buffer.toString());
+
+ result.append(s, pos, s.length());
+ if (ansi) { + if (ansi) {
+ result.append(ANSI_RESET); + result.append(ANSI_RESET);
+ } + }