Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support MC RGB colors in conversion to ANSI color codes #1373

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 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,21 @@ 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])§([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);
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 +1216,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
Loading