2015-10-19 17:43:46 +00:00
package me.totalfreedom.totalfreedommod.commands ;
2011-10-19 00:37:00 +00:00
2015-11-15 23:32:04 +00:00
import me.totalfreedom.totalfreedommod.rank.PlayerRank ;
2015-10-19 17:43:46 +00:00
import java.nio.charset.StandardCharsets ;
2014-07-26 03:11:44 +00:00
import java.util.Iterator ;
2015-10-19 17:43:46 +00:00
import java.util.UUID ;
2015-11-15 23:32:04 +00:00
import me.totalfreedom.totalfreedommod.banning.Ban ;
import me.totalfreedom.totalfreedommod.freeze.FreezeData ;
2015-10-19 17:43:46 +00:00
import me.totalfreedom.totalfreedommod.util.FUtil ;
2011-10-19 00:37:00 +00:00
import org.bukkit.ChatColor ;
import org.bukkit.command.Command ;
import org.bukkit.command.CommandSender ;
import org.bukkit.entity.Player ;
2015-10-19 17:43:46 +00:00
@CommandPermissions ( level = PlayerRank . SUPER_ADMIN , source = SourceType . BOTH , blockHostConsole = true )
2013-04-10 02:05:24 +00:00
@CommandParameters (
2014-07-26 03:11:44 +00:00
description = " Use admin commands on someone by hash. Use mode 'list' to get a player's hash. Other modes are kick, nameban, ipban, ban, op, deop, ci, fr, smite. " ,
usage = " /<command> [list | [<kick | nameban | ipban | ban | op | deop | ci | fr | smite> <targethash>] ] " )
2015-10-19 17:43:46 +00:00
public class Command_gadmin extends FreedomCommand
2011-10-19 00:37:00 +00:00
{
2015-11-22 18:26:47 +00:00
2014-07-26 03:11:44 +00:00
private enum GadminMode
{
2015-11-22 18:26:47 +00:00
2014-07-26 03:11:44 +00:00
LIST ( " list " ) ,
KICK ( " kick " ) ,
NAMEBAN ( " nameban " ) ,
IPBAN ( " ipban " ) ,
BAN ( " ban " ) ,
OP ( " op " ) ,
DEOP ( " deop " ) ,
CI ( " ci " ) ,
FR ( " fr " ) ,
SMITE ( " smite " ) ;
private final String modeName ;
private GadminMode ( String command )
{
this . modeName = command ;
}
public String getModeName ( )
{
return modeName ;
}
public static GadminMode findMode ( String needle )
{
for ( final GadminMode mode : GadminMode . values ( ) )
{
if ( needle . equalsIgnoreCase ( mode . getModeName ( ) ) )
{
return mode ;
}
}
return null ;
}
}
2015-10-19 17:43:46 +00:00
public String getPlayerHash ( Player player )
{
return UUID . nameUUIDFromBytes ( player . getName ( ) . toLowerCase ( ) . getBytes ( StandardCharsets . UTF_8 ) ) . toString ( ) . substring ( 0 , 4 ) ;
}
2011-10-19 00:37:00 +00:00
@Override
2015-11-22 18:26:47 +00:00
public boolean run ( CommandSender sender , Player playerSender , Command cmd , String commandLabel , String [ ] args , boolean senderIsConsole )
2011-10-19 00:37:00 +00:00
{
if ( args . length = = 0 )
{
return false ;
}
2014-07-26 03:11:44 +00:00
final GadminMode mode = GadminMode . findMode ( args [ 0 ] . toLowerCase ( ) ) ;
if ( mode = = null )
2011-10-19 00:37:00 +00:00
{
2014-07-26 03:11:44 +00:00
playerMsg ( " Invalid mode: " + args [ 0 ] , ChatColor . RED ) ;
return true ;
2012-11-24 01:22:52 +00:00
}
2014-12-21 09:23:50 +00:00
final Iterator < ? extends Player > it = server . getOnlinePlayers ( ) . iterator ( ) ;
2014-07-26 03:11:44 +00:00
if ( mode = = GadminMode . LIST )
2012-11-24 01:22:52 +00:00
{
2014-07-26 03:11:44 +00:00
playerMsg ( " [ Real Name ] : [ Display Name ] - Hash: " ) ;
while ( it . hasNext ( ) )
2011-10-19 00:37:00 +00:00
{
2014-07-26 03:11:44 +00:00
final Player player = it . next ( ) ;
2012-11-24 01:22:52 +00:00
sender . sendMessage ( ChatColor . GRAY + String . format ( " [ %s ] : [ %s ] - %s " ,
2013-08-14 14:01:42 +00:00
player . getName ( ) ,
ChatColor . stripColor ( player . getDisplayName ( ) ) ,
2015-10-19 17:43:46 +00:00
getPlayerHash ( player ) ) ) ;
2011-10-19 00:37:00 +00:00
}
2014-07-26 03:11:44 +00:00
return true ;
}
2014-06-28 10:00:16 +00:00
2014-07-26 03:11:44 +00:00
if ( args . length < 2 )
{
return false ;
}
Player target = null ;
while ( it . hasNext ( ) & & target = = null )
{
final Player player = it . next ( ) ;
2015-10-19 17:43:46 +00:00
final String hash = getPlayerHash ( player ) ;
2014-07-26 03:11:44 +00:00
if ( hash . equalsIgnoreCase ( args [ 1 ] ) )
2014-06-28 10:00:16 +00:00
{
2014-07-26 03:11:44 +00:00
target = player ;
2014-06-28 10:00:16 +00:00
}
2014-07-26 03:11:44 +00:00
}
2014-06-28 10:00:16 +00:00
2014-07-26 03:11:44 +00:00
if ( target = = null )
{
playerMsg ( " Invalid player hash: " + args [ 1 ] , ChatColor . RED ) ;
return true ;
}
2014-06-28 10:00:16 +00:00
2014-07-26 03:11:44 +00:00
switch ( mode )
{
case KICK :
2011-10-19 00:37:00 +00:00
{
2015-10-19 17:43:46 +00:00
FUtil . adminAction ( sender . getName ( ) , String . format ( " Kicking: %s. " , target . getName ( ) ) , false ) ;
2014-07-26 03:11:44 +00:00
target . kickPlayer ( " Kicked by Administrator " ) ;
2014-06-29 08:53:39 +00:00
2014-07-26 03:11:44 +00:00
break ;
}
case NAMEBAN :
{
2015-10-19 17:43:46 +00:00
FUtil . adminAction ( sender . getName ( ) , String . format ( " Banning Name: %s. " , target . getName ( ) ) , true ) ;
2015-11-15 23:32:04 +00:00
plugin . bm . addBan ( Ban . forPlayerName ( target , sender , null , null ) ) ;
2014-07-26 03:11:44 +00:00
target . kickPlayer ( " Username banned by Administrator. " ) ;
break ;
}
case IPBAN :
{
String ip = target . getAddress ( ) . getAddress ( ) . getHostAddress ( ) ;
String [ ] ip_parts = ip . split ( " \\ . " ) ;
if ( ip_parts . length = = 4 )
2012-11-24 01:22:52 +00:00
{
2014-07-26 03:11:44 +00:00
ip = String . format ( " %s.%s.*.* " , ip_parts [ 0 ] , ip_parts [ 1 ] ) ;
2012-11-24 01:22:52 +00:00
}
2015-10-19 17:43:46 +00:00
FUtil . adminAction ( sender . getName ( ) , String . format ( " Banning IP: %s. " , ip ) , true ) ;
2015-11-15 23:32:04 +00:00
plugin . bm . addBan ( Ban . forPlayerIp ( ip , sender , null , null ) ) ;
2014-07-26 03:11:44 +00:00
target . kickPlayer ( " IP address banned by Administrator. " ) ;
break ;
}
case BAN :
{
String ip = target . getAddress ( ) . getAddress ( ) . getHostAddress ( ) ;
String [ ] ip_parts = ip . split ( " \\ . " ) ;
if ( ip_parts . length = = 4 )
2012-11-24 01:22:52 +00:00
{
2014-07-26 03:11:44 +00:00
ip = String . format ( " %s.%s.*.* " , ip_parts [ 0 ] , ip_parts [ 1 ] ) ;
2012-11-24 01:22:52 +00:00
}
2015-10-19 17:43:46 +00:00
FUtil . adminAction ( sender . getName ( ) , String . format ( " Banning Name: %s, IP: %s. " , target . getName ( ) , ip ) , true ) ;
2011-10-19 00:37:00 +00:00
2015-11-15 23:32:04 +00:00
plugin . bm . addBan ( Ban . forPlayer ( target , sender ) ) ;
2011-10-19 00:37:00 +00:00
2014-07-26 03:11:44 +00:00
target . kickPlayer ( " IP and username banned by Administrator. " ) ;
break ;
2011-10-19 00:37:00 +00:00
}
2014-07-26 03:11:44 +00:00
case OP :
{
2015-10-19 17:43:46 +00:00
FUtil . adminAction ( sender . getName ( ) , String . format ( " Opping %s. " , target . getName ( ) ) , false ) ;
2014-07-26 03:11:44 +00:00
target . setOp ( false ) ;
2015-10-19 17:43:46 +00:00
target . sendMessage ( FreedomCommand . YOU_ARE_OP ) ;
2012-11-24 01:22:52 +00:00
2014-07-26 03:11:44 +00:00
break ;
}
case DEOP :
{
2015-10-19 17:43:46 +00:00
FUtil . adminAction ( sender . getName ( ) , String . format ( " Deopping %s. " , target . getName ( ) ) , false ) ;
2014-07-26 03:11:44 +00:00
target . setOp ( false ) ;
2015-10-19 17:43:46 +00:00
target . sendMessage ( FreedomCommand . YOU_ARE_NOT_OP ) ;
2014-07-26 03:11:44 +00:00
break ;
}
case CI :
{
target . getInventory ( ) . clear ( ) ;
break ;
}
case FR :
{
2015-11-15 23:32:04 +00:00
FreezeData fd = plugin . pl . getPlayer ( target ) . getFreezeData ( ) ;
fd . setFrozen ( ! fd . isFrozen ( ) ) ;
2014-07-26 03:11:44 +00:00
2015-11-15 23:32:04 +00:00
playerMsg ( target . getName ( ) + " has been " + ( fd . isFrozen ( ) ? " frozen " : " unfrozen " ) + " . " ) ;
target . sendMessage ( ChatColor . AQUA + " You have been " + ( fd . isFrozen ( ) ? " frozen " : " unfrozen " ) + " . " ) ;
2014-07-26 03:11:44 +00:00
break ;
}
case SMITE :
{
Command_smite . smite ( target ) ;
break ;
}
2011-10-19 00:37:00 +00:00
}
return true ;
}
}