2016-05-12 19:40:39 +00:00
package me.totalfreedom.totalfreedommod.command ;
2020-01-25 06:27:16 +00:00
import java.util.ArrayList ;
2018-07-31 07:01:29 +00:00
import java.util.Arrays ;
2020-01-25 06:27:16 +00:00
import java.util.Collections ;
import java.util.List ;
2020-12-25 19:46:43 +00:00
import me.totalfreedom.totalfreedommod.admin.Admin ;
2019-04-21 00:16:00 +00:00
import me.totalfreedom.totalfreedommod.config.ConfigEntry ;
2020-11-10 03:05:33 +00:00
import me.totalfreedom.totalfreedommod.discord.Discord ;
2020-06-30 07:25:38 +00:00
import me.totalfreedom.totalfreedommod.player.PlayerData ;
2016-05-12 19:40:39 +00:00
import me.totalfreedom.totalfreedommod.rank.Rank ;
import me.totalfreedom.totalfreedommod.util.FUtil ;
2019-07-28 06:04:16 +00:00
import org.apache.commons.lang.StringUtils ;
2016-05-12 19:40:39 +00:00
import org.bukkit.ChatColor ;
import org.bukkit.command.Command ;
import org.bukkit.command.CommandSender ;
import org.bukkit.entity.Player ;
2020-09-28 03:17:01 +00:00
@CommandPermissions ( level = Rank . ADMIN , source = SourceType . ONLY_IN_GAME )
2020-11-24 16:53:37 +00:00
@CommandParameters ( description = " Manage your admin entry. " , usage = " /<command> [-o <admin name>] <clearips | clearip <ip> | setscformat <format> | clearscformat> | syncroles> " )
2020-10-02 09:27:06 +00:00
public class Command_myadmin extends FreedomCommand
2016-05-12 19:40:39 +00:00
{
@Override
2020-07-01 01:51:06 +00:00
public boolean run ( CommandSender sender , Player playerSender , Command cmd , String commandLabel , String [ ] args , boolean senderIsConsole )
2016-05-12 19:40:39 +00:00
{
if ( args . length < 1 )
{
return false ;
}
Player init = null ;
2020-12-04 00:28:53 +00:00
Admin target = getAdmin ( playerSender ) ;
2016-05-12 19:40:39 +00:00
Player targetPlayer = playerSender ;
// -o switch
if ( args [ 0 ] . equals ( " -o " ) )
{
2020-09-28 02:51:15 +00:00
checkRank ( Rank . SENIOR_ADMIN ) ;
2016-05-12 19:40:39 +00:00
init = playerSender ;
targetPlayer = getPlayer ( args [ 1 ] ) ;
if ( targetPlayer = = null )
{
msg ( FreedomCommand . PLAYER_NOT_FOUND ) ;
return true ;
}
2016-08-26 09:42:13 +00:00
2020-12-04 00:28:53 +00:00
target = getAdmin ( targetPlayer ) ;
2016-05-12 19:40:39 +00:00
if ( target = = null )
{
2020-12-04 00:28:53 +00:00
msg ( " That player is not an admin " , ChatColor . RED ) ;
2016-05-12 19:40:39 +00:00
return true ;
}
// Shift 2
args = Arrays . copyOfRange ( args , 2 , args . length ) ;
if ( args . length < 1 )
{
return false ;
}
}
2020-07-01 01:51:06 +00:00
final String targetIp = FUtil . getIp ( targetPlayer ) ;
2016-08-26 09:42:13 +00:00
2016-05-12 19:40:39 +00:00
switch ( args [ 0 ] )
{
case " clearips " :
{
if ( args . length ! = 1 )
{
return false ; // Double check: the player might mean "clearip"
}
if ( init = = null )
{
2020-12-04 00:28:53 +00:00
FUtil . adminAction ( sender . getName ( ) , " Clearing my IPs " , true ) ;
2016-05-12 19:40:39 +00:00
}
else
{
2020-12-04 00:28:53 +00:00
FUtil . adminAction ( sender . getName ( ) , " Clearing " + target . getName ( ) + " 's IPs " , true ) ;
2016-05-12 19:40:39 +00:00
}
int counter = target . getIps ( ) . size ( ) - 1 ;
target . clearIPs ( ) ;
target . addIp ( targetIp ) ;
2020-12-04 00:28:53 +00:00
plugin . al . save ( target ) ;
plugin . al . updateTables ( ) ;
2020-06-30 07:25:38 +00:00
plugin . pl . syncIps ( target ) ;
2016-05-12 19:40:39 +00:00
msg ( counter + " IPs removed. " ) ;
msg ( targetPlayer , target . getIps ( ) . get ( 0 ) + " is now your only IP address " ) ;
return true ;
}
case " clearip " :
{
if ( args . length ! = 2 )
{
return false ; // Double check: the player might mean "clearips"
}
if ( ! target . getIps ( ) . contains ( args [ 1 ] ) )
{
if ( init = = null )
{
msg ( " That IP is not registered to you. " ) ;
}
else
{
msg ( " That IP does not belong to that player. " ) ;
}
return true ;
}
if ( targetIp . equals ( args [ 1 ] ) )
{
if ( init = = null )
{
msg ( " You cannot remove your current IP. " ) ;
}
else
{
2020-12-04 00:28:53 +00:00
msg ( " You cannot remove that admins current IP. " ) ;
2016-05-12 19:40:39 +00:00
}
return true ;
}
2020-12-04 00:28:53 +00:00
FUtil . adminAction ( sender . getName ( ) , " Removing an IP " + ( init = = null ? " " : " from " + targetPlayer . getName ( ) + " 's IPs " ) , true ) ;
2016-05-12 19:40:39 +00:00
target . removeIp ( args [ 1 ] ) ;
2020-12-04 00:28:53 +00:00
plugin . al . save ( target ) ;
plugin . al . updateTables ( ) ;
2016-05-12 19:40:39 +00:00
2020-06-30 07:25:38 +00:00
plugin . pl . syncIps ( target ) ;
2016-05-12 19:40:39 +00:00
msg ( " Removed IP " + args [ 1 ] ) ;
msg ( " Current IPs: " + StringUtils . join ( target . getIps ( ) , " , " ) ) ;
return true ;
}
2020-12-26 23:54:15 +00:00
case " setacformat " :
2020-08-15 22:42:46 +00:00
case " setscformat " :
2018-06-01 22:40:25 +00:00
{
String format = StringUtils . join ( args , " " , 1 , args . length ) ;
target . setAcFormat ( format ) ;
2020-12-04 00:28:53 +00:00
plugin . al . save ( target ) ;
plugin . al . updateTables ( ) ;
msg ( " Set admin chat format to \" " + format + " \" . " , ChatColor . GRAY ) ;
String example = format . replace ( " %name% " , " ExampleAdmin " ) . replace ( " %rank% " , Rank . ADMIN . getAbbr ( ) ) . replace ( " %rankcolor% " , Rank . ADMIN . getColor ( ) . toString ( ) ) . replace ( " %msg% " , " The quick brown fox jumps over the lazy dog. " ) ;
2018-06-01 22:40:25 +00:00
msg ( ChatColor . GRAY + " Example: " + FUtil . colorize ( example ) ) ;
return true ;
}
2020-08-15 16:35:48 +00:00
2020-12-26 23:54:15 +00:00
case " clearacformat " :
2020-08-15 22:42:46 +00:00
case " clearscformat " :
2018-06-01 22:40:25 +00:00
{
target . setAcFormat ( null ) ;
2020-12-04 00:28:53 +00:00
plugin . al . save ( target ) ;
plugin . al . updateTables ( ) ;
msg ( " Cleared admin chat format. " , ChatColor . GRAY ) ;
2018-06-01 22:40:25 +00:00
return true ;
}
2018-02-22 01:22:13 +00:00
2019-04-21 00:16:00 +00:00
case " syncroles " :
{
if ( plugin . dc . enabled )
{
if ( ! ConfigEntry . DISCORD_ROLE_SYNC . getBoolean ( ) )
{
msg ( " Role syncing is not enabled. " , ChatColor . RED ) ;
return true ;
}
2020-06-30 07:25:38 +00:00
PlayerData playerData = plugin . pl . getData ( target . getName ( ) ) ;
if ( playerData . getDiscordID ( ) = = null )
2019-04-21 00:16:00 +00:00
{
msg ( " Please run /linkdiscord first! " , ChatColor . RED ) ;
return true ;
}
2020-11-10 03:05:33 +00:00
boolean synced = Discord . syncRoles ( target , playerData . getDiscordID ( ) ) ;
2019-04-21 00:16:00 +00:00
if ( synced )
{
msg ( " Successfully synced your roles. " , ChatColor . GREEN ) ;
}
else
{
msg ( " Failed to sync your roles, please check the console. " , ChatColor . RED ) ;
}
}
return true ;
}
2016-05-12 19:40:39 +00:00
default :
{
return false ;
}
}
}
2020-01-25 06:27:16 +00:00
@Override
public List < String > getTabCompleteOptions ( CommandSender sender , Command command , String alias , String [ ] args )
{
2020-12-04 00:28:53 +00:00
if ( ! plugin . al . isAdmin ( sender ) )
2020-01-25 06:27:16 +00:00
{
return Collections . emptyList ( ) ;
}
2020-12-26 23:54:15 +00:00
List < String > singleArguments = Arrays . asList ( " clearips " , " setscformat " , " setacformat " ) ;
List < String > doubleArguments = Arrays . asList ( " clearip " , " clearscformat " , " clearacformat " , " syncroles " ) ;
2020-01-25 06:27:16 +00:00
if ( args . length = = 1 )
{
List < String > options = new ArrayList < > ( ) ;
options . add ( " -o " ) ;
options . addAll ( singleArguments ) ;
options . addAll ( doubleArguments ) ;
return options ;
}
else if ( args . length = = 2 )
{
if ( args [ 0 ] . equals ( " -o " ) )
{
return FUtil . getPlayerList ( ) ;
}
else
{
if ( doubleArguments . contains ( args [ 0 ] ) )
{
if ( args [ 0 ] . equals ( " clearip " ) )
{
2020-12-04 00:28:53 +00:00
List < String > ips = plugin . al . getAdmin ( sender ) . getIps ( ) ;
2020-08-15 16:35:48 +00:00
ips . remove ( FUtil . getIp ( ( Player ) sender ) ) ;
2020-01-25 06:27:16 +00:00
return ips ;
}
}
}
}
else if ( args . length = = 3 )
{
if ( args [ 0 ] . equals ( " -o " ) )
{
List < String > options = new ArrayList < > ( ) ;
options . addAll ( singleArguments ) ;
options . addAll ( doubleArguments ) ;
return options ;
}
}
else if ( args . length = = 4 )
{
if ( args [ 0 ] . equals ( " -o " ) & & args [ 2 ] . equals ( " clearip " ) )
{
2020-12-04 00:28:53 +00:00
Admin admin = plugin . al . getEntryByName ( args [ 1 ] ) ;
if ( admin ! = null )
2020-01-25 06:27:16 +00:00
{
2020-12-04 00:28:53 +00:00
return admin . getIps ( ) ;
2020-01-25 06:27:16 +00:00
}
}
}
return FUtil . getPlayerList ( ) ;
}
2020-12-04 00:28:53 +00:00
}