diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java index 051d4f7..7a749c7 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java @@ -35,11 +35,16 @@ import org.bukkit.scheduler.BukkitTask; public class CommandTPSLimiter implements CommandExecutor { + private static final World WORLD = Bukkit.getWorlds().get(0); private static double currentTPSLimit = 20; - private static World world = Bukkit.getWorlds().get(0); + private long lastTime = System.nanoTime(); private long currentTime = System.nanoTime(); + private double delay = 0; + private int loops = 0; + private long sleepDelay = 0; + private BukkitTask tpsLimiter = null; private boolean permissionCheck(Player player) { @@ -95,6 +100,10 @@ public class CommandTPSLimiter implements CommandExecutor { } private void tpsLimiter() { + delay = 20 / currentTPSLimit; + loops = (int)Math.ceil(delay); + sleepDelay = (long) (50 * delay) / loops; + if (currentTPSLimit == 20) { if (tpsLimiter == null) return; tpsLimiter.cancel(); @@ -102,10 +111,11 @@ public class CommandTPSLimiter implements CommandExecutor { } else { if (tpsLimiter != null) return; 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(); + VersionedRunnable.call(new VersionedRunnable(() -> TPSLimit_12.createTickCache(WORLD), 8), + new VersionedRunnable(() -> TPSLimit_15.createTickCache(WORLD), 14)); + + 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);