adds anti spam

likely needs tweaking
This commit is contained in:
Telesphoreo 2022-04-20 19:46:06 -05:00
parent 7c27c03d98
commit b1fcf5e348
3 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,41 @@
package dev.plex.listener.impl;
import dev.plex.listener.PlexListener;
import dev.plex.services.impl.TimingService;
import io.papermc.paper.event.player.AsyncChatEvent;
import java.util.UUID;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
public class AntiSpamListener extends PlexListener
{
@EventHandler
public void onChat(AsyncChatEvent event)
{
TimingService.cooldowns.merge(event.getPlayer().getUniqueId(), 1L, Long::sum);
if (getCount(event.getPlayer().getUniqueId()) > 8L)
{
event.getPlayer().sendMessage(Component.text("Please refrain from spamming messages.").color(NamedTextColor.GRAY));
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
{
TimingService.cooldowns.merge(event.getPlayer().getUniqueId(), 1L, Long::sum);
if (getCount(event.getPlayer().getUniqueId()) > 8L)
{
event.getPlayer().sendMessage(Component.text("Please refrain from spamming commands.").color(NamedTextColor.GRAY));
event.setCancelled(true);
}
}
public long getCount(UUID uuid)
{
return TimingService.cooldowns.getOrDefault(uuid, 1L);
}
}

View File

@ -6,6 +6,7 @@ import dev.plex.services.impl.AutoWipeService;
import dev.plex.services.impl.BanService;
import dev.plex.services.impl.CommandBlockerService;
import dev.plex.services.impl.GameRuleService;
import dev.plex.services.impl.TimingService;
import dev.plex.services.impl.UpdateCheckerService;
import java.util.List;
import org.bukkit.Bukkit;
@ -21,6 +22,7 @@ public class ServiceManager
registerService(new BanService());
registerService(new CommandBlockerService());
registerService(new GameRuleService());
registerService(new TimingService());
registerService(new UpdateCheckerService());
}

View File

@ -0,0 +1,28 @@
package dev.plex.services.impl;
import dev.plex.services.AbstractService;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class TimingService extends AbstractService
{
public static final Map<UUID, Long> cooldowns = new HashMap<>();
public TimingService()
{
super(true, true);
}
@Override
public void run()
{
cooldowns.clear();
}
@Override
public int repeatInSeconds()
{
return 5;
}
}