2020-11-15 13:42:19 +01:00
/ *
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/>.
* /
2020-11-15 13:12:52 +01:00
package de.steamwar.bausystem.commands ;
import de.steamwar.bausystem.BauSystem ;
2020-11-15 14:10:11 +01:00
import de.steamwar.bausystem.Permission ;
import de.steamwar.bausystem.world.Welt ;
2020-11-16 14:24:55 +01:00
import de.steamwar.core.Core ;
2020-11-15 13:12:52 +01:00
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 ;
2020-11-15 13:42:19 +01:00
import org.bukkit.scheduler.BukkitTask ;
2020-11-15 13:12:52 +01:00
public class CommandTPSLimiter implements CommandExecutor {
2020-12-22 21:01:29 +01:00
private static double currentTPSLimit = 20 ;
2020-11-15 13:12:52 +01:00
private static World world = Bukkit . getWorlds ( ) . get ( 0 ) ;
2020-11-15 13:42:19 +01:00
private long lastTime = System . nanoTime ( ) ;
private long currentTime = System . nanoTime ( ) ;
2020-11-15 13:12:52 +01:00
2020-11-15 13:42:19 +01:00
private BukkitTask tpsLimiter = null ;
2020-11-15 13:12:52 +01:00
2020-11-15 14:10:11 +01:00
private boolean permissionCheck ( Player player ) {
2020-11-15 15:55:34 +01:00
if ( Welt . noPermission ( player , Permission . world ) ) {
2020-11-15 14:10:11 +01:00
player . sendMessage ( BauSystem . PREFIX + " §cDu darfst hier nicht den TPS-Limiter nutzen " ) ;
return false ;
}
return true ;
}
2020-11-15 13:12:52 +01:00
@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 ;
2020-11-16 14:04:08 +01:00
if ( ! permissionCheck ( player ) ) return false ;
2020-11-15 13:12:52 +01:00
String tpsLimit = args [ 0 ] ;
if ( tpsLimit . equals ( " default " ) ) {
currentTPSLimit = 20 ;
2020-11-15 13:42:19 +01:00
sendNewTPSLimitMessage ( ) ;
tpsLimiter ( ) ;
2020-11-15 13:12:52 +01:00
return false ;
}
try {
2020-12-22 21:01:29 +01:00
double tpsLimitDouble = Double . parseDouble ( tpsLimit . replace ( ',' , '.' ) ) ;
if ( tpsLimitDouble < 0 . 5 | | tpsLimitDouble > 20 ) {
2020-11-15 13:12:52 +01:00
sendInvalidArgumentMessage ( player ) ;
return false ;
}
2020-12-22 21:01:29 +01:00
currentTPSLimit = tpsLimitDouble ;
2020-11-15 13:42:19 +01:00
sendNewTPSLimitMessage ( ) ;
tpsLimiter ( ) ;
2020-11-15 13:12:52 +01:00
} catch ( NumberFormatException e ) {
sendInvalidArgumentMessage ( player ) ;
}
return false ;
}
2020-11-15 13:42:19 +01:00
private void sendNewTPSLimitMessage ( ) {
2020-11-15 13:12:52 +01:00
Bukkit . getOnlinePlayers ( ) . forEach ( p - > p . spigot ( ) . sendMessage ( ChatMessageType . ACTION_BAR , TextComponent . fromLegacyText ( " §eTPS limit auf " + currentTPSLimit + " gesetzt. " ) ) ) ;
}
private void sendInvalidArgumentMessage ( Player player ) {
2020-12-24 11:27:27 +01:00
player . sendMessage ( BauSystem . PREFIX + " §cNur Zahlen zwischen 0.5 und 20, und 'default' erlaubt. " ) ;
2020-11-15 13:42:19 +01:00
}
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 ( ) , ( ) - > {
2020-12-22 21:01:29 +01:00
versionDependantCall ( ( ) - > TPSLimit_12 . sendTntMetaData ( world ) , ( ) - > TPSLimit_15 . sendTntMetaData ( world ) ) ;
2020-11-15 13:42:19 +01:00
2020-12-22 21:01:29 +01:00
versionDependantCall ( ( ) - > TPSLimit_12 . createVelocityPacketCache ( world ) , ( ) - > TPSLimit_15 . createVelocityPacketCache ( world ) ) ;
2020-11-16 13:39:29 +01:00
for ( int i = 0 ; i < ( 20 / currentTPSLimit ) ; i + + ) {
sleepUntilNextTick ( ) ;
2020-12-22 21:01:29 +01:00
versionDependantCall ( ( ) - > TPSLimit_12 . sendTeleports ( world ) , ( ) - > TPSLimit_15 . sendTeleports ( world ) ) ;
versionDependantCall ( TPSLimit_12 : : sendVelocityPackets , TPSLimit_15 : : sendVelocityPackets ) ;
2020-11-15 13:42:19 +01:00
}
} , 0 , 1 ) ;
}
2020-11-15 13:12:52 +01:00
}
2020-11-16 13:39:29 +01:00
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 ( ) ;
}
2020-11-15 13:12:52 +01:00
}
2020-12-22 21:01:29 +01:00
private void versionDependantCall ( Runnable v12 , Runnable v15 ) {
2020-11-16 14:24:55 +01:00
switch ( Core . getVersion ( ) ) {
2020-12-22 21:01:29 +01:00
case 12 :
v12 . run ( ) ;
2020-11-16 14:24:55 +01:00
break ;
default :
2020-12-22 21:01:29 +01:00
v15 . run ( ) ;
2020-11-16 14:24:55 +01:00
break ;
}
2020-11-16 13:39:29 +01:00
}
2020-12-22 21:01:29 +01:00
public static double getCurrentTPSLimit ( ) {
2020-11-16 13:39:29 +01:00
return currentTPSLimit ;
2020-11-15 13:42:19 +01:00
}
2020-11-15 13:12:52 +01:00
}