Skip to content

Commit

Permalink
Support MC RGB colors in conversion to ANSI color codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pieter12345 committed Dec 17, 2023
1 parent 0ff73b4 commit 0b963c4
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/main/java/com/laytonsmith/core/Static.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit 0b963c4

Please sign in to comment.