2017-12-31 03:58:20 +00:00
package me.totalfreedom.totalfreedommod ;
2020-07-14 19:00:22 +00:00
import java.util.AbstractMap ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
import org.bukkit.Bukkit ;
2018-01-07 19:33:58 +00:00
import org.bukkit.ChatColor ;
2017-12-31 03:58:20 +00:00
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
import org.bukkit.event.EventHandler ;
import org.bukkit.event.EventPriority ;
2018-07-28 07:11:48 +00:00
import org.bukkit.event.entity.LingeringPotionSplashEvent ;
2017-12-31 03:58:20 +00:00
import org.bukkit.event.entity.PotionSplashEvent ;
2020-07-14 19:00:22 +00:00
import org.bukkit.potion.PotionEffect ;
import org.bukkit.potion.PotionEffectType ;
2017-12-31 03:58:20 +00:00
public class Monitors extends FreedomService
{
2020-08-03 22:07:50 +00:00
private final List < Map . Entry < ThrownPotion , Long > > allThrownPotions = new ArrayList < > ( ) ;
private final Map < Player , List < ThrownPotion > > recentlyThrownPotions = new HashMap < > ( ) ;
2020-07-14 19:00:22 +00:00
private final List < PotionEffectType > badPotionEffects = new ArrayList < > ( Arrays . asList ( PotionEffectType . BLINDNESS ,
PotionEffectType . LEVITATION , PotionEffectType . CONFUSION , PotionEffectType . SLOW , PotionEffectType . SLOW_DIGGING , PotionEffectType . HUNGER ) ) ; // A list of all effects that count as "troll".
2017-12-31 03:58:20 +00:00
@Override
2020-07-01 01:51:06 +00:00
public void onStart ( )
2017-12-31 03:58:20 +00:00
{
2020-07-14 19:00:22 +00:00
Bukkit . getScheduler ( ) . scheduleSyncRepeatingTask ( plugin , ( ) - >
{
for ( Player player : recentlyThrownPotions . keySet ( ) )
{
2020-08-15 13:33:29 +00:00
List < ThrownPotion > playerThrownPotions = recentlyThrownPotions . get ( player ) ;
ThrownPotion latestThrownPotion = playerThrownPotions . get ( playerThrownPotions . size ( ) - 1 ) ; // Get most recently thrown potion for the position.
int potionsThrown = playerThrownPotions . size ( ) ;
int trollPotions = 0 ;
2020-07-14 19:00:22 +00:00
2020-08-15 13:33:29 +00:00
for ( ThrownPotion potion : playerThrownPotions )
{
if ( isTrollPotion ( potion ) )
2020-07-14 19:00:22 +00:00
{
2020-08-15 13:33:29 +00:00
trollPotions + + ;
2020-07-14 19:00:22 +00:00
}
}
2020-08-15 13:33:29 +00:00
2020-12-04 00:28:53 +00:00
plugin . al . potionSpyMessage ( ChatColor . translateAlternateColorCodes ( '&' , String . format ( " &8[&ePotionSpy&8] &r%s splashed %s %s at X: %s Y: %s Z: %s in the world '%s'%s. " ,
2020-08-15 13:33:29 +00:00
player . getName ( ) , potionsThrown , potionsThrown = = 1 ? " potion " : " potions " , latestThrownPotion . getLocation ( ) . getBlockX ( ) , latestThrownPotion . getLocation ( ) . getBlockY ( ) , latestThrownPotion . getLocation ( ) . getBlockZ ( ) ,
2020-12-04 00:28:53 +00:00
latestThrownPotion . getWorld ( ) . getName ( ) , trollPotions > 0 ? String . format ( " &c(most likely troll %s) " , trollPotions = = 1 ? " potion " : " potions " ) : " " ) ) ) ;
2020-07-14 19:00:22 +00:00
}
recentlyThrownPotions . clear ( ) ;
} , 0L , 40L ) ;
2017-12-31 03:58:20 +00:00
}
@Override
2020-07-01 01:51:06 +00:00
public void onStop ( )
2017-12-31 03:58:20 +00:00
{
}
2018-01-07 19:33:58 +00:00
@EventHandler ( priority = EventPriority . MONITOR )
2017-12-31 03:58:20 +00:00
public void onLingeringPotionSplash ( LingeringPotionSplashEvent event )
{
2020-07-14 19:00:22 +00:00
if ( event . getEntity ( ) . getShooter ( ) instanceof Player )
2017-12-31 03:58:20 +00:00
{
2020-07-14 19:00:22 +00:00
ThrownPotion potion = event . getEntity ( ) ;
2020-08-02 00:46:14 +00:00
if ( potion . getShooter ( ) instanceof Player )
2020-07-14 19:00:22 +00:00
{
Player player = ( Player ) potion . getShooter ( ) ;
2017-12-31 03:58:20 +00:00
2020-07-14 19:00:22 +00:00
recentlyThrownPotions . putIfAbsent ( player , new ArrayList < > ( ) ) ;
recentlyThrownPotions . get ( player ) . add ( potion ) ;
allThrownPotions . add ( new AbstractMap . SimpleEntry < > ( potion , System . currentTimeMillis ( ) ) ) ;
2017-12-31 03:58:20 +00:00
2020-08-02 00:46:14 +00:00
if ( recentlyThrownPotions . get ( player ) . size ( ) > 128 )
2020-07-14 19:00:22 +00:00
{
recentlyThrownPotions . get ( player ) . remove ( 0 ) ;
}
2020-08-02 00:46:14 +00:00
if ( allThrownPotions . size ( ) > 1024 )
2020-07-14 19:00:22 +00:00
{
allThrownPotions . remove ( 0 ) ; // Remove the first element in the set.
}
2017-12-31 03:58:20 +00:00
}
}
}
2018-01-07 19:33:58 +00:00
@EventHandler ( priority = EventPriority . MONITOR )
2017-12-31 03:58:20 +00:00
public void onPotionSplash ( PotionSplashEvent event )
{
2020-07-14 19:00:22 +00:00
if ( event . getEntity ( ) . getShooter ( ) instanceof Player )
2017-12-31 03:58:20 +00:00
{
2020-07-14 19:00:22 +00:00
ThrownPotion potion = event . getEntity ( ) ;
2020-08-02 00:46:14 +00:00
if ( potion . getShooter ( ) instanceof Player )
2020-07-14 19:00:22 +00:00
{
Player player = ( Player ) potion . getShooter ( ) ;
recentlyThrownPotions . putIfAbsent ( player , new ArrayList < > ( ) ) ;
recentlyThrownPotions . get ( player ) . add ( potion ) ;
allThrownPotions . add ( new AbstractMap . SimpleEntry < > ( potion , System . currentTimeMillis ( ) ) ) ;
2020-08-02 00:46:14 +00:00
if ( recentlyThrownPotions . get ( player ) . size ( ) > 128 )
2020-07-14 19:00:22 +00:00
{
recentlyThrownPotions . get ( player ) . remove ( 0 ) ;
}
2020-08-02 00:46:14 +00:00
if ( allThrownPotions . size ( ) > 1024 )
2020-07-14 19:00:22 +00:00
{
allThrownPotions . remove ( 0 ) ; // Remove the first element in the set.
}
}
2017-12-31 03:58:20 +00:00
}
2020-07-14 19:00:22 +00:00
}
public List < Map . Entry < ThrownPotion , Long > > getPlayerThrownPotions ( Player player )
{
List < Map . Entry < ThrownPotion , Long > > thrownPotions = new ArrayList < > ( ) ;
2017-12-31 03:58:20 +00:00
2020-08-02 00:46:14 +00:00
for ( Map . Entry < ThrownPotion , Long > potionEntry : allThrownPotions )
2017-12-31 03:58:20 +00:00
{
2020-07-14 19:00:22 +00:00
ThrownPotion potion = potionEntry . getKey ( ) ;
2020-08-02 00:46:14 +00:00
if ( potion . getShooter ( ) ! = null & & potion . getShooter ( ) . equals ( player ) )
2020-07-14 19:00:22 +00:00
{
thrownPotions . add ( potionEntry ) ;
}
2017-12-31 03:58:20 +00:00
}
2020-07-14 19:00:22 +00:00
return thrownPotions ;
}
public boolean isTrollPotion ( ThrownPotion potion )
{
int badEffectsDetected = 0 ;
2020-08-02 00:46:14 +00:00
for ( PotionEffect effect : potion . getEffects ( ) )
2017-12-31 03:58:20 +00:00
{
2020-08-02 00:46:14 +00:00
if ( badPotionEffects . contains ( effect . getType ( ) ) & & effect . getAmplifier ( ) > 2 & & effect . getDuration ( ) > 200 )
2017-12-31 03:58:20 +00:00
{
2020-07-14 19:00:22 +00:00
badEffectsDetected + + ;
2017-12-31 03:58:20 +00:00
}
}
2020-07-14 19:00:22 +00:00
return badEffectsDetected > 0 ;
2017-12-31 03:58:20 +00:00
}
2020-12-25 19:46:43 +00:00
public List < Map . Entry < ThrownPotion , Long > > getAllThrownPotions ( )
{
return allThrownPotions ;
}
public Map < Player , List < ThrownPotion > > getRecentlyThrownPotions ( )
{
return recentlyThrownPotions ;
}
public List < PotionEffectType > getBadPotionEffects ( )
{
return badPotionEffects ;
}
2020-12-04 00:28:53 +00:00
}