From f528bedee9715b9637e733a255f23e1d45e56200 Mon Sep 17 00:00:00 2001 From: Pieter12345 Date: Sun, 17 Dec 2023 01:39:03 +0100 Subject: [PATCH 1/2] Support MC RGB colors in conversion to ANSI color codes --- src/main/java/com/laytonsmith/core/Static.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/laytonsmith/core/Static.java b/src/main/java/com/laytonsmith/core/Static.java index 6cb190a5a..ac00bbcd8 100644 --- a/src/main/java/com/laytonsmith/core/Static.java +++ b/src/main/java/com/laytonsmith/core/Static.java @@ -93,6 +93,7 @@ import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.regex.MatchResult; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.yaml.snakeyaml.Yaml; @@ -1178,12 +1179,20 @@ public static String GetStringResource(Class path, String name) { * @return */ public static String MCToANSIColors(String mes) { - //Pull out the MC colors if(mes == null) { return null; } + + // Convert RGB color codes to ANSI RGB color codes. Note that Windows command prompt ignores this. + mes = Pattern.compile("(?i)§x(?:§([a-f0-9])){6}").matcher(mes).replaceAll((MatchResult res) -> { + int red = Integer.parseInt(res.group(1) + res.group(2), 16); + int green = Integer.parseInt(res.group(3) + res.group(4), 16); + int blue = Integer.parseInt(res.group(5) + res.group(6), 16); + return "\u001B[38;2;" + red + ";" + green + ";" + blue + "m"; + }); + + // Convert preset MC color codes and markup codes to ANSI codes. return mes - .replaceAll("(?i)§x(§[a-f0-9]){6}", "") .replaceAll("§0", TermColors.BLACK) .replaceAll("§1", TermColors.BLUE) .replaceAll("§2", TermColors.GREEN) @@ -1206,7 +1215,6 @@ public static String MCToANSIColors(String mes) { .replaceAll("§n", TermColors.UNDERLINE) .replaceAll("§o", TermColors.ITALIC) .replaceAll("§r", TermColors.RESET); - } public static MCCommandSender GetInjectedPlayer(String name) { From 65554a2037cfba80663c35980c77954b94d22739 Mon Sep 17 00:00:00 2001 From: Pieter12345 Date: Sun, 17 Mar 2024 00:46:42 +0100 Subject: [PATCH 2/2] Explore RGB ANSI code regex to fix matching --- src/main/java/com/laytonsmith/core/Static.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/laytonsmith/core/Static.java b/src/main/java/com/laytonsmith/core/Static.java index ac00bbcd8..900d3395e 100644 --- a/src/main/java/com/laytonsmith/core/Static.java +++ b/src/main/java/com/laytonsmith/core/Static.java @@ -1184,7 +1184,8 @@ public static String MCToANSIColors(String mes) { } // Convert RGB color codes to ANSI RGB color codes. Note that Windows command prompt ignores this. - mes = Pattern.compile("(?i)§x(?:§([a-f0-9])){6}").matcher(mes).replaceAll((MatchResult res) -> { + mes = Pattern.compile("(?i)§x§([a-f0-9])§([a-f0-9])§([a-f0-9])§([a-f0-9])§([a-f0-9])§([a-f0-9])") + .matcher(mes).replaceAll((MatchResult res) -> { int red = Integer.parseInt(res.group(1) + res.group(2), 16); int green = Integer.parseInt(res.group(3) + res.group(4), 16); int blue = Integer.parseInt(res.group(5) + res.group(6), 16);