Add /tpslimit Command #113
@ -1,49 +1,65 @@
|
|||||||
|
/*
|
||||||
|
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;
|
package de.steamwar.bausystem.commands;
|
||||||
|
|
||||||
import de.steamwar.bausystem.BauSystem;
|
import de.steamwar.bausystem.BauSystem;
|
||||||
import net.md_5.bungee.api.ChatMessageType;
|
import net.md_5.bungee.api.ChatMessageType;
|
||||||
import net.md_5.bungee.api.chat.TextComponent;
|
import net.md_5.bungee.api.chat.TextComponent;
|
||||||
|
import net.minecraft.server.v1_15_R1.PacketPlayOutEntityVelocity;
|
||||||
|
import net.minecraft.server.v1_15_R1.PlayerConnection;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Particle;
|
import org.bukkit.Particle;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.block.data.type.TNT;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
|
||||||
|
import org.bukkit.entity.Entity;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.entity.TNTPrimed;
|
import org.bukkit.entity.TNTPrimed;
|
||||||
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
|
||||||
public class CommandTPSLimiter implements CommandExecutor {
|
public class CommandTPSLimiter implements CommandExecutor {
|
||||||
|
|
||||||
private static int currentTPSLimit = 20;
|
private static int currentTPSLimit = 20;
|
||||||
private static World world = Bukkit.getWorlds().get(0);
|
private static World world = Bukkit.getWorlds().get(0);
|
||||||
private long lastTime = System.currentTimeMillis();
|
private long lastTime = System.nanoTime();
|
||||||
private long currentTime = System.currentTimeMillis();
|
private long currentTime = System.nanoTime();
|
||||||
|
|
||||||
|
private BukkitTask tpsLimiter = null;
|
||||||
|
|
||||||
public CommandTPSLimiter() {
|
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(), () -> {
|
Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> {
|
||||||
if (currentTPSLimit == 20) {
|
if (currentTPSLimit == 20) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*PacketPlayOutEntityVelocity packet = new PacketPlayOutEntityVelocity(entity);
|
||||||
|
for(Player player : Bukkit.getOnlinePlayers()){
|
||||||
|
PlayerConnection connection = ((CraftPlayer)player).getHandle().playerConnection;
|
||||||
|
connection.sendPacket(packet);
|
||||||
|
}
|
||||||
|
world.getEntities().stream().filter(entity -> entity instanceof TNT).forEach(entity -> entity.setVelocity(0, 0, 0));*/
|
||||||
|
|
||||||
world.getEntities().stream().filter(entity -> entity instanceof TNTPrimed).forEach(entity -> {
|
world.getEntities().stream().filter(entity -> entity instanceof TNTPrimed).forEach(entity -> {
|
||||||
Location location = entity.getLocation();
|
Location location = entity.getLocation();
|
||||||
world.spawnParticle(Particle.BARRIER, location.getX(), location.getY() + 0.49, location.getZ(), 1);
|
world.spawnParticle(Particle.BARRIER, location.getX(), location.getY() + 0.49, location.getZ(), 1);
|
||||||
@ -65,7 +81,8 @@ public class CommandTPSLimiter implements CommandExecutor {
|
|||||||
String tpsLimit = args[0];
|
String tpsLimit = args[0];
|
||||||
if (tpsLimit.equals("default")) {
|
if (tpsLimit.equals("default")) {
|
||||||
currentTPSLimit = 20;
|
currentTPSLimit = 20;
|
||||||
sendNewTPSLimitMessage(player);
|
sendNewTPSLimitMessage();
|
||||||
|
tpsLimiter();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +93,8 @@ public class CommandTPSLimiter implements CommandExecutor {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
currentTPSLimit = tpsLimitInt;
|
currentTPSLimit = tpsLimitInt;
|
||||||
sendNewTPSLimitMessage(player);
|
sendNewTPSLimitMessage();
|
||||||
|
tpsLimiter();
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
sendInvalidArgumentMessage(player);
|
sendInvalidArgumentMessage(player);
|
||||||
}
|
}
|
||||||
@ -84,17 +102,48 @@ public class CommandTPSLimiter implements CommandExecutor {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendNewTPSLimitMessage(Player player) {
|
private void sendNewTPSLimitMessage() {
|
||||||
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.")));
|
Bukkit.getOnlinePlayers().forEach(p -> p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§eTPS limit auf " + currentTPSLimit + " gesetzt.")));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendInvalidArgumentMessage(Player player) {
|
private void sendInvalidArgumentMessage(Player player) {
|
||||||
player.sendMessage(BauSystem.PREFIX + "Nur Zahlen zwischen 1 und 20, und 'default' erlaubt.");
|
player.sendMessage(BauSystem.PREFIX + "§cNur Zahlen zwischen 1 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(), () -> {
|
||||||
|
lastTime = currentTime;
|
||||||
|
currentTime = System.nanoTime();
|
||||||
|
|
||||||
|
long timeDelta = (currentTime - lastTime) / 1000000;
|
||||||
|
long neededDelta = 1000 / currentTPSLimit;
|
||||||
|
|
||||||
|
if (neededDelta - timeDelta < 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(neededDelta - timeDelta);
|
||||||
|
currentTime = System.nanoTime();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}, 0, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getCurrentTPSLimit() {
|
public static int getCurrentTPSLimit() {
|
||||||
return currentTPSLimit;
|
return currentTPSLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setMotion(Entity entity) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,22 @@
|
|||||||
|
/*
|
||||||
|
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;
|
package de.steamwar.bausystem.commands;
|
||||||
|
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
|
@ -76,7 +76,7 @@ public class BauScoreboard implements Listener {
|
|||||||
if (CommandTPSLimiter.getCurrentTPSLimit() == 20) {
|
if (CommandTPSLimiter.getCurrentTPSLimit() == 20) {
|
||||||
strings.add("§eTPS§8: §7" + TPSWatcher.getTPS());
|
strings.add("§eTPS§8: §7" + TPSWatcher.getTPS());
|
||||||
} else {
|
} else {
|
||||||
strings.add("§eTPS§8: §7" + TPSWatcher.getTPS() + " §eLimit§8: §7" + CommandTPSLimiter.getCurrentTPSLimit());
|
strings.add("§eTPS§8: §7" + TPSWatcher.getTPS() + "§8/§7" + CommandTPSLimiter.getCurrentTPSLimit());
|
||||||
}
|
}
|
||||||
|
|
||||||
int i = strings.size();
|
int i = strings.size();
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren