diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java index 051d4f7..7d2e71f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java @@ -22,6 +22,7 @@ package de.steamwar.bausystem.commands; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; import de.steamwar.bausystem.world.Welt; +import de.steamwar.core.TPSWatcher; import de.steamwar.core.VersionedRunnable; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; @@ -36,10 +37,14 @@ import org.bukkit.scheduler.BukkitTask; public class CommandTPSLimiter implements CommandExecutor { private static double currentTPSLimit = 20; - private static World world = Bukkit.getWorlds().get(0); private long lastTime = System.nanoTime(); private long currentTime = System.nanoTime(); + private static final long neededDelta = 50; + private static long neededDeltaFix = 50; + + private static World world = Bukkit.getWorlds().get(0); + private BukkitTask tpsLimiter = null; private boolean permissionCheck(Player player) { @@ -104,8 +109,13 @@ public class CommandTPSLimiter implements CommandExecutor { tpsLimiter = Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> { VersionedRunnable.call(new VersionedRunnable(() -> TPSLimit_12.createTickCache(world), 8), new VersionedRunnable(() -> TPSLimit_15.createTickCache(world), 14)); - for (int i = 0; i < (20 / currentTPSLimit); i++) { - sleepUntilNextTick(); + + double delay = 50 * (20 / currentTPSLimit); + int loops = (int)Math.ceil(delay / 50.0); + long sleepDelay = (long) delay / loops; + + for (int i = 0; i < loops; i++) { + sleepUntilNextTick(sleepDelay); VersionedRunnable.call(new VersionedRunnable(TPSLimit_12::sendTickPackets, 8), 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; currentTime = System.nanoTime(); long timeDelta = (currentTime - lastTime) / 1000000; - long neededDelta = 50; - - if (neededDelta - timeDelta < 0) { - return; - } + if (neededDelta - timeDelta < 0) return; try { Thread.sleep(neededDelta - timeDelta);