Much better console interaction. History, no more losing the command, colours, inline editing, etc. Shorter log output to console (but not to file).

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
CraftBukkit/Spigot
2011-02-25 16:12:38 +00:00
parent 0114eb89e9
commit a4d2cf2a38
5 changed files with 124 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
package org.bukkit.craftbukkit.command;
import java.util.EnumMap;
import java.util.Map;
import jline.ANSIBuffer.ANSICodes;
import jline.ConsoleReader;
import jline.Terminal;
import org.bukkit.ChatColor;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.craftbukkit.CraftServer;
public class ColouredConsoleSender extends ConsoleCommandSender {
private final ConsoleReader reader;
private final Terminal terminal;
private final Map<ChatColor, String> replacements = new EnumMap<ChatColor, String>(ChatColor.class);
private final ChatColor[] colors = ChatColor.values();
public ColouredConsoleSender(CraftServer server) {
super(server);
this.reader = server.getReader();
this.terminal = reader.getTerminal();
replacements.put(ChatColor.BLACK, ANSICodes.attrib(0));
replacements.put(ChatColor.RED, ANSICodes.attrib(31));
replacements.put(ChatColor.DARK_RED, ANSICodes.attrib(31));
replacements.put(ChatColor.GREEN, ANSICodes.attrib(32));
replacements.put(ChatColor.DARK_GREEN, ANSICodes.attrib(32));
replacements.put(ChatColor.YELLOW, ANSICodes.attrib(33));
replacements.put(ChatColor.BLUE, ANSICodes.attrib(34));
replacements.put(ChatColor.DARK_BLUE, ANSICodes.attrib(34));
replacements.put(ChatColor.LIGHT_PURPLE, ANSICodes.attrib(35));
replacements.put(ChatColor.DARK_PURPLE, ANSICodes.attrib(35));
replacements.put(ChatColor.AQUA, ANSICodes.attrib(36));
replacements.put(ChatColor.WHITE, ANSICodes.attrib(37));
}
@Override
public void sendMessage(String message) {
if (terminal.isANSISupported()) {
String result = message;
for (ChatColor color : colors) {
if (replacements.containsKey(color)) {
result = result.replaceAll(color.toString(), replacements.get(color));
} else {
result = result.replaceAll(color.toString(), "");
}
}
System.out.println(result + ANSICodes.attrib(0));
} else {
super.sendMessage(message);
}
}
}