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
2020-12-26 22:41:00 +01:00

140 Zeilen
4.9 KiB
Java

/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.commands;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.world.Welt;
import de.steamwar.core.VersionedRunnable;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
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.scheduler.BukkitTask;
public class CommandTPSLimiter implements CommandExecutor {
private static double currentTPSLimit = 20;
private static World world = Bukkit.getWorlds().get(0);
private long lastTime = System.nanoTime();
private long currentTime = System.nanoTime();
private BukkitTask tpsLimiter = null;
private boolean permissionCheck(Player player) {
if (Welt.noPermission(player, Permission.world)) {
player.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht den TPS-Limiter nutzen");
return false;
}
return true;
}
@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;
if (!permissionCheck(player)) 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(BauSystem.PREFIX + "§cNur Zahlen zwischen 0,5 und 20, und 'default' erlaubt.");
}
private void tpsLimiter() {
if (currentTPSLimit == 20) {
if (tpsLimiter == null) return;
tpsLimiter.cancel();
tpsLimiter = null;
} else {
if (tpsLimiter != null) return;
tpsLimiter = Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> {
VersionedRunnable.call(new VersionedRunnable(() -> TPSLimit_12.createTickCache(world), 8),
new VersionedRunnable(() -> TPSLimit_15.createTickCache(world), 14));
for (int i = 0; i < (20 / currentTPSLimit); i++) {
sleepUntilNextTick();
VersionedRunnable.call(new VersionedRunnable(TPSLimit_12::sendTickPackets, 8),
new VersionedRunnable(TPSLimit_15::sendTickPackets, 14));
}
}, 0, 1);
}
}
private void sleepUntilNextTick() {
lastTime = currentTime;
currentTime = System.nanoTime();
long timeDelta = (currentTime - lastTime) / 1000000;
long neededDelta = 50;
if (neededDelta - timeDelta < 0) {
return;
}
try {
Thread.sleep(neededDelta - timeDelta);
currentTime = System.nanoTime();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
public static double getCurrentTPSLimit() {
return currentTPSLimit;
}
}