SteamWar/BauSystem
Archiviert
13
0
Dieses Repository wurde am 2024-08-04 archiviert. Du kannst Dateien ansehen und es klonen, aber nicht pushen oder Issues/Pull-Requests öffnen.
BauSystem/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java

101 Zeilen
3.5 KiB
Java

2020-11-15 13:12:52 +01:00
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;
}
}