Skip to content

Commit 0b963c4

Browse files
committed
Support MC RGB colors in conversion to ANSI color codes
1 parent 0ff73b4 commit 0b963c4

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/main/java/com/laytonsmith/core/Static.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
import java.util.UUID;
9494
import java.util.logging.Level;
9595
import java.util.logging.Logger;
96+
import java.util.regex.MatchResult;
9697
import java.util.regex.Matcher;
9798
import java.util.regex.Pattern;
9899
import org.yaml.snakeyaml.Yaml;
@@ -1178,12 +1179,20 @@ public static String GetStringResource(Class path, String name) {
11781179
* @return
11791180
*/
11801181
public static String MCToANSIColors(String mes) {
1181-
//Pull out the MC colors
11821182
if(mes == null) {
11831183
return null;
11841184
}
1185+
1186+
// Convert RGB color codes to ANSI RGB color codes. Note that Windows command prompt ignores this.
1187+
mes = Pattern.compile("(?i)§x(?:§([a-f0-9])){6}").matcher(mes).replaceAll((MatchResult res) -> {
1188+
int red = Integer.parseInt(res.group(1) + res.group(2), 16);
1189+
int green = Integer.parseInt(res.group(3) + res.group(4), 16);
1190+
int blue = Integer.parseInt(res.group(5) + res.group(6), 16);
1191+
return "\u001B[38;2;" + red + ";" + green + ";" + blue + "m";
1192+
});
1193+
1194+
// Convert preset MC color codes and markup codes to ANSI codes.
11851195
return mes
1186-
.replaceAll("(?i)§x(§[a-f0-9]){6}", "")
11871196
.replaceAll("§0", TermColors.BLACK)
11881197
.replaceAll("§1", TermColors.BLUE)
11891198
.replaceAll("§2", TermColors.GREEN)
@@ -1206,7 +1215,6 @@ public static String MCToANSIColors(String mes) {
12061215
.replaceAll("§n", TermColors.UNDERLINE)
12071216
.replaceAll("§o", TermColors.ITALIC)
12081217
.replaceAll("§r", TermColors.RESET);
1209-
12101218
}
12111219

12121220
public static MCCommandSender GetInjectedPlayer(String name) {

0 commit comments

Comments
 (0)