diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index 18d1f2a..b3ce821 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -98,6 +98,8 @@ public class BauSystem extends JavaPlugin implements Listener { getCommand("trace").setExecutor(new CommandTrace()); getCommand("trace").setTabCompleter(new CommandTraceTabCompleter()); + getCommand("tpslimit").setExecutor(new CommandTPSLimiter()); + getCommand("tpslimit").setTabCompleter(new CommandTPSLimiterTabComplete()); getCommand("nightvision").setExecutor(new CommandNV()); getCommand("reset").setExecutor(new CommandReset()); getCommand("speed").setExecutor(new CommandSpeed()); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java new file mode 100644 index 0000000..c5daea6 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java @@ -0,0 +1,100 @@ +package de.steamwar.bausystem.commands; + +import de.steamwar.bausystem.BauSystem; +import net.md_5.bungee.api.ChatMessageType; +import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Particle; +import org.bukkit.World; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.entity.TNTPrimed; + +public class CommandTPSLimiter implements CommandExecutor { + + private static int currentTPSLimit = 20; + private static World world = Bukkit.getWorlds().get(0); + private long lastTime = System.currentTimeMillis(); + private long currentTime = System.currentTimeMillis(); + + public CommandTPSLimiter() { + Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> { + lastTime = currentTime; + currentTime = System.currentTimeMillis(); + + long timeDelta = currentTime - lastTime; + long neededDelta = 1000 / currentTPSLimit; + + if (neededDelta - timeDelta < 1) { + return; + } + + try { + Thread.sleep(neededDelta - timeDelta); + currentTime = System.currentTimeMillis(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }, 0, 1); + Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> { + if (currentTPSLimit == 20) { + return; + } + + world.getEntities().stream().filter(entity -> entity instanceof TNTPrimed).forEach(entity -> { + Location location = entity.getLocation(); + world.spawnParticle(Particle.BARRIER, location.getX(), location.getY() + 0.49, location.getZ(), 1); + }); + }, 0, 1); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) { + return false; + } else if (args.length == 0) { + sender.sendMessage(BauSystem.PREFIX + "Jetziges TPS limit: " + currentTPSLimit); + sender.sendMessage(BauSystem.PREFIX + "Ändere das TPS limit mit: §8/§etpslimit §8[§7TPS§8|§edefault§8]"); + return false; + } + Player player = (Player) sender; + + String tpsLimit = args[0]; + if (tpsLimit.equals("default")) { + currentTPSLimit = 20; + sendNewTPSLimitMessage(player); + return false; + } + + try { + int tpsLimitInt = Integer.parseInt(tpsLimit); + if (tpsLimitInt < 1 || tpsLimitInt > 20) { + sendInvalidArgumentMessage(player); + return false; + } + currentTPSLimit = tpsLimitInt; + sendNewTPSLimitMessage(player); + } catch (NumberFormatException e) { + sendInvalidArgumentMessage(player); + } + + return false; + } + + private void sendNewTPSLimitMessage(Player player) { + player.sendMessage(BauSystem.PREFIX + "TPS limit auf " + currentTPSLimit + " gesetzt."); + Bukkit.getOnlinePlayers().forEach(p -> p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§eTPS limit auf " + currentTPSLimit + " gesetzt."))); + } + + private void sendInvalidArgumentMessage(Player player) { + player.sendMessage(BauSystem.PREFIX + "Nur Zahlen zwischen 1 und 20, und 'default' erlaubt."); + } + + public static int getCurrentTPSLimit() { + return currentTPSLimit; + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiterTabComplete.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiterTabComplete.java new file mode 100644 index 0000000..a250019 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiterTabComplete.java @@ -0,0 +1,29 @@ +package de.steamwar.bausystem.commands; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class CommandTPSLimiterTabComplete implements TabCompleter { + + private List arguments = Arrays.asList("default", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"); + + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + if (args.length != 1) { + return new ArrayList<>(); + } + List validArguments = new ArrayList<>(arguments.size()); + for (String s : arguments) { + if (s.startsWith(args[0])) { + validArguments.add(s); + } + } + return validArguments; + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTraceTabCompleter.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTraceTabCompleter.java index d21551d..b97fa39 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTraceTabCompleter.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTraceTabCompleter.java @@ -20,7 +20,6 @@ package de.steamwar.bausystem.commands; import de.steamwar.bausystem.tracer.recorder.RecordManager; -import de.steamwar.core.Core; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java b/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java index bbe4330..e1fb722 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java @@ -21,6 +21,7 @@ package de.steamwar.bausystem.world; import de.steamwar.bausystem.commands.CommandFreeze; import de.steamwar.bausystem.commands.CommandTNT; +import de.steamwar.bausystem.commands.CommandTPSLimiter; import de.steamwar.bausystem.tracer.TraceManager; import de.steamwar.bausystem.tracer.recorder.RecordManager; import de.steamwar.bausystem.tracer.recorder.TNTRecorder; @@ -72,7 +73,11 @@ public class BauScoreboard implements Listener { } strings.add("§4"); - strings.add("§eTPS§8: §7" + TPSWatcher.getTPS()); + if (CommandTPSLimiter.getCurrentTPSLimit() == 20) { + strings.add("§eTPS§8: §7" + TPSWatcher.getTPS()); + } else { + strings.add("§eTPS§8: §7" + TPSWatcher.getTPS() + " §eLimit§8: §7" + CommandTPSLimiter.getCurrentTPSLimit()); + } int i = strings.size(); HashMap result = new HashMap<>(); diff --git a/BauSystem_Main/src/plugin.yml b/BauSystem_Main/src/plugin.yml index 4a21d05..bfd4616 100644 --- a/BauSystem_Main/src/plugin.yml +++ b/BauSystem_Main/src/plugin.yml @@ -11,6 +11,7 @@ commands: tnt: fire: trace: + tpslimit: testblock: aliases: tb reset: