3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-11-16 21:10:17 +01:00
Paper/CraftBukkit-Patches/0100-Highly-Optimized-Tick-Loop.patch

142 Zeilen
5.5 KiB
Diff

From a6907b6a5936948036e02fdc6ce81f554bb787e9 Mon Sep 17 00:00:00 2001
2014-01-22 09:16:12 +01:00
From: md_5 <git@md-5.net>
Date: Wed, 22 Jan 2014 19:14:15 +1100
Subject: [PATCH] Highly Optimized Tick Loop
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 588ce0a..0ca1bb6 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2014-01-22 09:16:12 +01:00
@@ -101,6 +101,11 @@ public abstract class MinecraftServer implements ICommandListener, Runnable, IMo
public java.util.Queue<Runnable> processQueue = new java.util.concurrent.ConcurrentLinkedQueue<Runnable>();
public int autosavePeriod;
// CraftBukkit end
+ // Spigot start
+ private static final int TPS = 20;
+ private static final int TICK_TIME = 1000000000 / TPS;
+ public static double currentTPS = 0;
+ // Spigot end
2013-12-01 04:40:53 +01:00
public MinecraftServer(OptionSet options, Proxy proxy) { // CraftBukkit - signature file -> OptionSet
i = this;
@@ -429,38 +434,25 @@ public abstract class MinecraftServer implements ICommandListener, Runnable, IMo
2014-01-22 09:16:12 +01:00
this.p.setServerInfo(new ServerPingServerData("1.7.2", 4));
this.a(this.p);
+ // Spigot start
+ long lastTick = 0, catchupTime = 0, curTime, wait;
while (this.isRunning) {
2013-12-01 04:40:53 +01:00
- long k = ap();
- long l = k - i;
-
2013-12-01 04:40:53 +01:00
- if (l > 2000L && i - this.O >= 15000L) {
- if (this.server.getWarnOnOverload()) // CraftBukkit - Added option to suppress warning messages
2013-12-01 04:40:53 +01:00
- h.warn("Can\'t keep up! Did the system time change, or is the server overloaded? Running {}ms behind, skipping {} tick(s)", new Object[] { Long.valueOf(l), Long.valueOf(l / 50L)});
- l = 2000L;
2013-12-01 04:40:53 +01:00
- this.O = i;
- }
-
- if (l < 0L) {
2013-12-01 04:40:53 +01:00
- h.warn("Time ran backwards! Did the system time change?");
- l = 0L;
- }
-
- j += l;
- i = k;
- if (this.worlds.get(0).everyoneDeeplySleeping()) { // CraftBukkit
2013-12-01 04:40:53 +01:00
- this.t();
- j = 0L;
2014-01-22 09:16:12 +01:00
+ curTime = System.nanoTime();
+ wait = TICK_TIME - (curTime - lastTick) - catchupTime;
+ if (wait > 0) {
+ Thread.sleep(wait / 1000000);
+ catchupTime = 0;
+ continue;
} else {
- while (j > 50L) {
- MinecraftServer.currentTick = (int) (System.currentTimeMillis() / 50); // CraftBukkit
- j -= 50L;
2013-12-01 04:40:53 +01:00
- this.t();
- }
2014-01-22 09:16:12 +01:00
+ catchupTime = Math.min(1000000000, Math.abs(wait));
}
-
- Thread.sleep(1L);
+ currentTPS = (currentTPS * 0.95) + (1E9 / (curTime - lastTick) * 0.05);
+ lastTick = curTime;
+ MinecraftServer.currentTick++;
2013-12-01 04:40:53 +01:00
+ this.t();
2014-01-22 09:16:12 +01:00
this.N = true;
}
+ // Spigot end
} else {
this.a((CrashReport) null);
}
diff --git a/src/main/java/org/bukkit/craftbukkit/command/TicksPerSecondCommand.java b/src/main/java/org/bukkit/craftbukkit/command/TicksPerSecondCommand.java
new file mode 100644
index 0000000..f114a31
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/command/TicksPerSecondCommand.java
@@ -0,0 +1,35 @@
+package org.bukkit.craftbukkit.command;
+
+import net.minecraft.server.MinecraftServer;
+import org.bukkit.ChatColor;
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandSender;
+
+public class TicksPerSecondCommand extends Command {
+
+ public TicksPerSecondCommand(String name) {
+ super(name);
+ this.description = "Gets the current ticks per second for the server";
+ this.usageMessage = "/tps";
+ this.setPermission("bukkit.command.tps");
+ }
+
+ @Override
+ public boolean execute(CommandSender sender, String currentAlias, String[] args) {
+ if (!testPermission(sender)) return true;
+
+ double tps = Math.min(20, Math.round(MinecraftServer.currentTPS * 10) / 10.0);
+ ChatColor color;
+ if (tps > 19.2D) {
+ color = ChatColor.GREEN;
+ } else if (tps > 17.4D) {
+ color = ChatColor.YELLOW;
+ } else {
+ color = ChatColor.RED;
+ }
+
+ sender.sendMessage(ChatColor.GOLD + "[TPS] " + color + tps);
+
+ return true;
+ }
+}
diff --git a/src/main/java/org/spigotmc/SpigotConfig.java b/src/main/java/org/spigotmc/SpigotConfig.java
2014-01-22 09:19:39 +01:00
index 205249e..4319e9b 100755
--- a/src/main/java/org/spigotmc/SpigotConfig.java
+++ b/src/main/java/org/spigotmc/SpigotConfig.java
2014-01-22 09:19:39 +01:00
@@ -20,6 +20,7 @@ import org.bukkit.command.Command;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
+import org.bukkit.craftbukkit.command.TicksPerSecondCommand;
public class SpigotConfig
{
2014-01-22 09:19:39 +01:00
@@ -248,4 +249,9 @@ public class SpigotConfig
"screen." );
}
}
+
2013-06-21 10:57:20 +02:00
+ private static void tpsCommand()
+ {
+ commands.put( "tps", new TicksPerSecondCommand( "tps" ) );
+ }
}
--
2013-12-01 04:40:53 +01:00
1.8.3.2