Commits
Narimm authored and md_5 committed 74b6982b034
1 1 | package org.bukkit.craftbukkit.command; |
2 2 | |
3 + | import java.awt.Color; |
3 4 | import java.util.EnumMap; |
4 5 | import java.util.Map; |
6 + | import java.util.regex.Matcher; |
7 + | import java.util.regex.Pattern; |
5 8 | import jline.Terminal; |
6 9 | import org.bukkit.Bukkit; |
7 10 | import org.bukkit.ChatColor; |
8 11 | import org.bukkit.command.ConsoleCommandSender; |
9 12 | import org.bukkit.craftbukkit.CraftServer; |
10 13 | import org.fusesource.jansi.Ansi; |
11 14 | import org.fusesource.jansi.Ansi.Attribute; |
12 15 | |
13 16 | public class ColouredConsoleSender extends CraftConsoleCommandSender { |
14 17 | private final Terminal terminal; |
15 18 | private final Map<ChatColor, String> replacements = new EnumMap<ChatColor, String>(ChatColor.class); |
16 19 | private final ChatColor[] colors = ChatColor.values(); |
17 20 | private final boolean jansiPassthrough; |
21 + | private static final char ANSI_ESC_CHAR = '\u001B'; |
22 + | private static final String RGB_STRING = String.valueOf(ANSI_ESC_CHAR) + "[38;2;%d;%d;%dm"; |
23 + | 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); |
18 24 | |
19 25 | protected ColouredConsoleSender() { |
20 26 | super(); |
21 27 | this.terminal = ((CraftServer) getServer()).getReader().getTerminal(); |
22 28 | this.jansiPassthrough = Boolean.getBoolean("jansi.passthrough"); |
23 29 | |
24 30 | replacements.put(ChatColor.BLACK, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLACK).boldOff().toString()); |
25 31 | replacements.put(ChatColor.DARK_BLUE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLUE).boldOff().toString()); |
26 32 | replacements.put(ChatColor.DARK_GREEN, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.GREEN).boldOff().toString()); |
27 33 | replacements.put(ChatColor.DARK_AQUA, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.CYAN).boldOff().toString()); |
43 49 | replacements.put(ChatColor.UNDERLINE, Ansi.ansi().a(Attribute.UNDERLINE).toString()); |
44 50 | replacements.put(ChatColor.ITALIC, Ansi.ansi().a(Attribute.ITALIC).toString()); |
45 51 | replacements.put(ChatColor.RESET, Ansi.ansi().a(Attribute.RESET).toString()); |
46 52 | } |
47 53 | |
48 54 | |
49 55 | public void sendMessage(String message) { |
50 56 | // support jansi passthrough VM option when jansi doesn't detect an ANSI supported terminal |
51 57 | if (jansiPassthrough || terminal.isAnsiSupported()) { |
52 58 | if (!conversationTracker.isConversingModaly()) { |
53 - | String result = message.replaceAll("(?i)" + ChatColor.COLOR_CHAR + "x(" + ChatColor.COLOR_CHAR + "[0-9a-f]){6}", ""); // Hex is not supported by ANSI console |
59 + | String result = convertRGBColors(message); |
54 60 | for (ChatColor color : colors) { |
55 61 | if (replacements.containsKey(color)) { |
56 62 | result = result.replaceAll("(?i)" + color.toString(), replacements.get(color)); |
57 63 | } else { |
58 64 | result = result.replaceAll("(?i)" + color.toString(), ""); |
59 65 | } |
60 66 | } |
61 67 | System.out.println(result + Ansi.ansi().reset().toString()); |
62 68 | } |
63 69 | } else { |
64 70 | super.sendMessage(message); |
65 71 | } |
66 72 | } |
67 73 | |
74 + | private static String convertRGBColors(String input) { |
75 + | Matcher matcher = RBG_TRANSLATE.matcher(input); |
76 + | StringBuffer buffer = new StringBuffer(); |
77 + | while (matcher.find()) { |
78 + | String s = matcher.group().replace("§", "").replace('x', '#'); |
79 + | Color color = Color.decode(s); |
80 + | int red = color.getRed(); |
81 + | int blue = color.getBlue(); |
82 + | int green = color.getGreen(); |
83 + | String replacement = String.format(RGB_STRING, red, green, blue); |
84 + | matcher.appendReplacement(buffer, replacement); |
85 + | } |
86 + | matcher.appendTail(buffer); |
87 + | return buffer.toString(); |
88 + | } |
89 + | |
68 90 | public static ConsoleCommandSender getInstance() { |
69 91 | if (Bukkit.getConsoleSender() != null) { |
70 92 | return Bukkit.getConsoleSender(); |
71 93 | } else { |
72 94 | return new ColouredConsoleSender(); |
73 95 | } |
74 96 | } |
75 97 | } |