2015-11-15 23:32:04 +00:00
|
|
|
package me.totalfreedom.totalfreedommod;
|
|
|
|
|
2021-04-22 11:54:29 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
2015-11-15 23:32:04 +00:00
|
|
|
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.scheduler.BukkitRunnable;
|
|
|
|
import org.bukkit.scheduler.BukkitTask;
|
|
|
|
|
2021-04-22 11:54:29 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2016-03-01 16:47:01 +00:00
|
|
|
public class AntiSpam extends FreedomService
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
public static final int MSG_PER_CYCLE = 8;
|
|
|
|
public static final int TICKS_PER_CYCLE = 2 * 10;
|
|
|
|
//
|
|
|
|
public BukkitTask cycleTask = null;
|
2021-04-22 11:54:29 +00:00
|
|
|
private Map<Player, Integer> muteCounts = new HashMap<>();
|
2015-11-15 23:32:04 +00:00
|
|
|
|
|
|
|
@Override
|
2020-07-01 01:51:06 +00:00
|
|
|
public void onStart()
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
new BukkitRunnable()
|
|
|
|
{
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
cycle();
|
|
|
|
}
|
|
|
|
}.runTaskTimer(plugin, TICKS_PER_CYCLE, TICKS_PER_CYCLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-07-01 01:51:06 +00:00
|
|
|
public void onStop()
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
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();
|
2018-03-18 08:32:50 +00:00
|
|
|
|
2020-12-04 00:28:53 +00:00
|
|
|
if (plugin.al.isAdmin(player))
|
2018-03-18 08:32:50 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
final FPlayer playerdata = plugin.pl.getPlayerSync(player);
|
2021-04-22 11:54:29 +00:00
|
|
|
int count = muteCounts.getOrDefault(player, 0);
|
|
|
|
int minutes = ConfigEntry.ANTISPAM_MINUTES.getInteger();
|
2015-11-15 23:32:04 +00:00
|
|
|
|
|
|
|
// Check for spam
|
2021-04-22 11:54:29 +00:00
|
|
|
if (playerdata.incrementAndGetMsgCount() > MSG_PER_CYCLE && !playerdata.isMuted())
|
2020-12-25 19:46:43 +00:00
|
|
|
{
|
2021-04-22 11:54:29 +00:00
|
|
|
count++;
|
|
|
|
muteCounts.put(player, count);
|
2015-11-15 23:32:04 +00:00
|
|
|
|
2021-04-22 11:54:29 +00:00
|
|
|
int time = count * minutes;
|
|
|
|
playerdata.setMuted(true, time);
|
2015-11-15 23:32:04 +00:00
|
|
|
|
2021-04-22 11:54:29 +00:00
|
|
|
FSync.bcastMsg(String.format("%s has automatically been muted for %d minutes for spamming chat.",
|
|
|
|
player.getName(),
|
|
|
|
time),
|
|
|
|
ChatColor.RED);
|
|
|
|
|
|
|
|
playerdata.resetMsgCount();
|
|
|
|
event.setCancelled(true);
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
2020-01-11 22:46:14 +00:00
|
|
|
else if (playerdata.incrementAndGetMsgCount() > MSG_PER_CYCLE / 2)
|
|
|
|
{
|
|
|
|
FUtil.playerMsg(player, "Please refrain from spamming chat.", ChatColor.GRAY);
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
2020-12-25 19:46:43 +00:00
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
2020-12-04 00:28:53 +00:00
|
|
|
if (plugin.al.isAdmin(player))
|
2018-03-18 08:32:50 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
if (fPlayer.incrementAndGetMsgCount() > MSG_PER_CYCLE)
|
|
|
|
{
|
|
|
|
FUtil.bcastMsg(player.getName() + " was automatically kicked for spamming commands.", ChatColor.RED);
|
2016-05-12 19:40:39 +00:00
|
|
|
plugin.ae.autoEject(player, "Kicked for spamming commands.");
|
2015-11-15 23:32:04 +00:00
|
|
|
|
|
|
|
fPlayer.resetMsgCount();
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|
2020-12-04 00:28:53 +00:00
|
|
|
}
|