TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/Monitors.java

146 lines
5.6 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod;
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 lombok.Getter;
import me.totalfreedom.totalfreedommod.util.FUtil;
import org.bukkit.Bukkit;
2018-01-07 19:33:58 +00:00
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.entity.ThrownPotion;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.LingeringPotionSplashEvent;
import org.bukkit.event.entity.PotionSplashEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
public class Monitors extends FreedomService
{
@Getter
private final List<Map.Entry<ThrownPotion, Long>> allThrownPotions = new ArrayList<>();
private final Map<Player, List<ThrownPotion>> recentlyThrownPotions = new HashMap<>();
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".
@Override
2020-07-01 01:51:06 +00:00
public void onStart()
{
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-08-15 13:33:29 +00:00
for (ThrownPotion potion : playerThrownPotions)
{
if (isTrollPotion(potion))
{
2020-08-15 13:33:29 +00:00
trollPotions++;
}
}
2020-08-15 13:33:29 +00:00
plugin.al.messageAllAdmins(ChatColor.translateAlternateColorCodes('&', String.format("&8[&ePotionSpy&8] &r%s splashed %s %s at X: %s Y: %s Z: %s in the world '%s'%s.",
player.getName(), potionsThrown, potionsThrown == 1 ? "potion" : "potions", latestThrownPotion.getLocation().getBlockX(), latestThrownPotion.getLocation().getBlockY(), latestThrownPotion.getLocation().getBlockZ(),
latestThrownPotion.getWorld().getName(), trollPotions > 0 ? String.format(" &c(most likely troll %s)", trollPotions == 1 ? "potion" : "potions") : "")));
}
recentlyThrownPotions.clear();
}, 0L, 40L);
}
@Override
2020-07-01 01:51:06 +00:00
public void onStop()
{
}
2018-01-07 19:33:58 +00:00
@EventHandler(priority = EventPriority.MONITOR)
public void onLingeringPotionSplash(LingeringPotionSplashEvent event)
{
if (event.getEntity().getShooter() instanceof Player)
{
ThrownPotion potion = event.getEntity();
2020-08-02 00:46:14 +00:00
if (potion.getShooter() instanceof Player)
{
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)
{
recentlyThrownPotions.get(player).remove(0);
}
2020-08-02 00:46:14 +00:00
if (allThrownPotions.size() > 1024)
{
allThrownPotions.remove(0); // Remove the first element in the set.
}
}
}
}
2018-01-07 19:33:58 +00:00
@EventHandler(priority = EventPriority.MONITOR)
public void onPotionSplash(PotionSplashEvent event)
{
if (event.getEntity().getShooter() instanceof Player)
{
ThrownPotion potion = event.getEntity();
2020-08-02 00:46:14 +00:00
if (potion.getShooter() instanceof Player)
{
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)
{
recentlyThrownPotions.get(player).remove(0);
}
2020-08-02 00:46:14 +00:00
if (allThrownPotions.size() > 1024)
{
allThrownPotions.remove(0); // Remove the first element in the set.
}
}
}
}
public List<Map.Entry<ThrownPotion, Long>> getPlayerThrownPotions(Player player)
{
List<Map.Entry<ThrownPotion, Long>> thrownPotions = new ArrayList<>();
2020-08-02 00:46:14 +00:00
for (Map.Entry<ThrownPotion, Long> potionEntry : allThrownPotions)
{
ThrownPotion potion = potionEntry.getKey();
2020-08-02 00:46:14 +00:00
if (potion.getShooter() != null && potion.getShooter().equals(player))
{
thrownPotions.add(potionEntry);
}
}
return thrownPotions;
}
public boolean isTrollPotion(ThrownPotion potion)
{
int badEffectsDetected = 0;
2020-08-02 00:46:14 +00:00
for (PotionEffect effect : potion.getEffects())
{
2020-08-02 00:46:14 +00:00
if (badPotionEffects.contains(effect.getType()) && effect.getAmplifier() > 2 && effect.getDuration() > 200)
{
badEffectsDetected++;
}
}
return badEffectsDetected > 0;
}
}