2017-12-31 03:58:20 +00:00
package me.totalfreedom.totalfreedommod.command ;
2020-07-14 19:00:22 +00:00
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.List ;
import java.util.Map ;
2020-08-15 22:41:23 +00:00
import me.totalfreedom.totalfreedommod.admin.Admin ;
2017-12-31 03:58:20 +00:00
import me.totalfreedom.totalfreedommod.rank.Rank ;
2020-07-14 19:00:22 +00:00
import me.totalfreedom.totalfreedommod.util.FUtil ;
import org.apache.commons.lang.math.NumberUtils ;
import org.bukkit.Bukkit ;
import org.bukkit.ChatColor ;
2017-12-31 03:58:20 +00:00
import org.bukkit.command.Command ;
import org.bukkit.command.CommandSender ;
import org.bukkit.entity.Player ;
2020-07-14 19:00:22 +00:00
import org.bukkit.entity.ThrownPotion ;
2017-12-31 03:58:20 +00:00
2020-08-15 22:41:23 +00:00
@CommandPermissions ( level = Rank . SUPER_ADMIN , source = SourceType . ONLY_IN_GAME )
@CommandParameters ( description = " Allows admins to see potions that are thrown. " , usage = " /<command> <enable | on | disable | off> | history [player] <page> " , aliases = " potspy " )
2017-12-31 03:58:20 +00:00
public class Command_potionspy extends FreedomCommand
{
2020-07-14 19:00:22 +00:00
private String titleText = " &8&m------------------&r &ePotionSpy &8&m------------------&r " ;
private String validPageText = " Please specify a valid page number between 1 and %s. " ;
private String noPlayerRecord = " That player has not thrown any potions yet. " ;
private String splashedText = " &r%s splashed a potion at &eX: %s Y: %s Z: %s&r \ nin the world '&e%s&r' about &e%s &rago%s. " ;
private String bottomText = " &8&m--------------------&r &e%s / %s &8&m--------------------&r " ;
2017-12-31 03:58:20 +00:00
@Override
public boolean run ( CommandSender sender , Player playerSender , Command cmd , String commandLabel , String [ ] args , boolean senderIsConsole )
{
2020-08-15 22:41:23 +00:00
Admin admin = plugin . al . getAdmin ( playerSender ) ;
2020-07-14 19:00:22 +00:00
2020-08-02 00:46:14 +00:00
if ( args . length < = 0 )
2020-07-14 19:00:22 +00:00
{
2020-08-15 22:41:23 +00:00
setPotionSpyState ( admin , ! admin . getPotionSpy ( ) ) ;
2020-07-14 19:00:22 +00:00
return true ;
}
else
{
switch ( args [ 0 ] . toLowerCase ( ) )
{
case " enable " :
case " on " :
2020-08-15 22:41:23 +00:00
setPotionSpyState ( admin , true ) ;
2020-07-14 19:00:22 +00:00
break ;
case " disable " :
case " off " :
2020-08-15 22:41:23 +00:00
setPotionSpyState ( admin , false ) ;
2020-07-14 19:00:22 +00:00
break ;
case " history " :
2020-08-02 00:46:14 +00:00
if ( args . length = = 3 )
2020-07-14 19:00:22 +00:00
{
Player player = Bukkit . getPlayer ( args [ 1 ] ) ;
2020-08-02 00:46:14 +00:00
if ( player = = null )
2020-07-14 19:00:22 +00:00
{
msg ( sender , " Please specify a valid player name. " ) ;
return true ;
}
List < Map . Entry < ThrownPotion , Long > > thrownPotions = new ArrayList < > ( ) ;
thrownPotions . addAll ( plugin . mo . getPlayerThrownPotions ( player ) ) ; // Make a copy of the list to avoid modifying the original.
List < String > potionThrowNotifications = new ArrayList < > ( ) ;
int lastPage = ( int ) Math . ceil ( thrownPotions . size ( ) / 5 . 0 ) ;
2020-08-02 00:46:14 +00:00
if ( thrownPotions . isEmpty ( ) )
2020-07-14 19:00:22 +00:00
{
msg ( sender , noPlayerRecord ) ;
return true ;
}
2020-08-02 00:46:14 +00:00
if ( ! NumberUtils . isNumber ( args [ 2 ] ) )
2020-07-14 19:00:22 +00:00
{
msg ( sender , String . format ( validPageText , lastPage ) ) ;
return true ;
}
Collections . reverse ( thrownPotions ) ;
int pageIndex = Integer . parseInt ( args [ 2 ] ) ;
2020-08-02 00:46:14 +00:00
for ( Map . Entry < ThrownPotion , Long > potionEntry : thrownPotions )
2020-07-14 19:00:22 +00:00
{
ThrownPotion potion = potionEntry . getKey ( ) ;
boolean trollPotions = plugin . mo . isTrollPotion ( potion ) ;
potionThrowNotifications . add ( ChatColor . translateAlternateColorCodes ( '&' , String . format ( splashedText , player . getName ( ) , potion . getLocation ( ) . getBlockX ( ) ,
potion . getLocation ( ) . getBlockY ( ) , potion . getLocation ( ) . getBlockZ ( ) , potion . getWorld ( ) . getName ( ) , getUnixTimeDifference ( potionEntry . getValue ( ) , System . currentTimeMillis ( ) ) , trollPotions ? " &c(most likely troll potion/potions) " : " " ) ) ) ;
}
List < String > page = FUtil . getPageFromList ( potionThrowNotifications , 5 , pageIndex - 1 ) ;
2020-08-02 00:46:14 +00:00
if ( ! page . isEmpty ( ) )
2020-07-14 19:00:22 +00:00
{
msg ( sender , ChatColor . translateAlternateColorCodes ( '&' , titleText ) ) ;
for ( String potionThrowNotification : page )
{
msg ( sender , potionThrowNotification ) ;
}
}
else
{
msg ( sender , String . format ( validPageText , lastPage ) ) ;
return true ;
}
msg ( sender , ChatColor . translateAlternateColorCodes ( '&' , String . format ( bottomText , pageIndex , lastPage ) ) ) ;
}
2020-08-02 00:46:14 +00:00
else if ( args . length = = 2 )
2020-07-14 19:00:22 +00:00
{
List < Map . Entry < ThrownPotion , Long > > thrownPotions = new ArrayList < > ( ) ;
thrownPotions . addAll ( plugin . mo . getAllThrownPotions ( ) ) ; // Make a copy of the list to avoid modifying the original.
List < String > potionThrowNotifications = new ArrayList < > ( ) ;
int lastPage = ( int ) Math . ceil ( thrownPotions . size ( ) / 5 . 0 ) ;
2020-08-02 00:46:14 +00:00
if ( thrownPotions . isEmpty ( ) )
2020-07-14 19:00:22 +00:00
{
if ( Bukkit . getPlayer ( args [ 1 ] ) ! = null )
{
msg ( sender , noPlayerRecord ) ;
}
else
{
msg ( sender , " No potions have been thrown yet. " ) ;
}
return true ;
}
2020-08-02 00:46:14 +00:00
if ( ! NumberUtils . isNumber ( args [ 1 ] ) )
2020-07-14 19:00:22 +00:00
{
msg ( sender , String . format ( validPageText , lastPage ) ) ;
return true ;
}
Collections . reverse ( thrownPotions ) ;
int pageIndex = Integer . parseInt ( args [ 1 ] ) ;
2020-08-02 00:46:14 +00:00
for ( Map . Entry < ThrownPotion , Long > potionEntry : thrownPotions )
2020-07-14 19:00:22 +00:00
{
ThrownPotion potion = potionEntry . getKey ( ) ;
Player player = ( Player ) potion . getShooter ( ) ;
boolean trollPotions = plugin . mo . isTrollPotion ( potion ) ;
if ( player ! = null )
{
potionThrowNotifications . add ( ChatColor . translateAlternateColorCodes ( '&' , String . format ( splashedText , player . getName ( ) , potion . getLocation ( ) . getBlockX ( ) ,
potion . getLocation ( ) . getBlockY ( ) , potion . getLocation ( ) . getBlockZ ( ) , potion . getWorld ( ) . getName ( ) , getUnixTimeDifference ( potionEntry . getValue ( ) , System . currentTimeMillis ( ) ) , trollPotions ? " &c(most likely troll potion/potions) " : " " ) ) ) ;
}
}
List < String > page = FUtil . getPageFromList ( potionThrowNotifications , 5 , pageIndex - 1 ) ;
2020-08-02 00:46:14 +00:00
if ( ! page . isEmpty ( ) )
2020-07-14 19:00:22 +00:00
{
msg ( sender , ChatColor . translateAlternateColorCodes ( '&' , titleText ) ) ;
for ( String potionThrowNotification : page )
{
msg ( sender , potionThrowNotification ) ;
}
}
else
{
msg ( sender , String . format ( validPageText , lastPage ) ) ;
return true ;
}
msg ( sender , ChatColor . translateAlternateColorCodes ( '&' , String . format ( bottomText , pageIndex , lastPage ) ) ) ;
}
else
{
return false ;
}
break ;
default :
return false ;
}
}
return true ;
}
2020-08-15 22:41:23 +00:00
private void setPotionSpyState ( Admin admin , boolean state )
2020-07-14 19:00:22 +00:00
{
2020-08-15 22:41:23 +00:00
admin . setPotionSpy ( state ) ;
plugin . al . save ( admin ) ;
plugin . al . updateTables ( ) ;
msg ( " PotionSpy is now " + ( admin . getPotionSpy ( ) ? " enabled. " : " disabled. " ) ) ;
2020-07-14 19:00:22 +00:00
}
/ * *
* Get the unix time difference in string format ( 1h , 30m , 15s ) .
* @param past The unix time at the start .
* @param now The current unix time .
* @return A string that displays the time difference between the two unix time values .
* /
private String getUnixTimeDifference ( long past , long now )
{
long unix = now - past ;
long seconds = Math . round ( unix / 1000 . 0 ) ;
2020-08-02 00:46:14 +00:00
if ( seconds < 60 )
2020-07-14 19:00:22 +00:00
{
return seconds + " s " ;
}
else
{
long minutes = Math . round ( seconds / 60 . 0 ) ;
2020-08-02 00:46:14 +00:00
if ( minutes < 60 )
2020-07-14 19:00:22 +00:00
{
return minutes + " m " ;
}
else
{
long hours = Math . round ( minutes / 60 . 0 ) ;
2020-08-02 00:46:14 +00:00
if ( hours < 24 )
2020-07-14 19:00:22 +00:00
{
return hours + " h " ;
}
else
{
return Math . round ( hours / 24 . 0 ) + " d " ;
}
}
}
2017-12-31 03:58:20 +00:00
}
}