Use TerminalConsoleAppender for console improvements

Rewrite console improvements (console colors, tab completion,
persistent input line, ...) using JLine 3.x and TerminalConsoleAppender.

Also uses the new ANSIComponentSerializer to serialize components when
logging them via the ComponentLogger, or when sending messages to the
console, for hex color support.

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

Co-Authored-By: Emilia Kond <emilia@rymiel.space>
This commit is contained in:
Minecrell 2017-06-09 19:03:43 +02:00
commit 36723cdd60
19 changed files with 343 additions and 133 deletions

View file

@ -49,7 +49,7 @@
+import com.mojang.serialization.Lifecycle;
+import java.io.File;
+import java.util.Random;
+import jline.console.ConsoleReader;
+// import jline.console.ConsoleReader; // Paper
+import joptsimple.OptionSet;
+import net.minecraft.nbt.NbtException;
+import net.minecraft.nbt.ReportedNbtException;
@ -118,17 +118,15 @@
private int playerIdleTimeout;
private final long[] tickTimesNanos;
private long aggregatedTickTimesNanos;
@@ -276,6 +301,26 @@
private static final AtomicReference<RuntimeException> fatalException = new AtomicReference();
@@ -277,6 +302,25 @@
private final SuppressedExceptionCollector suppressedExceptions;
private final DiscontinuousFrame tickFrame;
+
+ // CraftBukkit start
+ public final WorldLoader.DataLoadContext worldLoader;
+ public org.bukkit.craftbukkit.CraftServer server;
+ public OptionSet options;
+ public org.bukkit.command.ConsoleCommandSender console;
+ public ConsoleReader reader;
+ public static int currentTick = (int) (System.currentTimeMillis() / 50);
+ public java.util.Queue<Runnable> processQueue = new java.util.concurrent.ConcurrentLinkedQueue<Runnable>();
+ public int autosavePeriod;
@ -142,10 +140,11 @@
+ public final double[] recentTps = new double[ 3 ];
+ // Spigot end
+ public final io.papermc.paper.configuration.PaperConfigurations paperConfigurations; // Paper - add paper configuration files
+
public static <S extends MinecraftServer> S spin(Function<Thread, S> serverFactory) {
AtomicReference<S> atomicreference = new AtomicReference();
@@ -290,14 +335,14 @@
Thread thread = new Thread(() -> {
@@ -290,14 +334,14 @@
thread.setPriority(8);
}
@ -162,7 +161,7 @@
super("Server");
this.metricsRecorder = InactiveMetricsRecorder.INSTANCE;
this.onMetricsRecordingStopped = (methodprofilerresults) -> {
@@ -319,36 +364,64 @@
@@ -319,36 +363,68 @@
this.scoreboard = new ServerScoreboard(this);
this.customBossEvents = new CustomBossEvents();
this.suppressedExceptions = new SuppressedExceptionCollector();
@ -215,7 +214,9 @@
+ this.options = options;
+ this.worldLoader = worldLoader;
+ this.vanillaCommandDispatcher = worldstem.dataPackResources().commands; // CraftBukkit
+ // Paper start - Handled by TerminalConsoleAppender
+ // Try to see if we're actually running in a terminal, disable jline if not
+ /*
+ if (System.console() == null && System.getProperty("jline.terminal") == null) {
+ System.setProperty("jline.terminal", "jline.UnsupportedTerminal");
+ Main.useJline = false;
@ -236,13 +237,15 @@
+ MinecraftServer.LOGGER.warn((String) null, ex);
+ }
+ }
+ */
+ // Paper end
+ Runtime.getRuntime().addShutdownHook(new org.bukkit.craftbukkit.util.ServerShutdownThread(this));
+ // CraftBukkit end
+ this.paperConfigurations = services.paperConfigurations(); // Paper - add paper configuration files
}
private void readScoreboard(DimensionDataStorage persistentStateManager) {
@@ -357,7 +430,7 @@
@@ -357,7 +433,7 @@
protected abstract boolean initServer() throws IOException;
@ -251,7 +254,7 @@
if (!JvmProfiler.INSTANCE.isRunning()) {
;
}
@@ -365,12 +438,8 @@
@@ -365,12 +441,8 @@
boolean flag = false;
ProfiledDuration profiledduration = JvmProfiler.INSTANCE.onWorldLoadedStarted();
@ -265,7 +268,7 @@
if (profiledduration != null) {
profiledduration.finish(true);
}
@@ -387,23 +456,217 @@
@@ -387,23 +459,217 @@
protected void forceDifficulty() {}
@ -497,7 +500,7 @@
if (!iworlddataserver.isInitialized()) {
try {
@@ -427,30 +690,8 @@
@@ -427,30 +693,8 @@
iworlddataserver.setInitialized(true);
}
@ -529,7 +532,7 @@
private static void setInitialSpawn(ServerLevel world, ServerLevelData worldProperties, boolean bonusChest, boolean debugWorld) {
if (debugWorld) {
@@ -458,6 +699,21 @@
@@ -458,6 +702,21 @@
} else {
ServerChunkCache chunkproviderserver = world.getChunkSource();
ChunkPos chunkcoordintpair = new ChunkPos(chunkproviderserver.randomState().sampler().findSpawnPosition());
@ -551,7 +554,7 @@
int i = chunkproviderserver.getGenerator().getSpawnHeight(world);
if (i < world.getMinY()) {
@@ -516,31 +772,36 @@
@@ -516,31 +775,36 @@
iworlddataserver.setGameType(GameType.SPECTATOR);
}
@ -599,7 +602,7 @@
ForcedChunksSavedData forcedchunk = (ForcedChunksSavedData) worldserver1.getDataStorage().get(ForcedChunksSavedData.factory(), "chunks");
if (forcedchunk != null) {
@@ -555,10 +816,17 @@
@@ -555,10 +819,17 @@
}
}
@ -621,7 +624,7 @@
}
public GameType getDefaultGameType() {
@@ -588,12 +856,16 @@
@@ -588,12 +859,16 @@
worldserver.save((ProgressListener) null, flush, worldserver.noSave && !force);
}
@ -640,7 +643,7 @@
if (flush) {
Iterator iterator1 = this.getAllLevels().iterator();
@@ -628,18 +900,40 @@
@@ -628,18 +903,40 @@
this.stopServer();
}
@ -681,7 +684,7 @@
}
MinecraftServer.LOGGER.info("Saving worlds");
@@ -693,6 +987,15 @@
@@ -693,6 +990,15 @@
} catch (IOException ioexception1) {
MinecraftServer.LOGGER.error("Failed to unlock level {}", this.storageSource.getLevelId(), ioexception1);
}
@ -697,21 +700,23 @@
}
@@ -720,6 +1023,13 @@
}
@@ -717,8 +1023,15 @@
MinecraftServer.LOGGER.error("Error while shutting down", interruptedexception);
}
}
+
+ }
+ // Spigot Start
+ private static double calcTps(double avg, double exp, double tps)
+ {
+ return ( avg * exp ) + ( tps * ( 1 - exp ) );
+ }
}
+ // Spigot End
+
protected void runServer() {
try {
if (!this.initServer()) {
@@ -727,9 +1037,12 @@
@@ -727,9 +1040,12 @@
}
this.nextTickTimeNanos = Util.getNanos();
@ -725,7 +730,7 @@
while (this.running) {
long i;
@@ -744,11 +1057,23 @@
@@ -744,11 +1060,23 @@
if (j > MinecraftServer.OVERLOADED_THRESHOLD_NANOS + 20L * i && this.nextTickTimeNanos - this.lastOverloadWarningNanos >= MinecraftServer.OVERLOADED_WARNING_INTERVAL_NANOS + 100L * i) {
long k = j / i;
@ -749,7 +754,7 @@
boolean flag = i == 0L;
@@ -757,6 +1082,7 @@
@@ -757,6 +1085,7 @@
this.debugCommandProfiler = new MinecraftServer.TimeProfiler(Util.getNanos(), this.tickCount);
}
@ -757,21 +762,21 @@
this.nextTickTimeNanos += i;
try {
@@ -830,6 +1156,13 @@
@@ -830,6 +1159,13 @@
this.services.profileCache().clearExecutor();
}
+ org.spigotmc.WatchdogThread.doStop(); // Spigot
+ // CraftBukkit start - Restore terminal to original settings
+ try {
+ this.reader.getTerminal().restore();
+ net.minecrell.terminalconsole.TerminalConsoleAppender.close(); // Paper - Use TerminalConsoleAppender
+ } catch (Exception ignored) {
+ }
+ // CraftBukkit end
this.onServerExit();
}
@@ -889,9 +1222,16 @@
@@ -889,9 +1225,16 @@
}
private boolean haveTime() {
@ -789,7 +794,7 @@
public static boolean throwIfFatalException() {
RuntimeException runtimeexception = (RuntimeException) MinecraftServer.fatalException.get();
@@ -903,7 +1243,7 @@
@@ -903,7 +1246,7 @@
}
public static void setFatalException(RuntimeException exception) {
@ -798,7 +803,7 @@
}
@Override
@@ -977,7 +1317,7 @@
@@ -977,7 +1320,7 @@
}
}
@ -807,7 +812,7 @@
Profiler.get().incrementCounter("runTask");
super.doRunTask(ticktask);
}
@@ -1025,6 +1365,7 @@
@@ -1025,6 +1368,7 @@
}
public void tickServer(BooleanSupplier shouldKeepTicking) {
@ -815,7 +820,7 @@
long i = Util.getNanos();
int j = this.pauseWhileEmptySeconds() * 20;
@@ -1041,11 +1382,13 @@
@@ -1041,11 +1385,13 @@
this.autoSave();
}
@ -829,7 +834,7 @@
++this.tickCount;
this.tickRateManager.tick();
this.tickChildren(shouldKeepTicking);
@@ -1055,7 +1398,7 @@
@@ -1055,7 +1401,7 @@
}
--this.ticksUntilAutosave;
@ -838,7 +843,7 @@
this.autoSave();
}
@@ -1071,10 +1414,13 @@
@@ -1071,10 +1417,13 @@
this.smoothedTickTimeMillis = this.smoothedTickTimeMillis * 0.8F + (float) k / (float) TimeUtil.NANOSECONDS_PER_MILLISECOND * 0.19999999F;
this.logTickMethodTime(i);
gameprofilerfiller.pop();
@ -853,7 +858,7 @@
MinecraftServer.LOGGER.debug("Autosave started");
ProfilerFiller gameprofilerfiller = Profiler.get();
@@ -1082,6 +1428,7 @@
@@ -1082,6 +1431,7 @@
this.saveEverything(true, false, false);
gameprofilerfiller.pop();
MinecraftServer.LOGGER.debug("Autosave finished");
@ -861,7 +866,7 @@
}
private void logTickMethodTime(long tickStartTime) {
@@ -1123,7 +1470,7 @@
@@ -1123,7 +1473,7 @@
private ServerStatus buildServerStatus() {
ServerStatus.Players serverping_serverpingplayersample = this.buildPlayerStatus();
@ -870,7 +875,7 @@
}
private ServerStatus.Players buildPlayerStatus() {
@@ -1154,11 +1501,35 @@
@@ -1154,11 +1504,35 @@
this.getPlayerList().getPlayers().forEach((entityplayer) -> {
entityplayer.connection.suspendFlushing();
});
@ -906,7 +911,7 @@
while (iterator.hasNext()) {
ServerLevel worldserver = (ServerLevel) iterator.next();
@@ -1167,16 +1538,20 @@
@@ -1167,16 +1541,20 @@
return s + " " + String.valueOf(worldserver.dimension().location());
});
@ -927,7 +932,7 @@
} catch (Throwable throwable) {
CrashReport crashreport = CrashReport.forThrowable(throwable, "Exception ticking world");
@@ -1189,18 +1564,24 @@
@@ -1189,18 +1567,24 @@
}
gameprofilerfiller.popPush("connection");
@ -952,7 +957,7 @@
gameprofilerfiller.popPush("send chunks");
iterator = this.playerList.getPlayers().iterator();
@@ -1267,6 +1648,22 @@
@@ -1267,6 +1651,22 @@
return (ServerLevel) this.levels.get(key);
}
@ -975,7 +980,7 @@
public Set<ResourceKey<Level>> levelKeys() {
return this.levels.keySet();
}
@@ -1296,7 +1693,7 @@
@@ -1296,7 +1696,7 @@
@DontObfuscate
public String getServerModName() {
@ -984,7 +989,16 @@
}
public SystemReport fillSystemReport(SystemReport details) {
@@ -1481,10 +1878,20 @@
@@ -1347,7 +1747,7 @@
@Override
public void sendSystemMessage(Component message) {
- MinecraftServer.LOGGER.info(message.getString());
+ MinecraftServer.LOGGER.info(io.papermc.paper.adventure.PaperAdventure.ANSI_SERIALIZER.serialize(io.papermc.paper.adventure.PaperAdventure.asAdventure(message))); // Paper - Log message with colors
}
public KeyPair getKeyPair() {
@@ -1481,10 +1881,20 @@
@Override
public String getMotd() {
@ -1006,7 +1020,7 @@
this.motd = motd;
}
@@ -1507,7 +1914,7 @@
@@ -1507,7 +1917,7 @@
}
public ServerConnectionListener getConnection() {
@ -1015,7 +1029,7 @@
}
public boolean isReady() {
@@ -1634,11 +2041,11 @@
@@ -1634,11 +2044,11 @@
public CompletableFuture<Void> reloadResources(Collection<String> dataPacks) {
CompletableFuture<Void> completablefuture = CompletableFuture.supplyAsync(() -> {
@ -1029,7 +1043,7 @@
}, this).thenCompose((immutablelist) -> {
MultiPackResourceManager resourcemanager = new MultiPackResourceManager(PackType.SERVER_DATA, immutablelist);
List<Registry.PendingTags<?>> list = TagLoader.loadTagsForExistingRegistries(resourcemanager, this.registries.compositeAccess());
@@ -1654,6 +2061,7 @@
@@ -1654,6 +2064,7 @@
}).thenAcceptAsync((minecraftserver_reloadableresources) -> {
this.resources.close();
this.resources = minecraftserver_reloadableresources;
@ -1037,7 +1051,7 @@
this.packRepository.setSelected(dataPacks);
WorldDataConfiguration worlddataconfiguration = new WorldDataConfiguration(MinecraftServer.getSelectedPacks(this.packRepository, true), this.worldData.enabledFeatures());
@@ -1952,7 +2360,7 @@
@@ -1952,7 +2363,7 @@
final List<String> list = Lists.newArrayList();
final GameRules gamerules = this.getGameRules();
@ -1046,7 +1060,7 @@
@Override
public <T extends GameRules.Value<T>> void visit(GameRules.Key<T> key, GameRules.Type<T> type) {
list.add(String.format(Locale.ROOT, "%s=%s\n", key.getId(), gamerules.getRule(key)));
@@ -2058,7 +2466,7 @@
@@ -2058,7 +2469,7 @@
try {
label51:
{
@ -1055,7 +1069,7 @@
try {
arraylist = Lists.newArrayList(NativeModuleLister.listModules());
@@ -2105,9 +2513,25 @@
@@ -2105,9 +2516,25 @@
if (bufferedwriter != null) {
bufferedwriter.close();
}
@ -1081,7 +1095,7 @@
private ProfilerFiller createProfiler() {
if (this.willStartRecordingMetrics) {
this.metricsRecorder = ActiveMetricsRecorder.createStarted(new ServerMetricsSamplersProvider(Util.timeSource, this.isDedicatedServer()), Util.timeSource, Util.ioPool(), new MetricsPersister("server"), this.onMetricsRecordingStopped, (path) -> {
@@ -2225,18 +2649,24 @@
@@ -2225,18 +2652,24 @@
}
public void logChatMessage(Component message, ChatType.Bound params, @Nullable String prefix) {