Fix further issues with rgb text pattern matching (#615)

This commit is contained in:
Josh Roy 2021-03-01 18:44:04 -05:00 committed by GitHub
parent 57c175ce7f
commit 07617c8b80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 26 deletions

View File

@ -1,4 +1,4 @@
From 6f38a71e1ed6d0715ac9495c1ab4df78a6bc83ed Mon Sep 17 00:00:00 2001
From f3589d89f522ed91d30330e62026dc1f7578e273 Mon Sep 17 00:00:00 2001
From: Josh Roy <10731363+JRoy@users.noreply.github.com>
Date: Sun, 21 Feb 2021 23:52:11 -0500
Subject: [PATCH] Add support for hex color codes in console
@ -59,10 +59,10 @@ index 3b3525f0..a42d63fa 100644
<Policies>
diff --git a/proxy/src/main/java/io/github/waterfallmc/waterfall/console/HexFormattingConverter.java b/proxy/src/main/java/io/github/waterfallmc/waterfall/console/HexFormattingConverter.java
new file mode 100644
index 00000000..2236f9f8
index 00000000..5d1a6126
--- /dev/null
+++ b/proxy/src/main/java/io/github/waterfallmc/waterfall/console/HexFormattingConverter.java
@@ -0,0 +1,186 @@
@@ -0,0 +1,178 @@
+package io.github.waterfallmc.waterfall.console;
+
+import net.minecrell.terminalconsole.TerminalConsoleAppender;
@ -97,6 +97,7 @@ index 00000000..2236f9f8
+ private static final String LOOKUP = "0123456789abcdefklmnor";
+
+ 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 String[] ansiCodes = new String[] {
@ -134,7 +135,7 @@ index 00000000..2236f9f8
+ * @param strip If true, the converter will strip all formatting codes
+ */
+ protected HexFormattingConverter(List<PatternFormatter> formatters, boolean strip) {
+ super("minecraftFormatting", null);
+ super("paperMinecraftFormatting", null);
+ this.formatters = formatters;
+ this.ansi = !strip;
+ }
@ -183,39 +184,30 @@ index 00000000..2236f9f8
+ return buffer.toString();
+ }
+
+ static void format(String s, StringBuilder result, int start, boolean ansi) {
+ int next = s.indexOf(COLOR_CHAR);
+ int last = s.length() - 1;
+ static void format(String content, StringBuilder result, int start, boolean ansi) {
+ int next = content.indexOf(COLOR_CHAR);
+ int last = content.length() - 1;
+ if (next == -1 || next == last) {
+ result.setLength(start);
+ result.append(s);
+ result.append(content);
+ if (ansi) {
+ result.append(ANSI_RESET);
+ }
+ return;
+ }
+
+ result.setLength(start + next);
+
+ int pos = next;
+ do {
+ int format = LOOKUP.indexOf(Character.toLowerCase(s.charAt(next + 1)));
+ Matcher matcher = NAMED_PATTERN.matcher(content);
+ StringBuffer buffer = new StringBuffer();
+ while (matcher.find()) {
+ int format = LOOKUP.indexOf(Character.toLowerCase(matcher.group().charAt(1)));
+ if (format != -1) {
+ if (pos != next) {
+ result.append(s, pos, next);
+ }
+ if (ansi) {
+ result.append(ansiCodes[format]);
+ }
+ pos = next += 2;
+ } else {
+ next++;
+ matcher.appendReplacement(buffer, ansi ? ansiCodes[format] : "");
+ }
+ }
+ matcher.appendTail(buffer);
+
+ next = s.indexOf(COLOR_CHAR, next);
+ } while (next != -1 && next < last);
+
+ result.append(s, pos, s.length());
+ result.setLength(start);
+ result.append(buffer.toString());
+ if (ansi) {
+ result.append(ANSI_RESET);
+ }