From be82441ed929094c4d401b3e5743792c3505c53f Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 21 Mar 2021 14:19:58 +0100 Subject: [PATCH] Add TPSUtils Add TPSWarping --- .../spectatesystem/SpectateSystem.java | 2 + .../commands/TPSLimitCommand.java | 10 ++++- .../spectatesystem/util/TPSUtils.java | 38 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/de/steamwar/spectatesystem/util/TPSUtils.java diff --git a/src/de/steamwar/spectatesystem/SpectateSystem.java b/src/de/steamwar/spectatesystem/SpectateSystem.java index 5c5f3d3..354800f 100644 --- a/src/de/steamwar/spectatesystem/SpectateSystem.java +++ b/src/de/steamwar/spectatesystem/SpectateSystem.java @@ -27,6 +27,7 @@ import de.steamwar.spectatesystem.listener.PlayerListener; import de.steamwar.spectatesystem.listener.CancelListener; import de.steamwar.spectatesystem.listener.JoinListener; import de.steamwar.spectatesystem.listener.PlayerSeatListener; +import de.steamwar.spectatesystem.util.TPSUtils; import de.steamwar.spectatesystem.util.WorldLoader; import de.steamwar.sql.UserGroup; import org.bukkit.Bukkit; @@ -59,6 +60,7 @@ public class SpectateSystem extends JavaPlugin { Bukkit.getLogger().log(Level.SEVERE, "Could not open ConnectionAcceptor", e); } + TPSUtils.init(); Bukkit.getPluginCommand("replay").setExecutor(new ReplayCommand()); Bukkit.getPluginCommand("inspect").setExecutor(new InspectCommand()); Bukkit.getPluginCommand("tpslimit").setExecutor(new TPSLimitCommand()); diff --git a/src/de/steamwar/spectatesystem/commands/TPSLimitCommand.java b/src/de/steamwar/spectatesystem/commands/TPSLimitCommand.java index d776965..22eba86 100644 --- a/src/de/steamwar/spectatesystem/commands/TPSLimitCommand.java +++ b/src/de/steamwar/spectatesystem/commands/TPSLimitCommand.java @@ -1,6 +1,7 @@ package de.steamwar.spectatesystem.commands; import de.steamwar.spectatesystem.SpectateSystem; +import de.steamwar.spectatesystem.util.TPSUtils; import de.steamwar.sql.SteamwarUser; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; @@ -55,7 +56,7 @@ public class TPSLimitCommand implements CommandExecutor { try { double tpsLimitDouble = Double.parseDouble(tpsLimit.replace(',', '.')); - if (tpsLimitDouble < 0.5 || tpsLimitDouble > 20) { + if (tpsLimitDouble < 0.5 || tpsLimitDouble > 40) { sendInvalidArgumentMessage(player); return false; } @@ -74,7 +75,7 @@ public class TPSLimitCommand implements CommandExecutor { } private void sendInvalidArgumentMessage(Player player) { - player.sendMessage("§eSteam§8War» §cNur Zahlen zwischen 0,5 und 20, und 'default' erlaubt."); + player.sendMessage("§eSteam§8War» §cNur Zahlen zwischen 0,5 und 40, und 'default' erlaubt."); } private void tpsLimiter() { @@ -82,6 +83,7 @@ public class TPSLimitCommand implements CommandExecutor { loops = (int) Math.ceil(delay); sleepDelay = (long) (50 * delay) / loops; + TPSUtils.setTPS(currentTPSLimit); if (currentTPSLimit >= 20) { if (tpsLimiter == null) return; tpsLimiter.cancel(); @@ -111,4 +113,8 @@ public class TPSLimitCommand implements CommandExecutor { } } + public static double getCurrentTPSLimit() { + return (double) Math.round(currentTPSLimit * 10.0D) / 10.0D; + } + } diff --git a/src/de/steamwar/spectatesystem/util/TPSUtils.java b/src/de/steamwar/spectatesystem/util/TPSUtils.java new file mode 100644 index 0000000..8ba2ac1 --- /dev/null +++ b/src/de/steamwar/spectatesystem/util/TPSUtils.java @@ -0,0 +1,38 @@ +package de.steamwar.spectatesystem.util; + +import de.steamwar.core.TPSWatcher; +import de.steamwar.spectatesystem.SpectateSystem; +import de.steamwar.spectatesystem.commands.TPSLimitCommand; +import net.minecraft.server.v1_15_R1.SystemUtils; +import org.bukkit.Bukkit; + +public class TPSUtils { + + private TPSUtils() { + throw new IllegalStateException("Utility Class"); + } + + private static long nanoOffset = 0; + private static long nanoDOffset = 0; + + public static void init() { + Bukkit.getScheduler().runTaskTimer(SpectateSystem.get(), () -> nanoOffset += nanoDOffset, 1, 1); + SystemUtils.a = () -> nanoOffset; + } + + public static void setTPS(double tps) { + double d = 50 - (50 / (tps / 20.0)); + nanoDOffset = Math.max(0, Math.min((long) (d * 1000000), 25000000)); + } + + public static boolean isWarping() { + return nanoDOffset > 0; + } + + public static double getTps(TPSWatcher.TPSType tpsType) { + if (TPSUtils.isWarping()) + return TPSWatcher.getTPS(tpsType, Math.max(TPSLimitCommand.getCurrentTPSLimit(), 20)); + return TPSWatcher.getTPS(tpsType); + } + +} -- 2.39.5