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 ;
import net.md_5.bungee.api.ChatMessageType ;
import net.md_5.bungee.api.chat.TextComponent ;
2020-11-15 13:42:19 +01:00
import net.minecraft.server.v1_15_R1.PacketPlayOutEntityVelocity ;
import net.minecraft.server.v1_15_R1.PlayerConnection ;
2020-11-15 13:12:52 +01:00
import org.bukkit.Bukkit ;
import org.bukkit.Location ;
import org.bukkit.Particle ;
import org.bukkit.World ;
2020-11-15 13:42:19 +01:00
import org.bukkit.block.data.type.TNT ;
2020-11-15 13:12:52 +01:00
import org.bukkit.command.Command ;
import org.bukkit.command.CommandExecutor ;
import org.bukkit.command.CommandSender ;
2020-11-15 13:42:19 +01:00
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer ;
import org.bukkit.entity.Entity ;
2020-11-15 13:12:52 +01:00
import org.bukkit.entity.Player ;
import org.bukkit.entity.TNTPrimed ;
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 {
private static int currentTPSLimit = 20 ;
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 13:42:19 +01:00
public CommandTPSLimiter ( ) {
2020-11-15 13:12:52 +01:00
Bukkit . getScheduler ( ) . runTaskTimer ( BauSystem . getPlugin ( ) , ( ) - > {
if ( currentTPSLimit = = 20 ) {
return ;
}
2020-11-15 13:42:19 +01:00
/ * 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 ) ) ; * /
2020-11-15 13:12:52 +01:00
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 ;
2020-11-15 13:42:19 +01:00
sendNewTPSLimitMessage ( ) ;
tpsLimiter ( ) ;
2020-11-15 13:12:52 +01:00
return false ;
}
try {
int tpsLimitInt = Integer . parseInt ( tpsLimit ) ;
if ( tpsLimitInt < 1 | | tpsLimitInt > 20 ) {
sendInvalidArgumentMessage ( player ) ;
return false ;
}
currentTPSLimit = tpsLimitInt ;
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-11-15 13:42:19 +01:00
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 ) ;
}
2020-11-15 13:12:52 +01:00
}
public static int getCurrentTPSLimit ( ) {
return currentTPSLimit ;
}
2020-11-15 13:42:19 +01:00
private void setMotion ( Entity entity ) {
}
2020-11-15 13:12:52 +01:00
}