From a377bdfe48e957b151b75ef9b8483281de041080 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Sat, 6 Apr 2024 13:11:05 +0200 Subject: [PATCH] Implement player death message limits The code now includes player death message limits, with a configured maximum number of messages per defined period. This was added in the PlayerListener class and configuration options were placed in Config.java. This change will restrict spamming of death messages. --- .../java/com/alttd/chat/config/Config.java | 8 +++++++ .../alttd/chat/listeners/PlayerListener.java | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/api/src/main/java/com/alttd/chat/config/Config.java b/api/src/main/java/com/alttd/chat/config/Config.java index a2028f0..2bc277c 100755 --- a/api/src/main/java/com/alttd/chat/config/Config.java +++ b/api/src/main/java/com/alttd/chat/config/Config.java @@ -543,4 +543,12 @@ public final class Config { NICK_ALLOWED_COLOR_CODESLIST = getList("nicknames.allowed-color-codes", List.of("&0", "&1", "&2", "&3", "&4", "&5", "&6", "&7", "&8", "&9", "&a", "&b", "&c", "&d", "&e", "&f", "&r")); NICK_CURRENT = getString("nicknames.messages.nick-current", NICK_CURRENT); } + + public static int DEATH_MESSAGES_MAX_PER_PERIOD = 5; + public static int DEATH_MESSAGES_LIMIT_PERIOD_MINUTES = 15; + + private static void deathMessagesSettings() { + DEATH_MESSAGES_MAX_PER_PERIOD = getInt("death-messages.max-per-period", DEATH_MESSAGES_MAX_PER_PERIOD); + DEATH_MESSAGES_LIMIT_PERIOD_MINUTES = getInt("death-messages.limit-period-minutes", DEATH_MESSAGES_LIMIT_PERIOD_MINUTES); + } } diff --git a/galaxy/src/main/java/com/alttd/chat/listeners/PlayerListener.java b/galaxy/src/main/java/com/alttd/chat/listeners/PlayerListener.java index 75a6f47..037a302 100755 --- a/galaxy/src/main/java/com/alttd/chat/listeners/PlayerListener.java +++ b/galaxy/src/main/java/com/alttd/chat/listeners/PlayerListener.java @@ -17,9 +17,14 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.HashMap; +import java.util.Stack; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -86,4 +91,22 @@ public class PlayerListener implements Listener { } } + private final HashMap> sendPlayerDeaths = new HashMap<>(); + @EventHandler(ignoreCancelled = true) + public void onPlayerDeath(PlayerDeathEvent event) { + UUID uuid = event.getPlayer().getUniqueId(); + Stack playerDeathsStack = sendPlayerDeaths.computeIfAbsent(uuid, key -> new Stack<>()); + Instant cutOff = Instant.now().minus(Config.DEATH_MESSAGES_LIMIT_PERIOD_MINUTES, ChronoUnit.MINUTES); + + while (playerDeathsStack.peek().isBefore(cutOff)) { + playerDeathsStack.pop(); + } + + if (playerDeathsStack.size() > Config.DEATH_MESSAGES_MAX_PER_PERIOD) { + event.deathMessage(Component.empty()); + return; + } + playerDeathsStack.push(Instant.now()); + } + }