From 9e8372a318eacdb41a0184dddc9572522f291da4 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 1 May 2021 17:03:07 +0200 Subject: [PATCH] Optimize TPSWarpUtils --- .../features/tpslimit/TPSLimitCommand.java | 4 ++-- .../bausystem/features/tpslimit/TPSWarpUtils.java | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitCommand.java index fcf9ed74..e1a2feac 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitCommand.java @@ -67,8 +67,8 @@ public class TPSLimitCommand extends SWCommand implements Enable { public void defaultCommand(Player p) { if (!permissionCheck(p)) return; TPSLimitUtils.currentTPSLimit = 20; - sendNewTPSLimitMessage(); TPSLimitUtils.tpsLimiter(); + sendNewTPSLimitMessage(); } @Register @@ -79,8 +79,8 @@ public class TPSLimitCommand extends SWCommand implements Enable { return; } TPSLimitUtils.currentTPSLimit = tpsLimitDouble; - sendNewTPSLimitMessage(); TPSLimitUtils.tpsLimiter(); + sendNewTPSLimitMessage(); } @ClassMapper(value = double.class, local = true) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSWarpUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSWarpUtils.java index 7b9f64c7..997ae238 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSWarpUtils.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSWarpUtils.java @@ -20,10 +20,12 @@ package de.steamwar.bausystem.features.tpslimit; import de.steamwar.bausystem.BauSystem; +import de.steamwar.core.Core; import de.steamwar.core.TPSWatcher; import de.steamwar.core.VersionedRunnable; import lombok.experimental.UtilityClass; import org.bukkit.Bukkit; +import org.bukkit.scheduler.BukkitTask; @UtilityClass public class TPSWarpUtils { @@ -31,11 +33,11 @@ public class TPSWarpUtils { private static boolean warp = true; private static long nanoOffset = 0; private static long nanoDOffset = 0; + private static BukkitTask bukkitTask = null; static void init() { VersionedRunnable.call(new VersionedRunnable(() -> warp = false, 8), new VersionedRunnable(() -> { - Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> nanoOffset += nanoDOffset, 1, 1); TPSUtils_15.init(() -> nanoOffset); }, 15)); } @@ -43,6 +45,16 @@ public class TPSWarpUtils { public static void setTPS(double tps) { double d = 50 - (50 / (tps / 20.0)); nanoDOffset = Math.max(0, Math.min((long) (d * 1000000), 37500000)); + if (Core.getVersion() != 15) { + return; + } + if (nanoDOffset == 0) { + if (bukkitTask == null) return; + bukkitTask.cancel(); + bukkitTask = null; + } else { + bukkitTask = Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> nanoOffset += nanoDOffset, 1, 1); + } } public static boolean isWarpAllowed() { @@ -53,7 +65,6 @@ public class TPSWarpUtils { return nanoDOffset > 0; } - @SuppressWarnings("unused") public static double getTps(TPSWatcher.TPSType tpsType) { if (TPSWarpUtils.isWarping()) return TPSWatcher.getTPS(tpsType, Math.max(TPSLimitUtils.getCurrentTPSLimit(), 20));