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.
This commit is contained in:
Teriuihi 2024-04-06 13:11:05 +02:00
parent 4af85d0e79
commit a377bdfe48
2 changed files with 31 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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<UUID, Stack<Instant>> sendPlayerDeaths = new HashMap<>();
@EventHandler(ignoreCancelled = true)
public void onPlayerDeath(PlayerDeathEvent event) {
UUID uuid = event.getPlayer().getUniqueId();
Stack<Instant> 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());
}
}