TotalFreedomMod/commons/src/main/java/me/totalfreedom/totalfreedommod/AntiSpam.java

118 lines
3.8 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.player.FPlayer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
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 java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class AntiSpam extends FreedomService
{
private ScheduledThreadPoolExecutor cycle;
public static final int MSG_PER_CYCLE = 8;
//
private Map<Player, Integer> muteCounts = new HashMap<>();
@Override
2020-07-01 01:51:06 +00:00
public void onStart()
{
cycle = new ScheduledThreadPoolExecutor(1);
cycle.scheduleAtFixedRate(this::cycle, 0, 1, TimeUnit.SECONDS);
}
@Override
2020-07-01 01:51:06 +00:00
public void onStop()
{
cycle.shutdownNow();
}
private void cycle()
{
server.getOnlinePlayers().stream().map(player -> plugin.pl.getPlayer(player)).forEach(fPlayer ->
{
// TODO: Move each to their own section
fPlayer.resetMsgCount();
fPlayer.resetBlockDestroyCount();
fPlayer.resetBlockPlaceCount();
});
}
@EventHandler(priority = EventPriority.LOW)
public void onAsyncPlayerChat(AsyncPlayerChatEvent event)
{
final Player player = event.getPlayer();
2018-03-18 08:32:50 +00:00
if (plugin.al.isAdmin(player))
2018-03-18 08:32:50 +00:00
{
return;
}
final FPlayer playerdata = plugin.pl.getPlayerSync(player);
int count = muteCounts.getOrDefault(player, 0);
int minutes = ConfigEntry.ANTISPAM_MINUTES.getInteger();
// Check for spam
if (playerdata.incrementAndGetMsgCount() > MSG_PER_CYCLE && !playerdata.isMuted())
{
count++;
muteCounts.put(player, count);
int time = count * minutes;
playerdata.setMuted(true, time);
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));
playerdata.resetMsgCount();
event.setCancelled(true);
}
else if (playerdata.incrementAndGetMsgCount() > MSG_PER_CYCLE / 2)
{
player.sendMessage(Component.text("Please refrain from spamming chat.", NamedTextColor.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())
{
player.sendMessage(Component.text("Your commands have been blocked by an admin.", NamedTextColor.RED));
event.setCancelled(true);
return;
}
if (plugin.al.isAdmin(player))
2018-03-18 08:32:50 +00:00
{
return;
}
if (fPlayer.incrementAndGetMsgCount() > MSG_PER_CYCLE)
{
server.broadcast(Component.text(player.getName())
.append(Component.text(" was automatically kicked for spamming commands."))
.color(NamedTextColor.RED));
TotalFreedomMod Electrum Version 5.0 This TotalFreedomMod release implements many changes. Most notably, the internals have been completely revamped. TotalFreedomMod now relies on the Aero library for core mechanics such as command handling and services. Another important change is the UUID system. In TotalFreedomMod Electrum, it has been completely removed. The core reason for this is that the system as a whole was very bugged. Additionally, it did not solve the primary reason for its conception: preserving player data when the player changes their username. This is because TotalFreedomMod servers usually run in offline-mode. This meaning that some of the players joining do not have a registerd Mojang UUID whatsoever. All in all, the UUID system was buggy, and it did not fix the reason it was implemented, so it has been completely removed. The admin list and the ban list now use usernames and IPs again. Lastly, many smaller changes have been implemented. Due to the amount of changes, they have not been named individualy. Please refer to the issues below for more details. Fixes #342 Fixes #350 Fixes #380 Fixes #684 Fixes #704 Fixes #716 Fixes #735 Fixes #745 Fixes #784 Fixes #765 Fixes #791 Fixes #805 Fixes #826 Fixes #883 Fixes #1524 Fixes #1534 Fixes #1536 Fixes #1538 Fixes #1545 Fixes #1546 Fixes #1568 Fixes #1627 Resolves #403 Resolves #435 Resolves #597 Resolves #603 Resolves #628 Resolves #690 Resolves #708 Resolves #747 Resolves #748 Resolves #749 Resolves #764 Resolves #767 Resolves #782 Resolves #809 Resolves #803 Resolves #811 Resolves #813 Resolves #830 Resolves #848 Resolves #856 Resolves #876 Resolves #908 Resolves #992 Resolves #1018 Resolves #1432 Resolves #1446 Resolves #1494 Resolves #1501 Resolves #1526 Resolves #1540 Resolves #1550 Resolves #1560 Resolves #1561 Resolves #1578 Resolves #1613
2016-05-12 19:40:39 +00:00
plugin.ae.autoEject(player, "Kicked for spamming commands.");
fPlayer.resetMsgCount();
event.setCancelled(true);
}
}
}