2020-09-27 18:05:16 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Ivan Pekov <ivan@mrivanplays.com>
Date: Sun, 27 Sep 2020 18:30:10 +0300
Subject: [PATCH] Add last tick time API
2020-09-27 19:43:40 +02:00
Original patch by:
Co-authored-by: tr7zw <tr7zw@live.de>
2020-09-27 18:05:16 +02:00
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
2021-06-02 01:00:22 +02:00
index 3e1e5e8b23a2d6a43deb37fafc0bdc7396f2a84a..7901775e03185cf2d16ccc3d6d8bc50b7687f4db 100644
2020-09-27 18:05:16 +02:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2021-05-30 01:17:00 +02:00
@@ -1068,6 +1068,8 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
2020-09-27 18:05:16 +02:00
// Paper End
// Spigot End
+ public static java.time.Duration lastTickTime = java.time.Duration.ZERO; // Yatopia
+
protected void w() {
try {
long serverStartTime = SystemUtils.getMonotonicNanos(); // Paper
2021-05-30 01:17:00 +02:00
@@ -1139,7 +1141,9 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
2020-09-27 18:05:16 +02:00
this.a(gameprofilertick);
2020-11-26 10:52:33 +01:00
this.methodProfiler.a();
this.methodProfiler.enter("tick");
2020-09-27 18:05:16 +02:00
+ long tickStart = System.nanoTime(); // Yatopia
this.a(this::canSleepForTick);
+ lastTickTime = java.time.Duration.ofNanos(System.nanoTime() - tickStart); // Yatopia
2020-11-26 10:52:33 +01:00
this.methodProfiler.exitEnter("nextTickWait");
2020-09-27 18:05:16 +02:00
this.X = true;
2021-01-21 11:58:52 +01:00
// Purpur start - tps catchup
2020-09-27 18:05:16 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
2021-06-02 01:00:22 +02:00
index 413d32ba0383eeb28d94bcab77bc2c5e63435225..3fb32dff91742c1ccca5a970333246186359a51d 100644
2020-09-27 18:05:16 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
2021-06-02 01:00:22 +02:00
@@ -2573,4 +2573,6 @@ public final class CraftServer implements Server {
2020-09-27 18:05:16 +02:00
return getServer().lagging;
}
// Purpur end
+
+ @Override public java.time.Duration getLastTickTime() { return net.minecraft.server.MinecraftServer.lastTickTime; } // Yatopia
}
diff --git a/src/main/java/org/spigotmc/TicksPerSecondCommand.java b/src/main/java/org/spigotmc/TicksPerSecondCommand.java
2021-02-08 16:31:51 +01:00
index 312248b5c6febc6a3c10940714fac7fbc3c53b7f..abc9b7aa53e554ef2196dceafe8db8ba65f34e84 100644
2020-09-27 18:05:16 +02:00
--- a/src/main/java/org/spigotmc/TicksPerSecondCommand.java
+++ b/src/main/java/org/spigotmc/TicksPerSecondCommand.java
2021-02-08 16:31:51 +01:00
@@ -32,6 +32,10 @@ public class TicksPerSecondCommand extends Command
2020-09-27 18:05:16 +02:00
tpsAvg[i] = format( tps[i] );
}
sender.sendMessage(ChatColor.GOLD + "TPS from last 5s, 1m, 5m, 15m: " + org.apache.commons.lang.StringUtils.join(tpsAvg, ", ")); // Purpur
+ // Yatopia start
+ java.time.Duration lastTickTime = org.bukkit.Bukkit.getLastTickTime();
2020-10-05 12:48:12 +02:00
+ sender.sendMessage(ChatColor.GOLD + "Last tick: " + formatTo( lastTickTime, java.util.concurrent.TimeUnit.MILLISECONDS ) + " (" + formatTo( lastTickTime, java.util.concurrent.TimeUnit.NANOSECONDS ) + ")");
2020-09-27 18:05:16 +02:00
+ // Yatopia end
if (args.length > 0 && args[0].equals("mem") && sender.hasPermission("bukkit.command.tpsmemory")) {
sender.sendMessage(ChatColor.GOLD + "Current Memory Usage: " + ChatColor.GREEN + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (1024 * 1024)) + "/" + (Runtime.getRuntime().totalMemory() / (1024 * 1024)) + " mb (Max: " + (Runtime.getRuntime().maxMemory() / (1024 * 1024)) + " mb)");
if (!hasShownMemoryWarning) {
2021-02-08 16:31:51 +01:00
@@ -50,4 +54,16 @@ public class TicksPerSecondCommand extends Command
2020-09-27 18:05:16 +02:00
return ( ( tps > 18.0 ) ? ChatColor.GREEN : ( tps > 16.0 ) ? ChatColor.YELLOW : ChatColor.RED ).toString()
+ ( ( tps > 21.0 ) ? "*" : "" ) + Math.min( Math.round( tps * 100.0 ) / 100.0, 20.0 ); // Paper - only print * at 21, we commonly peak to 20.02 as the tick sleep is not accurate enough, stop the noise
}
+
+ // Yatopia start
+ public static String formatTo(java.time.Duration duration, java.util.concurrent.TimeUnit unit)
+ {
2020-09-27 19:43:40 +02:00
+ java.util.concurrent.TimeUnit nanosUnit = java.util.concurrent.TimeUnit.NANOSECONDS;
+ long nanos = duration.toNanos();
+ long toAskedUnit = unit.convert( nanos, nanosUnit );
+ long ms = nanosUnit.toMillis( nanos );
2020-09-27 18:05:16 +02:00
+ ChatColor startingColor = ms < 40 ? ChatColor.GREEN : ( ms < 50 ) ? ChatColor.YELLOW : ChatColor.RED;
2020-12-02 07:35:11 +01:00
+ return startingColor.toString() + toAskedUnit + ChatColor.GOLD + org.yatopiamc.yatopia.server.util.TimeUtils.getFriendlyName( unit );
2020-09-27 18:05:16 +02:00
+ }
+ // Yatopia end
}