From 74b6982b034962cc85f38368a3e27f8890a6130a Mon Sep 17 00:00:00 2001 From: Narimm Date: Thu, 27 Aug 2020 11:11:40 +1000 Subject: [PATCH] #711: Add Full RGB support to the console This is a fairly hacky way to do this - primarily because the Ansi library doesnt support RGB - however the VT100 codes here are the standard for RGB terminals. --- .../command/ColouredConsoleSender.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/bukkit/craftbukkit/command/ColouredConsoleSender.java b/src/main/java/org/bukkit/craftbukkit/command/ColouredConsoleSender.java index fefb09f27d..4580642e0e 100644 --- a/src/main/java/org/bukkit/craftbukkit/command/ColouredConsoleSender.java +++ b/src/main/java/org/bukkit/craftbukkit/command/ColouredConsoleSender.java @@ -1,7 +1,10 @@ package org.bukkit.craftbukkit.command; +import java.awt.Color; import java.util.EnumMap; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import jline.Terminal; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -15,6 +18,9 @@ public class ColouredConsoleSender extends CraftConsoleCommandSender { private final Map replacements = new EnumMap(ChatColor.class); private final ChatColor[] colors = ChatColor.values(); private final boolean jansiPassthrough; + private static final char ANSI_ESC_CHAR = '\u001B'; + private static final String RGB_STRING = String.valueOf(ANSI_ESC_CHAR) + "[38;2;%d;%d;%dm"; + private static final Pattern RBG_TRANSLATE = Pattern.compile(String.valueOf(ChatColor.COLOR_CHAR) + "x(" + String.valueOf(ChatColor.COLOR_CHAR) + "[A-F0-9]){6}", Pattern.CASE_INSENSITIVE); protected ColouredConsoleSender() { super(); @@ -50,7 +56,7 @@ public class ColouredConsoleSender extends CraftConsoleCommandSender { // support jansi passthrough VM option when jansi doesn't detect an ANSI supported terminal if (jansiPassthrough || terminal.isAnsiSupported()) { if (!conversationTracker.isConversingModaly()) { - String result = message.replaceAll("(?i)" + ChatColor.COLOR_CHAR + "x(" + ChatColor.COLOR_CHAR + "[0-9a-f]){6}", ""); // Hex is not supported by ANSI console + String result = convertRGBColors(message); for (ChatColor color : colors) { if (replacements.containsKey(color)) { result = result.replaceAll("(?i)" + color.toString(), replacements.get(color)); @@ -65,6 +71,22 @@ public class ColouredConsoleSender extends CraftConsoleCommandSender { } } + private static String convertRGBColors(String input) { + Matcher matcher = RBG_TRANSLATE.matcher(input); + StringBuffer buffer = new StringBuffer(); + while (matcher.find()) { + String s = matcher.group().replace("ยง", "").replace('x', '#'); + Color color = Color.decode(s); + int red = color.getRed(); + int blue = color.getBlue(); + int green = color.getGreen(); + String replacement = String.format(RGB_STRING, red, green, blue); + matcher.appendReplacement(buffer, replacement); + } + matcher.appendTail(buffer); + return buffer.toString(); + } + public static ConsoleCommandSender getInstance() { if (Bukkit.getConsoleSender() != null) { return Bukkit.getConsoleSender();