From 45d5ee8e901d32538761c7f0b256ac07a53442dc Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 21 Mar 2021 14:09:54 +0100 Subject: [PATCH] Add TPSLimitCommand --- .../spectatesystem/SpectateSystem.java | 2 + .../commands/TPSLimitCommand.java | 114 ++++++++++++++++++ src/plugin.yml | 1 + 3 files changed, 117 insertions(+) create mode 100644 src/de/steamwar/spectatesystem/commands/TPSLimitCommand.java diff --git a/src/de/steamwar/spectatesystem/SpectateSystem.java b/src/de/steamwar/spectatesystem/SpectateSystem.java index fbd45b2..5c5f3d3 100644 --- a/src/de/steamwar/spectatesystem/SpectateSystem.java +++ b/src/de/steamwar/spectatesystem/SpectateSystem.java @@ -21,6 +21,7 @@ package de.steamwar.spectatesystem; import de.steamwar.spectatesystem.commands.InspectCommand; import de.steamwar.spectatesystem.commands.ReplayCommand; +import de.steamwar.spectatesystem.commands.TPSLimitCommand; import de.steamwar.spectatesystem.elements.RScoreboard; import de.steamwar.spectatesystem.listener.PlayerListener; import de.steamwar.spectatesystem.listener.CancelListener; @@ -60,6 +61,7 @@ public class SpectateSystem extends JavaPlugin { Bukkit.getPluginCommand("replay").setExecutor(new ReplayCommand()); Bukkit.getPluginCommand("inspect").setExecutor(new InspectCommand()); + Bukkit.getPluginCommand("tpslimit").setExecutor(new TPSLimitCommand()); WorldLoader.configureForceLoad(); } diff --git a/src/de/steamwar/spectatesystem/commands/TPSLimitCommand.java b/src/de/steamwar/spectatesystem/commands/TPSLimitCommand.java new file mode 100644 index 0000000..d776965 --- /dev/null +++ b/src/de/steamwar/spectatesystem/commands/TPSLimitCommand.java @@ -0,0 +1,114 @@ +package de.steamwar.spectatesystem.commands; + +import de.steamwar.spectatesystem.SpectateSystem; +import de.steamwar.sql.SteamwarUser; +import net.md_5.bungee.api.ChatMessageType; +import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitTask; + +public class TPSLimitCommand implements CommandExecutor { + + private static double currentTPSLimit = 20; + + 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; + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) { + return false; + } + Player player = (Player) sender; + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + if(!SpectateSystem.allowedGroups.contains(user.getUserGroup())){ + player.sendMessage("§eSteam§8War» §cUnbekannter Befehl."); + return false; + } + if (!InspectCommand.inspecting) { + player.sendMessage("§eSteam§8War» §cNur für den Inspect mode."); + return false; + } + if (args.length == 0) { + sender.sendMessage("§eSteam§8War» §aJetziges TPS limit: " + currentTPSLimit); + sender.sendMessage("§eSteam§8War» §aÄndere das TPS limit mit: §8/§etpslimit §8[§7TPS§8|§edefault§8]"); + return false; + } + + String tpsLimit = args[0]; + if (tpsLimit.equals("default")) { + currentTPSLimit = 20; + sendNewTPSLimitMessage(); + tpsLimiter(); + return false; + } + + try { + double tpsLimitDouble = Double.parseDouble(tpsLimit.replace(',', '.')); + if (tpsLimitDouble < 0.5 || tpsLimitDouble > 20) { + sendInvalidArgumentMessage(player); + return false; + } + currentTPSLimit = tpsLimitDouble; + sendNewTPSLimitMessage(); + tpsLimiter(); + } catch (NumberFormatException e) { + sendInvalidArgumentMessage(player); + } + + return false; + } + + private void sendNewTPSLimitMessage() { + Bukkit.getOnlinePlayers().forEach(p -> p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§eTPS limit auf " + currentTPSLimit + " gesetzt."))); + } + + private void sendInvalidArgumentMessage(Player player) { + player.sendMessage("§eSteam§8War» §cNur Zahlen zwischen 0,5 und 20, und 'default' erlaubt."); + } + + 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(); + tpsLimiter = null; + } else { + if (tpsLimiter != null) return; + tpsLimiter = Bukkit.getScheduler().runTaskTimer(SpectateSystem.get(), () -> { + for (int i = 0; i < loops; i++) { + sleepUntilNextTick(sleepDelay); + } + }, 0, 1); + } + } + + private void sleepUntilNextTick(long neededDelta) { + lastTime = currentTime; + currentTime = System.nanoTime(); + + long timeDelta = (currentTime - lastTime) / 1000000; + if (neededDelta - timeDelta < 0) return; + + try { + Thread.sleep(neededDelta - timeDelta); + currentTime = System.nanoTime(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + +} diff --git a/src/plugin.yml b/src/plugin.yml index 10295c1..1c8b2df 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -12,3 +12,4 @@ depend: commands: replay: inspect: + tpslimit: