fix: server has no tick catch up limit

This commit is contained in:
DeidaraMC 2024-03-29 16:16:06 -04:00 committed by iam
parent b0e38f14b9
commit 154059468e
2 changed files with 12 additions and 4 deletions

View File

@ -13,6 +13,7 @@ public final class ServerFlag {
// Server Behavior
public static final int SERVER_TICKS_PER_SECOND = Integer.getInteger("minestom.tps", 20);
public static final int SERVER_MAX_TICK_CATCH_UP = Integer.getInteger("minestom.max-tick-catch-up", 5);
public static final int CHUNK_VIEW_DISTANCE = Integer.getInteger("minestom.chunk-view-distance", 8);
public static final int ENTITY_VIEW_DISTANCE = Integer.getInteger("minestom.entity-view-distance", 5);
public static final int ENTITY_SYNCHRONIZATION_TICKS = Integer.getInteger("minestom.entity-synchronization-ticks", 20);

View File

@ -21,7 +21,8 @@ public final class TickSchedulerThread extends MinestomThread {
@Override
public void run() {
long timeOverslept = 0;
long ticks = 0;
long baseTime = System.nanoTime();
while (serverProcess.isAlive()) {
final long tickStart = System.nanoTime();
try {
@ -30,10 +31,16 @@ public final class TickSchedulerThread extends MinestomThread {
serverProcess.exception().handleException(e);
}
long tickEnd = System.nanoTime();
long nextTickTime = tickEnd + TICK_TIME_NANOS - (tickEnd - tickStart) - timeOverslept;
ticks++;
long nextTickTime = baseTime + ticks * TICK_TIME_NANOS;
waitUntilNextTick(nextTickTime);
timeOverslept = System.nanoTime() - nextTickTime;
// Check if the server can not keep up with the tickrate
// if it gets too far behind, reset the ticks & baseTime
// to avoid running too many ticks at once
if (System.nanoTime() > nextTickTime + TICK_TIME_NANOS * ServerFlag.SERVER_MAX_TICK_CATCH_UP) {
baseTime = System.nanoTime();
ticks = 0;
}
}
}