2013-12-21 23:05:27 +00:00
From 00045f375cae8ca1e74ef10254c6827b095ecdb3 Mon Sep 17 00:00:00 2001
2013-02-03 01:28:39 +00:00
From: md_5 <md_5@live.com.au>
Date: Sun, 3 Feb 2013 12:28:17 +1100
2013-06-11 04:11:48 +00:00
Subject: [PATCH] Highly Optimized Tick Loop
2013-02-03 01:28:39 +00:00
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
2013-12-21 23:05:27 +00:00
index 6a01982..0966b4b 100644
2013-02-03 01:28:39 +00:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2013-12-01 03:40:53 +00:00
@@ -101,6 +101,12 @@ public abstract class MinecraftServer implements ICommandListener, Runnable, IMo
2013-02-03 01:28:39 +00:00
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;
+ private static long catchupTime = 0;
+ // Spigot end
2013-12-01 03:40:53 +00:00
public MinecraftServer(OptionSet options, Proxy proxy) { // CraftBukkit - signature file -> OptionSet
i = this;
2013-12-14 06:58:22 +00:00
@@ -422,45 +428,23 @@ public abstract class MinecraftServer implements ICommandListener, Runnable, IMo
2013-02-03 01:28:39 +00:00
public void run() {
try {
if (this.init()) {
2013-12-01 03:40:53 +00:00
- long i = ap();
- long j = 0L;
2013-02-03 01:28:39 +00:00
-
2013-12-01 03:40:53 +00:00
- this.p.setMOTD(new ChatComponentText(this.motd));
- this.p.setServerInfo(new ServerPingServerData("1.7.2", 4));
- this.a(this.p);
-
- while (this.isRunning) {
- long k = ap();
2013-02-03 01:28:39 +00:00
- long l = k - i;
-
2013-12-01 03:40:53 +00:00
- if (l > 2000L && i - this.O >= 15000L) {
2013-02-03 01:28:39 +00:00
- if (this.server.getWarnOnOverload()) // CraftBukkit - Added option to suppress warning messages
2013-12-01 03:40:53 +00: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)});
2013-02-03 01:28:39 +00:00
- l = 2000L;
2013-12-01 03:40:53 +00:00
- this.O = i;
2013-02-03 01:28:39 +00:00
- }
-
- if (l < 0L) {
2013-12-01 03:40:53 +00:00
- h.warn("Time ran backwards! Did the system time change?");
2013-02-03 01:28:39 +00:00
- l = 0L;
- }
-
- j += l;
- i = k;
- if (this.worlds.get(0).everyoneDeeplySleeping()) { // CraftBukkit
2013-12-01 03:40:53 +00:00
- this.t();
2013-02-03 01:28:39 +00:00
- j = 0L;
+ // Spigot start
2013-12-01 03:40:53 +00:00
+ for (long lastTick = 0L; this.isRunning;) {
2013-02-03 01:28:39 +00:00
+ long curTime = System.nanoTime();
+ long 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 03:40:53 +00:00
- this.t();
2013-02-03 01:28:39 +00:00
- }
2013-06-08 06:14:43 +00:00
+ catchupTime = Math.min(TICK_TIME * TPS, Math.abs(wait));
2013-02-03 01:28:39 +00:00
}
-
- Thread.sleep(1L);
2013-12-01 03:40:53 +00:00
- this.N = true;
2013-02-03 01:28:39 +00:00
+ currentTPS = (currentTPS * 0.95) + (1E9 / (curTime - lastTick) * 0.05);
+ lastTick = curTime;
+ MinecraftServer.currentTick++;
2013-12-01 03:40:53 +00:00
+ this.t();
2013-02-03 01:28:39 +00:00
}
+ // 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;
+ }
+}
2013-06-21 08:25:45 +00:00
diff --git a/src/main/java/org/spigotmc/SpigotConfig.java b/src/main/java/org/spigotmc/SpigotConfig.java
2013-12-14 06:58:22 +00:00
index 3dfe4ed..1b0e1bf 100644
2013-06-21 08:25:45 +00:00
--- a/src/main/java/org/spigotmc/SpigotConfig.java
+++ b/src/main/java/org/spigotmc/SpigotConfig.java
2013-06-22 04:07:01 +00:00
@@ -14,6 +14,7 @@ import net.minecraft.server.MinecraftServer;
import org.bukkit.Bukkit;
2013-06-21 08:25:45 +00:00
import org.bukkit.command.Command;
import org.bukkit.configuration.file.YamlConfiguration;
+import org.bukkit.craftbukkit.command.TicksPerSecondCommand;
public class SpigotConfig
{
2013-12-01 03:40:53 +00:00
@@ -117,4 +118,9 @@ public class SpigotConfig
config.addDefault( path, def );
return config.getString( path, config.getString( path ) );
2013-06-21 08:25:45 +00:00
}
+
2013-06-21 08:57:20 +00:00
+ private static void tpsCommand()
2013-06-21 08:25:45 +00:00
+ {
+ commands.put( "tps", new TicksPerSecondCommand( "tps" ) );
+ }
}
2013-02-03 01:28:39 +00:00
--
2013-12-01 03:40:53 +00:00
1.8.3.2
2013-02-03 01:28:39 +00:00