Add TPSLimitCommand
Dieser Commit ist enthalten in:
Ursprung
c9e1bb6a9e
Commit
45d5ee8e90
@ -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();
|
||||
}
|
||||
|
||||
|
114
src/de/steamwar/spectatesystem/commands/TPSLimitCommand.java
Normale Datei
114
src/de/steamwar/spectatesystem/commands/TPSLimitCommand.java
Normale Datei
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -12,3 +12,4 @@ depend:
|
||||
commands:
|
||||
replay:
|
||||
inspect:
|
||||
tpslimit:
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren