Support dynamic tick rate & prevent call from being delayed

This commit is contained in:
TheMode 2021-07-08 15:40:27 +02:00
parent f33a09fc24
commit dc89b180fb

View File

@ -15,6 +15,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.*;
import java.util.concurrent.locks.LockSupport;
import java.util.function.Consumer;
import java.util.function.LongConsumer;
@ -25,9 +26,6 @@ import java.util.function.LongConsumer;
*/
public final class UpdateManager {
private final ScheduledExecutorService updateExecutionService = Executors.newSingleThreadScheduledExecutor(r ->
new Thread(r, MinecraftServer.THREAD_NAME_TICK_SCHEDULER));
private volatile boolean stopRequested;
// TODO make configurable
@ -49,13 +47,9 @@ public final class UpdateManager {
protected void start() {
final ConnectionManager connectionManager = MinecraftServer.getConnectionManager();
updateExecutionService.scheduleAtFixedRate(() -> {
new Thread(() -> {
while (!stopRequested) {
try {
if (stopRequested) {
updateExecutionService.shutdown();
return;
}
long currentTime = System.nanoTime();
final long tickStart = System.currentTimeMillis();
@ -83,7 +77,6 @@ public final class UpdateManager {
final double tickTimeMs = tickTime / 1e6D;
final TickMonitor tickMonitor = new TickMonitor(tickTimeMs, acquisitionTimeMs);
this.tickMonitors.forEach(consumer -> consumer.accept(tickMonitor));
Acquirable.resetAcquiringTime();
}
@ -93,10 +86,13 @@ public final class UpdateManager {
.map(player -> (NettyPlayerConnection) player.getPlayerConnection())
.forEach(NettyPlayerConnection::flush));
// Disable thread until next tick
LockSupport.parkNanos((long) ((MinecraftServer.TICK_MS * 1e6) - tickTime));
} catch (Exception e) {
MinecraftServer.getExceptionManager().handleException(e);
}
}, 0, MinecraftServer.TICK_MS, TimeUnit.MILLISECONDS);
}
}, MinecraftServer.THREAD_NAME_TICK_SCHEDULER).start();
}
/**