SteamWar/BauSystem
Archiviert
13
0

Optimize CommandTPSLimiter with accuracy

Dieser Commit ist enthalten in:
jojo 2021-01-26 10:23:51 +01:00
Ursprung 1ddc58d1fb
Commit 56402bb012

Datei anzeigen

@ -22,6 +22,7 @@ package de.steamwar.bausystem.commands;
import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission; import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.world.Welt; import de.steamwar.bausystem.world.Welt;
import de.steamwar.core.TPSWatcher;
import de.steamwar.core.VersionedRunnable; import de.steamwar.core.VersionedRunnable;
import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
@ -36,10 +37,14 @@ import org.bukkit.scheduler.BukkitTask;
public class CommandTPSLimiter implements CommandExecutor { public class CommandTPSLimiter implements CommandExecutor {
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 static final long neededDelta = 50;
private static long neededDeltaFix = 50;
private static World world = Bukkit.getWorlds().get(0);
private BukkitTask tpsLimiter = null; private BukkitTask tpsLimiter = null;
private boolean permissionCheck(Player player) { private boolean permissionCheck(Player player) {
@ -104,8 +109,13 @@ public class CommandTPSLimiter implements CommandExecutor {
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(); 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), 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);