TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/AntiSpam.java
Paldiu 5c0f77c7c5 Removal of Lombok
Lombok implementation removal.

I have also gone through and replaced things with inline methods and variables, lambdas, and simplified loops down, removed unnecessary guard clauses, and overall cleaned up every single class. This took a long time, please do remember to follow proper naming conventions, don't include unnecessary guard clauses, follow exception rules and comment rules, and please PLEASE remember to use the DIAMOND OPERATOR rather than just inferring RAW TYPES!!!

Thank you!!
2020-12-25 14:46:43 -05:00

131 lines
3.8 KiB
Java

package me.totalfreedom.totalfreedommod;
import java.util.ArrayList;
import java.util.List;
import me.totalfreedom.totalfreedommod.player.FPlayer;
import me.totalfreedom.totalfreedommod.util.FSync;
import me.totalfreedom.totalfreedommod.util.FUtil;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
public class AntiSpam extends FreedomService
{
public static final int MSG_PER_CYCLE = 8;
public static final int TICKS_PER_CYCLE = 2 * 10;
//
public BukkitTask cycleTask = null;
List<Player> markedForDeath = new ArrayList<>();
@Override
public void onStart()
{
new BukkitRunnable()
{
@Override
public void run()
{
cycle();
}
}.runTaskTimer(plugin, TICKS_PER_CYCLE, TICKS_PER_CYCLE);
}
@Override
public void onStop()
{
FUtil.cancel(cycleTask);
}
private void cycle()
{
for (Player player : server.getOnlinePlayers())
{
final FPlayer playerdata = plugin.pl.getPlayer(player);
// TODO: Move each to their own section
playerdata.resetMsgCount();
playerdata.resetBlockDestroyCount();
playerdata.resetBlockPlaceCount();
}
}
@EventHandler(priority = EventPriority.LOW)
public void onAsyncPlayerChat(AsyncPlayerChatEvent event)
{
final Player player = event.getPlayer();
if (plugin.al.isAdmin(player))
{
return;
}
String message = event.getMessage().trim();
final FPlayer playerdata = plugin.pl.getPlayerSync(player);
// Check for spam
if (playerdata.incrementAndGetMsgCount() > MSG_PER_CYCLE)
{
if (!markedForDeath.contains(player))
{
markedForDeath.add(player);
FSync.bcastMsg(player.getName() + " was automatically kicked for spamming chat.", ChatColor.RED);
FSync.autoEject(player, "Kicked for spamming chat.");
playerdata.resetMsgCount();
event.setCancelled(true);
}
}
else if (playerdata.incrementAndGetMsgCount() > MSG_PER_CYCLE / 2)
{
FUtil.playerMsg(player, "Please refrain from spamming chat.", ChatColor.GRAY);
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
{
String command = event.getMessage();
final Player player = event.getPlayer();
final FPlayer fPlayer = plugin.pl.getPlayer(player);
fPlayer.setLastCommand(command);
if (fPlayer.allCommandsBlocked())
{
FUtil.playerMsg(player, "Your commands have been blocked by an admin.", ChatColor.RED);
event.setCancelled(true);
return;
}
if (plugin.al.isAdmin(player))
{
return;
}
if (fPlayer.incrementAndGetMsgCount() > MSG_PER_CYCLE)
{
FUtil.bcastMsg(player.getName() + " was automatically kicked for spamming commands.", ChatColor.RED);
plugin.ae.autoEject(player, "Kicked for spamming commands.");
fPlayer.resetMsgCount();
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerKick(PlayerKickEvent event)
{
markedForDeath.remove(event.getPlayer());
}
}