SteamWar/BauSystem
Archiviert
13
0

Optimize CommandTPSLimiter with accuracy #169

Manuell gemergt
YoyoNow hat 5 Commits von TPSLimiter nach master 2021-01-26 11:16:52 +01:00 zusammengeführt

Datei anzeigen

@ -35,11 +35,16 @@ import org.bukkit.scheduler.BukkitTask;
public class CommandTPSLimiter implements CommandExecutor { public class CommandTPSLimiter implements CommandExecutor {
private static final World WORLD = Bukkit.getWorlds().get(0);
private static double currentTPSLimit = 20; private static double currentTPSLimit = 20;
private static World world = Bukkit.getWorlds().get(0);
private long lastTime = System.nanoTime(); private long lastTime = System.nanoTime();
private long currentTime = System.nanoTime(); private long currentTime = System.nanoTime();
private double delay = 0;
private int loops = 0;
private long sleepDelay = 0;
private BukkitTask tpsLimiter = null; private BukkitTask tpsLimiter = null;
private boolean permissionCheck(Player player) { private boolean permissionCheck(Player player) {
@ -95,6 +100,10 @@ public class CommandTPSLimiter implements CommandExecutor {
} }
private void tpsLimiter() { private void tpsLimiter() {
delay = 20 / currentTPSLimit;
loops = (int)Math.ceil(delay);
sleepDelay = (long) (50 * delay) / loops;
if (currentTPSLimit == 20) { if (currentTPSLimit == 20) {
if (tpsLimiter == null) return; if (tpsLimiter == null) return;
tpsLimiter.cancel(); tpsLimiter.cancel();
@ -102,10 +111,11 @@ public class CommandTPSLimiter implements CommandExecutor {
} else { } else {
if (tpsLimiter != null) return; if (tpsLimiter != null) return;
tpsLimiter = Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> { tpsLimiter = Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> {
VersionedRunnable.call(new VersionedRunnable(() -> TPSLimit_12.createTickCache(world), 8), VersionedRunnable.call(new VersionedRunnable(() -> TPSLimit_12.createTickCache(WORLD), 8),
new VersionedRunnable(() -> TPSLimit_15.createTickCache(world), 14)); new VersionedRunnable(() -> TPSLimit_15.createTickCache(WORLD), 14));
for (int i = 0; i < (20 / currentTPSLimit); i++) {
sleepUntilNextTick(); for (int i = 0; i < loops; i++) {
sleepUntilNextTick(sleepDelay);
VersionedRunnable.call(new VersionedRunnable(TPSLimit_12::sendTickPackets, 8), VersionedRunnable.call(new VersionedRunnable(TPSLimit_12::sendTickPackets, 8),
new VersionedRunnable(TPSLimit_15::sendTickPackets, 14)); new VersionedRunnable(TPSLimit_15::sendTickPackets, 14));
} }
@ -113,16 +123,12 @@ public class CommandTPSLimiter implements CommandExecutor {
} }
} }
private void sleepUntilNextTick() { private void sleepUntilNextTick(long neededDelta) {
lastTime = currentTime; lastTime = currentTime;
currentTime = System.nanoTime(); currentTime = System.nanoTime();
long timeDelta = (currentTime - lastTime) / 1000000; long timeDelta = (currentTime - lastTime) / 1000000;
long neededDelta = 50; if (neededDelta - timeDelta < 0) return;
if (neededDelta - timeDelta < 0) {
return;
}
try { try {
Thread.sleep(neededDelta - timeDelta); Thread.sleep(neededDelta - timeDelta);