From 9cfd6ff449c2294a216181e50922196022f59fe6 Mon Sep 17 00:00:00 2001 From: Minecrell Date: Tue, 17 Jul 2018 14:48:52 +0200 Subject: [PATCH] Update TerminalConsoleAppender to 1.1.0 Fixes a race condition when using TerminalConsoleAppender that may result in IllegalStateExceptions or duplicate input prompts. --- ...Load-version-history-at-server-start.patch | 2 +- ...ams-to-redirect-System.out-err-to-lo.patch | 4 +- ...oleAppender-for-console-improvements.patch | 157 +++++++----------- 3 files changed, 65 insertions(+), 98 deletions(-) diff --git a/Spigot-Server-Patches/Load-version-history-at-server-start.patch b/Spigot-Server-Patches/Load-version-history-at-server-start.patch index 5946492c5a..ca120c060e 100644 --- a/Spigot-Server-Patches/Load-version-history-at-server-start.patch +++ b/Spigot-Server-Patches/Load-version-history-at-server-start.patch @@ -5,7 +5,7 @@ Subject: [PATCH] Load version history at server start diff --git a/src/main/java/net/minecraft/server/DedicatedServer.java b/src/main/java/net/minecraft/server/DedicatedServer.java -index 854455711..39a3d46ff 100644 +index 18d32ab76..292291592 100644 --- a/src/main/java/net/minecraft/server/DedicatedServer.java +++ b/src/main/java/net/minecraft/server/DedicatedServer.java @@ -0,0 +0,0 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer diff --git a/Spigot-Server-Patches/Use-Log4j-IOStreams-to-redirect-System.out-err-to-lo.patch b/Spigot-Server-Patches/Use-Log4j-IOStreams-to-redirect-System.out-err-to-lo.patch index 831ed4bcec..d4f366cbc9 100644 --- a/Spigot-Server-Patches/Use-Log4j-IOStreams-to-redirect-System.out-err-to-lo.patch +++ b/Spigot-Server-Patches/Use-Log4j-IOStreams-to-redirect-System.out-err-to-lo.patch @@ -12,7 +12,7 @@ results in a separate line, even though it should not result in a line break. Log4j's implementation handles it correctly. diff --git a/pom.xml b/pom.xml -index 26775156b..adf79de70 100644 +index 22b86c74a..acc4bdfa4 100644 --- a/pom.xml +++ b/pom.xml @@ -0,0 +0,0 @@ @@ -30,7 +30,7 @@ index 26775156b..adf79de70 100644 junit diff --git a/src/main/java/net/minecraft/server/DedicatedServer.java b/src/main/java/net/minecraft/server/DedicatedServer.java -index b3f1aa999..854455711 100644 +index 895ea0aad..18d32ab76 100644 --- a/src/main/java/net/minecraft/server/DedicatedServer.java +++ b/src/main/java/net/minecraft/server/DedicatedServer.java @@ -0,0 +0,0 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer diff --git a/Spigot-Server-Patches/Use-TerminalConsoleAppender-for-console-improvements.patch b/Spigot-Server-Patches/Use-TerminalConsoleAppender-for-console-improvements.patch index 3218516ec5..21ffa0a92a 100644 --- a/Spigot-Server-Patches/Use-TerminalConsoleAppender-for-console-improvements.patch +++ b/Spigot-Server-Patches/Use-TerminalConsoleAppender-for-console-improvements.patch @@ -15,12 +15,11 @@ New features: IntelliJ IDEA Other changes: - - Update JLine to 3.3.1 (from 2.12.1) - Server starts 1-2 seconds faster thanks to optimizations in Log4j configuration diff --git a/pom.xml b/pom.xml -index 9d273c6d9..26775156b 100644 +index 9d273c6d9..13fa95891 100644 --- a/pom.xml +++ b/pom.xml @@ -0,0 +0,0 @@ @@ -44,12 +43,12 @@ index 9d273c6d9..26775156b 100644 + + net.minecrell + terminalconsoleappender -+ 1.0.0 ++ 1.1.0 + + + net.java.dev.jna + jna -+ 4.4.0 ++ 4.5.2 + runtime + + @@ -88,6 +87,52 @@ index 9d273c6d9..26775156b 100644 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 000000000..116d1b3bf +--- /dev/null ++++ b/src/main/java/com/destroystokyo/paper/console/PaperConsole.java +@@ -0,0 +0,0 @@ ++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); ++ } ++ ++ @Override ++ protected void shutdown() { ++ this.server.safeShutdown(); ++ } ++ ++} 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 000000000..685deaa0e @@ -111,107 +156,29 @@ index 000000000..685deaa0e + } + +} -diff --git a/src/main/java/com/destroystokyo/paper/console/TerminalHandler.java b/src/main/java/com/destroystokyo/paper/console/TerminalHandler.java -new file mode 100644 -index 000000000..626bfeec8 ---- /dev/null -+++ b/src/main/java/com/destroystokyo/paper/console/TerminalHandler.java -@@ -0,0 +0,0 @@ -+package com.destroystokyo.paper.console; -+ -+import net.minecraft.server.DedicatedServer; -+import net.minecrell.terminalconsole.TerminalConsoleAppender; -+import org.bukkit.craftbukkit.command.ConsoleCommandCompleter; -+import org.jline.reader.EndOfFileException; -+import org.jline.reader.LineReader; -+import org.jline.reader.LineReaderBuilder; -+import org.jline.reader.UserInterruptException; -+import org.jline.terminal.Terminal; -+ -+public class TerminalHandler { -+ -+ private TerminalHandler() { -+ } -+ -+ public static boolean handleCommands(DedicatedServer server) { -+ final Terminal terminal = TerminalConsoleAppender.getTerminal(); -+ if (terminal == null) { -+ return false; -+ } -+ -+ LineReader reader = LineReaderBuilder.builder() -+ .appName("Paper") -+ .terminal(terminal) -+ .completer(new ConsoleCommandCompleter(server)) -+ .build(); -+ reader.setOpt(LineReader.Option.DISABLE_EVENT_EXPANSION); -+ reader.unsetOpt(LineReader.Option.INSERT_TAB); -+ -+ TerminalConsoleAppender.setReader(reader); -+ -+ try { -+ String line; -+ while (!server.isStopped() && server.isRunning()) { -+ try { -+ line = reader.readLine("> "); -+ } catch (EndOfFileException ignored) { -+ // Continue reading after EOT -+ continue; -+ } -+ -+ if (line == null) { -+ break; -+ } -+ -+ line = line.trim(); -+ if (!line.isEmpty()) { -+ server.issueCommand(line, server); -+ } -+ } -+ } catch (UserInterruptException e) { -+ server.safeShutdown(); -+ } finally { -+ TerminalConsoleAppender.setReader(null); -+ } -+ -+ return true; -+ } -+ -+} diff --git a/src/main/java/net/minecraft/server/DedicatedServer.java b/src/main/java/net/minecraft/server/DedicatedServer.java -index 8f2afcc32..b3f1aa999 100644 +index 8f2afcc32..895ea0aad 100644 --- a/src/main/java/net/minecraft/server/DedicatedServer.java +++ b/src/main/java/net/minecraft/server/DedicatedServer.java @@ -0,0 +0,0 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer if (!org.bukkit.craftbukkit.Main.useConsole) { return; } -- jline.console.ConsoleReader bufferedreader = reader; -+ // Paper start - Use TerminalConsoleAppender implementation -+ if (com.destroystokyo.paper.console.TerminalHandler.handleCommands(DedicatedServer.this)) return; -+ BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)); -+ // Paper end ++ // Paper start - Use TerminalConsoleAppender ++ new com.destroystokyo.paper.console.PaperConsole(DedicatedServer.this).start(); ++ /* + jline.console.ConsoleReader bufferedreader = reader; // CraftBukkit end - String s; @@ -0,0 +0,0 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer - try { - // CraftBukkit start - JLine disabling compatibility - while (!isStopped() && isRunning()) { -+ // Paper start - code is not used for jline -+ /* - if (org.bukkit.craftbukkit.Main.useJline) { - s = bufferedreader.readLine(">", null); - } else { - s = bufferedreader.readLine(); - } -+ */ -+ s = bufferedreader.readLine(); -+ // Paper end -+ - if (s != null && s.trim().length() > 0) { // Trim to filter lines which are just spaces - issueCommand(s, DedicatedServer.this); - } + DedicatedServer.LOGGER.error("Exception handling console input", ioexception); + } + ++ */ ++ // Paper end + } + }; + @@ -0,0 +0,0 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer } global.addHandler(new org.bukkit.craftbukkit.util.ForwardLogHandler());