2021-01-03 07:21:15 +00:00
package dev.plex.command.impl ;
2020-11-10 02:47:03 +00:00
import com.google.common.collect.ImmutableList ;
2022-04-04 08:36:50 +00:00
import dev.plex.Plex ;
2022-01-04 03:04:39 +00:00
import dev.plex.cache.DataUtils ;
import dev.plex.command.PlexCommand ;
2021-01-03 07:21:15 +00:00
import dev.plex.command.annotation.CommandParameters ;
import dev.plex.command.annotation.CommandPermissions ;
import dev.plex.command.exception.PlayerNotFoundException ;
import dev.plex.command.source.RequiredCommandSource ;
import dev.plex.player.PlexPlayer ;
2022-01-04 03:04:39 +00:00
import dev.plex.punishment.Punishment ;
import dev.plex.punishment.PunishmentType ;
2024-01-20 17:32:24 +00:00
import dev.plex.util.BungeeUtil ;
import dev.plex.util.PlexLog ;
import dev.plex.util.PlexUtils ;
import dev.plex.util.TimeUtils ;
2022-01-27 09:00:50 +00:00
import net.kyori.adventure.text.Component ;
2022-06-08 20:09:42 +00:00
import org.apache.commons.lang3.StringUtils ;
2020-11-10 02:47:03 +00:00
import org.bukkit.Bukkit ;
2022-01-27 09:00:50 +00:00
import org.bukkit.command.CommandSender ;
import org.bukkit.entity.Player ;
import org.jetbrains.annotations.NotNull ;
2022-02-04 04:49:05 +00:00
import org.jetbrains.annotations.Nullable ;
2020-11-10 02:47:03 +00:00
2023-03-08 20:26:10 +00:00
import java.time.ZoneId ;
import java.time.ZonedDateTime ;
2024-01-15 23:39:57 +00:00
import java.util.Collections ;
2023-03-08 20:26:10 +00:00
import java.util.List ;
2024-01-15 23:39:57 +00:00
@CommandParameters ( name = " ban " , usage = " /<command> <player> [-nrb] [reason] [-nrb] " , aliases = " offlineban,gtfo " , description = " Bans a player, offline or online " )
2023-08-25 11:07:56 +00:00
@CommandPermissions ( permission = " plex.ban " , source = RequiredCommandSource . ANY )
2020-11-10 02:47:03 +00:00
public class BanCMD extends PlexCommand
{
@Override
2022-02-04 04:49:05 +00:00
protected Component execute ( @NotNull CommandSender sender , @Nullable Player playerSender , String [ ] args )
2020-11-10 02:47:03 +00:00
{
if ( args . length = = 0 )
{
2022-02-14 05:55:50 +00:00
return usage ( ) ;
2020-11-10 02:47:03 +00:00
}
2023-08-25 05:36:14 +00:00
final PlexPlayer plexPlayer = DataUtils . getPlayer ( args [ 0 ] ) ;
2020-11-10 02:47:03 +00:00
2023-08-25 05:36:14 +00:00
if ( plexPlayer = = null )
2022-02-04 08:18:07 +00:00
{
throw new PlayerNotFoundException ( ) ;
}
2023-08-25 05:36:14 +00:00
Player player = Bukkit . getPlayer ( plexPlayer . getUuid ( ) ) ;
2020-11-10 02:47:03 +00:00
2023-08-25 05:36:14 +00:00
plugin . getPunishmentManager ( ) . isAsyncBanned ( plexPlayer . getUuid ( ) ) . whenComplete ( ( aBoolean , throwable ) - >
2022-02-25 08:59:48 +00:00
{
2022-04-04 08:36:50 +00:00
if ( aBoolean )
{
send ( sender , messageComponent ( " playerBanned " ) ) ;
return ;
}
String reason ;
2023-08-25 05:36:14 +00:00
Punishment punishment = new Punishment ( plexPlayer . getUuid ( ) , getUUID ( sender ) ) ;
2022-04-04 08:36:50 +00:00
punishment . setType ( PunishmentType . BAN ) ;
2024-01-15 23:39:57 +00:00
boolean rollBack = true ;
2022-04-04 08:36:50 +00:00
if ( args . length > 1 )
{
reason = StringUtils . join ( args , " " , 1 , args . length ) ;
2024-01-15 23:39:57 +00:00
String newReason = StringUtils . normalizeSpace ( reason . replace ( " -nrb " , " " ) ) ;
punishment . setReason ( newReason . trim ( ) . isEmpty ( ) ? " No reason provided. " : newReason ) ;
rollBack = ! reason . startsWith ( " -nrb " ) & & ! reason . endsWith ( " -nrb " ) ;
2022-04-13 02:22:17 +00:00
}
else
2022-04-04 08:36:50 +00:00
{
punishment . setReason ( " No reason provided. " ) ;
}
punishment . setPunishedUsername ( plexPlayer . getName ( ) ) ;
2022-04-19 21:49:45 +00:00
ZonedDateTime date = ZonedDateTime . now ( ZoneId . of ( TimeUtils . TIMEZONE ) ) ;
2022-04-04 08:36:50 +00:00
punishment . setEndDate ( date . plusDays ( 1 ) ) ;
punishment . setCustomTime ( false ) ;
2023-08-25 11:07:56 +00:00
punishment . setActive ( true ) ;
2024-01-17 06:23:31 +00:00
punishment . setIp ( player ! = null ? player . getAddress ( ) . getAddress ( ) . getHostAddress ( ) . trim ( ) : plexPlayer . getIps ( ) . get ( plexPlayer . getIps ( ) . size ( ) - 1 ) ) ;
2022-04-04 08:36:50 +00:00
plugin . getPunishmentManager ( ) . punish ( plexPlayer , punishment ) ;
PlexUtils . broadcast ( messageComponent ( " banningPlayer " , sender . getName ( ) , plexPlayer . getName ( ) ) ) ;
Bukkit . getScheduler ( ) . runTask ( Plex . get ( ) , ( ) - >
{
if ( player ! = null )
{
2022-05-10 05:00:48 +00:00
BungeeUtil . kickPlayer ( player , Punishment . generateBanMessage ( punishment ) ) ;
2022-04-04 08:36:50 +00:00
}
} ) ;
PlexLog . debug ( " (From /ban command) PunishedPlayer UUID: " + plexPlayer . getUuid ( ) ) ;
2024-01-15 23:39:57 +00:00
if ( rollBack )
{
/ * if ( plugin . getPrismHook ( ) . hasPrism ( ) ) {
PrismParameters parameters = plugin . getPrismHook ( ) . prismApi ( ) . createParameters ( ) ;
parameters . addActionType ( " block-place " ) ;
parameters . addActionType ( " block-break " ) ;
parameters . addActionType ( " block-burn " ) ;
parameters . addActionType ( " entity-spawn " ) ;
parameters . addActionType ( " entity-kill " ) ;
parameters . addActionType ( " entity-explode " ) ;
parameters . addPlayerName ( plexPlayer . getName ( ) ) ;
parameters . setBeforeTime ( Instant . now ( ) . toEpochMilli ( ) ) ;
parameters . setProcessType ( PrismProcessType . ROLLBACK ) ;
final Future < Result > result = plugin . getPrismHook ( ) . prismApi ( ) . performLookup ( parameters , sender ) ;
Bukkit . getAsyncScheduler ( ) . runNow ( plugin , scheduledTask - > {
try
{
final Result done = result . get ( ) ;
} catch ( InterruptedException | ExecutionException e )
{
throw new RuntimeException ( e ) ;
}
} ) ;
}
else * /
if ( plugin . getCoreProtectHook ( ) . hasCoreProtect ( ) )
{
PlexLog . debug ( " Testing coreprotect " ) ;
Bukkit . getAsyncScheduler ( ) . runNow ( plugin , scheduledTask - >
{
plugin . getCoreProtectHook ( ) . coreProtectAPI ( ) . performRollback ( 86400 , Collections . singletonList ( plexPlayer . getName ( ) ) , null , null , null , null , 0 , null ) ;
} ) ;
}
}
2022-04-04 08:36:50 +00:00
} ) ;
2022-02-25 08:59:48 +00:00
2022-01-27 09:00:50 +00:00
return null ;
2020-11-10 02:47:03 +00:00
}
@Override
2024-01-20 17:32:24 +00:00
public @NotNull List < String > smartTabComplete ( @NotNull CommandSender sender , @NotNull String alias , @NotNull String [ ] args ) throws IllegalArgumentException
2022-01-27 09:00:50 +00:00
{
2024-01-20 17:32:24 +00:00
return args . length = = 1 & & silentCheckPermission ( sender , this . getPermission ( ) ) ? PlexUtils . getPlayerNameList ( ) : ImmutableList . of ( ) ;
2020-11-10 02:47:03 +00:00
}
}