From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Minecrell Date: Fri, 9 Jun 2017 19:03:43 +0200 Subject: [PATCH] Use TerminalConsoleAppender for console improvements Rewrite console improvements (console colors, tab completion, persistent input line, ...) using JLine 3.x and TerminalConsoleAppender. New features: - Support console colors for Vanilla commands - Add console colors for warnings and errors - Server can now be turned off safely using CTRL + C. JLine catches the signal and the implementation shuts down the server cleanly. - Support console colors and persistent input line when running in IntelliJ IDEA Other changes: - Server starts 1-2 seconds faster thanks to optimizations in Log4j configuration diff --git a/pom.xml b/pom.xml index aec55b0b78fa47d427652d402cfbb61295672b11..48f5ed765cab101633efdcdd9118fd3cb5f97914 100644 --- a/pom.xml +++ b/pom.xml @@ -44,10 +44,27 @@ compile - jline - jline - 2.12.1 - compile + net.minecrell + terminalconsoleappender + 1.2.0 + + + org.jline + jline-terminal-jansi + 3.12.1 + runtime + + + + org.apache.logging.log4j + log4j-core + 2.8.1 + runtime org.ow2.asm @@ -264,10 +281,18 @@ META-INF/services/java.sql.Driver + + + + com.github.edwgiz + maven-shade-plugin.log4j2-cachefile-transformer + 2.13.1 + + org.apache.maven.plugins diff --git a/src/main/java/com/destroystokyo/paper/console/PaperConsole.java b/src/main/java/com/destroystokyo/paper/console/PaperConsole.java new file mode 100644 index 0000000000000000000000000000000000000000..cd6e259239b068f1e1b33f45004cf5d37ac5a1a3 --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/console/PaperConsole.java @@ -0,0 +1,40 @@ +package com.destroystokyo.paper.console; + +import net.minecraft.server.DedicatedServer; +import net.minecrell.terminalconsole.SimpleTerminalConsole; +import org.bukkit.craftbukkit.command.ConsoleCommandCompleter; +import org.jline.reader.LineReader; +import org.jline.reader.LineReaderBuilder; + +public final class PaperConsole extends SimpleTerminalConsole { + + private final DedicatedServer server; + + public PaperConsole(DedicatedServer server) { + this.server = server; + } + + @Override + protected LineReader buildReader(LineReaderBuilder builder) { + return super.buildReader(builder + .appName("Paper") + .completer(new ConsoleCommandCompleter(this.server)) + ); + } + + @Override + protected boolean isRunning() { + return !this.server.isStopped() && this.server.isRunning(); + } + + @Override + protected void runCommand(String command) { + this.server.issueCommand(command, this.server.getServerCommandListener()); + } + + @Override + protected void shutdown() { + this.server.safeShutdown(false); + } + +} diff --git a/src/main/java/com/destroystokyo/paper/console/TerminalConsoleCommandSender.java b/src/main/java/com/destroystokyo/paper/console/TerminalConsoleCommandSender.java new file mode 100644 index 0000000000000000000000000000000000000000..685deaa0e5d1ddc13e3a7c0471b1cfcf1710c869 --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/console/TerminalConsoleCommandSender.java @@ -0,0 +1,17 @@ +package com.destroystokyo.paper.console; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.bukkit.craftbukkit.command.CraftConsoleCommandSender; + +public class TerminalConsoleCommandSender extends CraftConsoleCommandSender { + + private static final Logger LOGGER = LogManager.getRootLogger(); + + @Override + public void sendRawMessage(String message) { + // TerminalConsoleAppender supports color codes directly in log messages + LOGGER.info(message); + } + +} diff --git a/src/main/java/net/minecraft/server/DedicatedServer.java b/src/main/java/net/minecraft/server/DedicatedServer.java index 4b1f8c53737f998fa57859146d5ddb999cdc8d41..d34f772fae3543cec6a130831b1f3eaa67934944 100644 --- a/src/main/java/net/minecraft/server/DedicatedServer.java +++ b/src/main/java/net/minecraft/server/DedicatedServer.java @@ -86,6 +86,9 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer if (!org.bukkit.craftbukkit.Main.useConsole) { return; } + // Paper start - Use TerminalConsoleAppender + new com.destroystokyo.paper.console.PaperConsole(DedicatedServer.this).start(); + /* jline.console.ConsoleReader bufferedreader = reader; // MC-33041, SPIGOT-5538: if System.in is not valid due to javaw, then return @@ -125,6 +128,8 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer DedicatedServer.LOGGER.error("Exception handling console input", ioexception); } + */ + // Paper end } }; @@ -136,6 +141,9 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer } global.addHandler(new org.bukkit.craftbukkit.util.ForwardLogHandler()); + // Paper start - Not needed with TerminalConsoleAppender + final org.apache.logging.log4j.Logger logger = LogManager.getRootLogger(); + /* final org.apache.logging.log4j.core.Logger logger = ((org.apache.logging.log4j.core.Logger) LogManager.getRootLogger()); for (org.apache.logging.log4j.core.Appender appender : logger.getAppenders().values()) { if (appender instanceof org.apache.logging.log4j.core.appender.ConsoleAppender) { @@ -144,6 +152,8 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer } new org.bukkit.craftbukkit.util.TerminalConsoleWriterThread(System.out, this.reader).start(); + */ + // Paper end System.setOut(new PrintStream(new LoggerOutputStream(logger, Level.INFO), true)); System.setErr(new PrintStream(new LoggerOutputStream(logger, Level.WARN), true)); diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java index 80d8b0b0eac47b8d8e62db60da9daf0da8671fb3..87595425a358a13c8f2393619d51a981140556cf 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -57,7 +57,7 @@ import org.apache.commons.lang3.Validate; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; // CraftBukkit start -import jline.console.ConsoleReader; +import joptsimple.OptionSet; import org.bukkit.Bukkit; import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.Main; @@ -161,7 +161,7 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant processQueue = new java.util.concurrent.ConcurrentLinkedQueue(); public int autosavePeriod; @@ -212,7 +212,9 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant replacements = new EnumMap(ChatColor.class); private final ChatColor[] colors = ChatColor.values(); @@ -71,5 +71,5 @@ public class ColouredConsoleSender extends CraftConsoleCommandSender { } else { return new ColouredConsoleSender(); } - } + }*/ // Paper } diff --git a/src/main/java/org/bukkit/craftbukkit/command/ConsoleCommandCompleter.java b/src/main/java/org/bukkit/craftbukkit/command/ConsoleCommandCompleter.java index befcc19f9b56df9096b98a23b0020f1db793ea5b..5510266fb114954322823b72e3199f33c4d7a9a7 100644 --- a/src/main/java/org/bukkit/craftbukkit/command/ConsoleCommandCompleter.java +++ b/src/main/java/org/bukkit/craftbukkit/command/ConsoleCommandCompleter.java @@ -4,20 +4,31 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.logging.Level; -import jline.console.completer.Completer; import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.util.Waitable; + +// Paper start - JLine update +import net.minecraft.server.DedicatedServer; // Paper +import org.jline.reader.Candidate; +import org.jline.reader.Completer; +import org.jline.reader.LineReader; +import org.jline.reader.ParsedLine; +// Paper end import org.bukkit.event.server.TabCompleteEvent; public class ConsoleCommandCompleter implements Completer { - private final CraftServer server; + private final DedicatedServer server; // Paper - CraftServer -> DedicatedServer - public ConsoleCommandCompleter(CraftServer server) { + public ConsoleCommandCompleter(DedicatedServer server) { // Paper - CraftServer -> DedicatedServer this.server = server; } + // Paper start - Change method signature for JLine update @Override - public int complete(final String buffer, final int cursor, final List candidates) { + public void complete(LineReader reader, ParsedLine line, List candidates) { + final CraftServer server = this.server.server; + final String buffer = line.line(); + // Paper end Waitable> waitable = new Waitable>() { @Override protected List evaluate() { @@ -29,25 +40,37 @@ public class ConsoleCommandCompleter implements Completer { return tabEvent.isCancelled() ? Collections.EMPTY_LIST : tabEvent.getCompletions(); } }; - this.server.getServer().processQueue.add(waitable); + server.getServer().processQueue.add(waitable); // Paper - Remove "this." try { List offers = waitable.get(); if (offers == null) { - return cursor; + return; // Paper - Method returns void + } + + // Paper start - JLine update + for (String completion : offers) { + if (completion.isEmpty()) { + continue; + } + + candidates.add(new Candidate(completion)); } - candidates.addAll(offers); + // Paper end + // Paper start - JLine handles cursor now + /* final int lastSpace = buffer.lastIndexOf(' '); if (lastSpace == -1) { return cursor - buffer.length(); } else { return cursor - (buffer.length() - lastSpace - 1); } + */ + // Paper end } catch (ExecutionException e) { - this.server.getLogger().log(Level.WARNING, "Unhandled exception when tab completing", e); + server.getLogger().log(Level.WARNING, "Unhandled exception when tab completing", e); // Paper - Remove "this." } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - return cursor; } } diff --git a/src/main/java/org/bukkit/craftbukkit/util/ServerShutdownThread.java b/src/main/java/org/bukkit/craftbukkit/util/ServerShutdownThread.java index 70f8d42992aa348ef7b2d03d22cdd59d7c73f0fe..449e99d1b673870ed6892f6ab2c715a2db35c35d 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/ServerShutdownThread.java +++ b/src/main/java/org/bukkit/craftbukkit/util/ServerShutdownThread.java @@ -17,7 +17,7 @@ public class ServerShutdownThread extends Thread { server.close(); } finally { try { - server.reader.getTerminal().restore(); + net.minecrell.terminalconsole.TerminalConsoleAppender.close(); // Paper - Use TerminalConsoleAppender } catch (Exception e) { } } diff --git a/src/main/java/org/bukkit/craftbukkit/util/TerminalConsoleWriterThread.java b/src/main/java/org/bukkit/craftbukkit/util/TerminalConsoleWriterThread.java index 99564fed7ce77e29dbdc591bcfe656af741acf8a..9a2da548b8860b496e396564b2c8f6383f020193 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/TerminalConsoleWriterThread.java +++ b/src/main/java/org/bukkit/craftbukkit/util/TerminalConsoleWriterThread.java @@ -5,12 +5,12 @@ import java.io.IOException; import java.io.OutputStream; import java.util.logging.Level; import java.util.logging.Logger; -import jline.console.ConsoleReader; +//import jline.console.ConsoleReader; import org.bukkit.craftbukkit.Main; -import org.fusesource.jansi.Ansi; -import org.fusesource.jansi.Ansi.Erase; +//import org.fusesource.jansi.Ansi; +//import org.fusesource.jansi.Ansi.Erase; -public class TerminalConsoleWriterThread extends Thread { +public class TerminalConsoleWriterThread /*extends Thread*/ {/* // Paper - disable private final ConsoleReader reader; private final OutputStream output; @@ -54,5 +54,5 @@ public class TerminalConsoleWriterThread extends Thread { Logger.getLogger(TerminalConsoleWriterThread.class.getName()).log(Level.SEVERE, null, ex); } } - } + }*/ } diff --git a/src/main/resources/log4j2.component.properties b/src/main/resources/log4j2.component.properties new file mode 100644 index 0000000000000000000000000000000000000000..0694b21465fb9e4164e71862ff24b62241b191f2 --- /dev/null +++ b/src/main/resources/log4j2.component.properties @@ -0,0 +1 @@ +log4j.skipJansi=true diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml index 722ca84968cbbbdeffd09939abff0cccd0a84010..620b9490e5f159080e50289d127404a1b56adbef 100644 --- a/src/main/resources/log4j2.xml +++ b/src/main/resources/log4j2.xml @@ -1,17 +1,14 @@ - - - - - - + + + - + @@ -24,10 +21,9 @@ - - +