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;
|
2022-11-21 05:57:31 +00:00
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
import net.kyori.adventure.text.format.NamedTextColor;
|
2015-11-15 23:32:04 +00:00
|
|
|
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;
|
|
|
|
|
2021-04-22 11:54:29 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2022-11-17 08:34:45 +00:00
|
|
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2021-04-22 11:54:29 +00:00
|
|
|
|
2016-03-01 16:47:01 +00:00
|
|
|
public class AntiSpam extends FreedomService
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2022-11-17 08:34:45 +00:00
|
|
|
private ScheduledThreadPoolExecutor cycle;
|
2015-11-15 23:32:04 +00:00
|
|
|
public static final int MSG_PER_CYCLE = 8;
|
|
|
|
//
|
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
|
|
|
{
|
2022-11-17 08:34:45 +00:00
|
|
|
cycle = new ScheduledThreadPoolExecutor(1);
|
|
|
|
cycle.scheduleAtFixedRate(this::cycle, 0, 1, TimeUnit.SECONDS);
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-07-01 01:51:06 +00:00
|
|
|
public void onStop()
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2022-11-17 08:34:45 +00:00
|
|
|
cycle.shutdownNow();
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void cycle()
|
|
|
|
{
|
2022-11-17 08:34:45 +00:00
|
|
|
server.getOnlinePlayers().stream().map(player -> plugin.pl.getPlayer(player)).forEach(fPlayer ->
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
// TODO: Move each to their own section
|
2022-11-17 08:34:45 +00:00
|
|
|
fPlayer.resetMsgCount();
|
|
|
|
fPlayer.resetBlockDestroyCount();
|
|
|
|
fPlayer.resetBlockPlaceCount();
|
|
|
|
});
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
|
2022-11-21 05:57:31 +00:00
|
|
|
server.broadcast(Component.text(player.getName()).append(Component.text(" has been automatically muted for "))
|
|
|
|
.append(Component.text(time)).append(Component.text(" minutes for spamming chat."))
|
|
|
|
.color(NamedTextColor.RED));
|
2021-04-22 11:54:29 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2022-11-21 05:57:31 +00:00
|
|
|
player.sendMessage(Component.text("Please refrain from spamming chat.", NamedTextColor.GRAY));
|
2020-01-11 22:46:14 +00:00
|
|
|
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())
|
|
|
|
{
|
2022-11-21 05:57:31 +00:00
|
|
|
player.sendMessage(Component.text("Your commands have been blocked by an admin.", NamedTextColor.RED));
|
2015-11-15 23:32:04 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-11-21 05:57:31 +00:00
|
|
|
server.broadcast(Component.text(player.getName())
|
|
|
|
.append(Component.text(" was automatically kicked for spamming commands."))
|
|
|
|
.color(NamedTextColor.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
|
|
|
}
|