Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
4fb43e2
Check if incoming message is the same as last message
MochaMilkie Nov 9, 2026
f14e01c
Add antispam to highest chat listner
MochaMilkie Nov 9, 2026
e7d327a
Check for message matching just with more length
MochaMilkie Nov 9, 2026
7d3fec7
Add getAntiSpam
MochaMilkie Nov 9, 2026
c5660e9
Added antispam to chat events. Added to highest priority where applic…
MochaMilkie Nov 9, 2026
54e7759
Added antispam to chat events. Added to highest priority where applic…
MochaMilkie Nov 9, 2026
f4e11f8
Change new message to accept an event to manipulate
MochaMilkie Nov 11, 2026
35e63e5
Not sure I need these here
MochaMilkie Nov 11, 2026
fe3b06f
Pass the event to Antispam
MochaMilkie Nov 11, 2026
bdec779
Add antispam settings to config.yml
MochaMilkie Nov 12, 2026
ee3847a
Added antispam timer to increment timers since the players last sent …
MochaMilkie Nov 12, 2026
41aba0c
Added checks for runable commands in response to a player sending a d…
MochaMilkie Nov 12, 2026
ab40243
Added duplicate punish and caps punish to config.yml
MochaMilkie Nov 12, 2026
a9f8dd1
Added chat cooldown bypass to plugin.yml
MochaMilkie Nov 12, 2026
2537dfe
Store the message in lowercase to help prevent bypass due to messages…
MochaMilkie Nov 12, 2026
fb8e802
Better explained features in config.yml
MochaMilkie Nov 12, 2026
c43ac41
Correct improper check for lastMessageTimer
MochaMilkie Nov 12, 2026
988fe6e
Proper initialization
MochaMilkie Nov 12, 2026
8f198dd
Fix improper chat punish checks and check if entry exists, if not add…
MochaMilkie Nov 12, 2026
9266d4f
Apply changes to the map itself not the queued entry
MochaMilkie Nov 12, 2026
183a911
Corrected config lookups
MochaMilkie Nov 12, 2026
7ca7e09
Corrected variables for comparison
MochaMilkie Nov 12, 2026
2eda8cd
Make hash maps concurrent for the async timer
MochaMilkie Nov 12, 2026
e32f2d2
Fire antispam before chat is processed
MochaMilkie Nov 12, 2026
b1f68a3
Make AntiSpam.java easier to maintain
MochaMilkie Nov 12, 2026
26a3c9d
fixed variable assignment
MochaMilkie Nov 12, 2026
20a3789
Fixed backwords logic on caps filter
MochaMilkie Nov 12, 2026
b4e9261
Add the message to the map after processing
MochaMilkie Nov 12, 2026
2d537b2
Check all messages in lowercase for duplication filter
MochaMilkie Nov 12, 2026
babf76b
Add method to separate out event types and process them correctly
MochaMilkie Nov 12, 2026
e8f8681
Pass along the event only since the event holds all the data we need.
MochaMilkie Nov 12, 2026
7232e03
Imports
MochaMilkie Nov 13, 2026
34c0547
Formatting
MochaMilkie Nov 13, 2026
fdcc5e9
Rebuilt potions of the AntiSpam service to provide results expected w…
MochaMilkie Nov 13, 2026
b6bf54f
Fire antispam after the AbstractChatEvent is created
MochaMilkie Nov 13, 2026
c248644
Formatting
MochaMilkie Nov 13, 2026
6ab4b75
Formatting
MochaMilkie Nov 13, 2026
221e449
Add PlayerQuitListener to remove players from maps, releasing memory
MochaMilkie Nov 13, 2026
10fffeb
Another rebuild of newMessage, return if already canceled, change set…
MochaMilkie Nov 13, 2026
4109b7d
Constructor for tests
MochaMilkie Nov 13, 2026
408050e
Assign the lastMessage once
MochaMilkie Nov 13, 2026
aa4699e
Added tests for antispam filters and cooldown
MochaMilkie Nov 13, 2026
7f3ff86
Register listener in the plugin class
MochaMilkie Nov 13, 2026
47ca925
Make events final
MochaMilkie Nov 13, 2026
a47b45d
Correct constructor issue
MochaMilkie Nov 13, 2026
1ed655e
Corrected tests to support the new comparison for duplication filters
MochaMilkie Nov 13, 2026
c04bfdf
Used levenshteinDistance to calculate how similar the new message is …
MochaMilkie Nov 13, 2026
00792ba
Formatting and imports
MochaMilkie Nov 13, 2026
cab0498
Add mention of using {player} in the punish command
MochaMilkie Nov 13, 2026
ef3366f
Add isSimilar to also check for shorter similar messages
MochaMilkie Nov 13, 2026
5983b18
Formatting
MochaMilkie Nov 13, 2026
c46f30f
Remove unused method
MochaMilkie Nov 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Essentials/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,27 @@ chat:
# You must restart your server after changing this setting.
paper-chat-events: true

#When enabled the plugin sends a message to the player letting them know not to send similar messages.
#Whether duplicate messages should be blocked.
duplicate-prevention: true

#Whether we run a command in response to duplicate messages. Leave empty for no punishment.
#Use {player} to insert the player that triggered this punishment
duplicate-punish:

#Whether chat should have a cooldown per player.
chat-cooldown-enable: false
chat-cooldown-time: 0

#When enabled the plugin sends a message to the player letting them know not to use too many capitol letters.
#Message is converted to lowercase before being sent
#Whether messages containing lots of capitols should be changed to lowercase
chat-caps-filter: true

#Use {player} to insert the player that triggered this punishment
#Whether we run a command in response to too many capitol letters. Leave empty for no punishment.
caps-punishment:

############################################################
# +------------------------------------------------------+ #
# | EssentialsX Protect | #
Expand Down
6 changes: 5 additions & 1 deletion EssentialsChat/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ plugins {

dependencies {
compileOnly project(':EssentialsX')
}
testImplementation project(':EssentialsX')

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.2'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.EssentialsLogger;
import com.earth2me.essentials.chat.processing.AntiSpam;
import com.earth2me.essentials.chat.processing.ChatHandler;
import com.earth2me.essentials.chat.processing.PaperChatHandler;
import com.earth2me.essentials.chat.processing.PlayerDisconnectListner;
import com.earth2me.essentials.metrics.MetricsWrapper;
import com.earth2me.essentials.utils.VersionUtil;
import net.ess3.api.IEssentials;
Expand All @@ -19,6 +21,7 @@
public class EssentialsChat extends JavaPlugin {
private transient IEssentials ess;
private transient MetricsWrapper metrics = null;
public transient AntiSpam antiSpam;

@Override
public void onEnable() {
Expand All @@ -44,6 +47,8 @@ public void onEnable() {
if (metrics == null) {
metrics = new MetricsWrapper(this, 3814, false);
}
antiSpam = new AntiSpam(ess);
pluginManager.registerEvents(new PlayerDisconnectListner(), this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package com.earth2me.essentials.chat.processing;

import com.earth2me.essentials.IEssentials;
import net.ess3.provider.AbstractChatEvent;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

public class AntiSpam {
private static AntiSpam antiSpam;
private final IEssentials ess;

private final FileConfiguration config;
private final Map<UUID, String> lastMessage;
private final Map<UUID, Long> cooldowns;

public AntiSpam(IEssentials ess) {
this.ess = ess;
antiSpam = this;
config = ess.getConfig();
lastMessage = new ConcurrentHashMap<>();
cooldowns = new ConcurrentHashMap<>();
}

public void newMessage(AbstractChatEvent event) {
if (event.isCancelled()) {
return;
}

final Player player = event.getPlayer();
final String message = event.getMessage();

event.setMessage(capsFilter(message, player));

if (duplicationFilter(message, player)) {
event.setCancelled(true);
return;
}

if (chatCooldown(player)) {
event.setCancelled(true);
return;
}

lastMessage.put(player.getUniqueId(), message.toLowerCase(Locale.ROOT));
}

private String capsFilter(String message, Player player) {
if(ess.getConfig().getBoolean("chat.chat-caps-filter")) {
int count = 0;
for (int i = 0; i < message.length(); i++) {
if (Character.isUpperCase(message.charAt(i))) {
count++;
}
}
if (message.length() * 0.70 < count) {
//return the message to chat with lowercase replaced
player.sendMessage(ChatColor.RED + "Please do not spam capital letters in chat");
final String command = config.getString("caps-punish");

if (command != null && !command.trim().isEmpty()) {
ess.getServer().dispatchCommand(
ess.getServer().getConsoleSender(),
command.replace("{player}", player.getName())
);
}
return message.toLowerCase(Locale.ROOT);
}
}
return message;

}

private boolean duplicationFilter(String message, Player player) {
if (!config.getBoolean("chat.duplicate-prevention")) {
return false;
}

final UUID uuid = player.getUniqueId();
final String normalizedMessage = message.toLowerCase(Locale.ROOT);
final String previousMessage = lastMessage.get(uuid);

if (previousMessage != null && isSimilar(previousMessage, normalizedMessage)) {
final String command = config.getString("duplication-punish");
if (command != null && !command.trim().isEmpty()) {
ess.getServer().dispatchCommand(
ess.getServer().getConsoleSender(),
command.replace("{player}", player.getName())
);
}
player.sendMessage(ChatColor.RED + "Please do not send the same message twice.");
return true;
}
return false;
}

private boolean chatCooldown(Player player) {
if (player.hasPermission("essentials.chat.bypass.cooldown")
|| !config.getBoolean("chat.chat-cooldown-enable")) {
return false;
}
final long cooldownTime = config.getInt("chat.chat-cooldown-time") * 1000L;
final UUID uuid = player.getUniqueId();
final long currentTime = System.currentTimeMillis();
final long lastMessageTime = cooldowns.getOrDefault(uuid, 0L);
final long remaining = cooldownTime - (currentTime - lastMessageTime);

if (remaining > 0) {
player.sendMessage(ChatColor.RED + "Please wait "
+ (remaining / 1000 + 1)
+ " seconds before sending another message.");
return true;
}

cooldowns.put(uuid, currentTime);
return false;
}

public static AntiSpam getAntiSpam(){
return antiSpam;
}

public void unloadPlayer(Player player){
lastMessage.remove(player.getUniqueId());
cooldowns.remove(player.getUniqueId());
}

private int levenshteinDistance(String a, String b) {
final int[] costs = new int[b.length() + 1];

for (int j = 0; j <= b.length(); j++) {
costs[j] = j;
}

for (int i = 1; i <= a.length(); i++) {
costs[0] = i;
int previousCost = i - 1;

for (int j = 1; j <= b.length(); j++) {
final int currentCost = costs[j];

costs[j] = Math.min(
Math.min(costs[j] + 1, costs[j - 1] + 1),
previousCost + (a.charAt(i - 1) == b.charAt(j - 1) ? 0 : 1)
);

previousCost = currentCost;
}
}

return costs[b.length()];
}

private boolean isSimilar(String a, String b) {
if (a.equals(b)) {
return true;
}

final int distance = levenshteinDistance(a, b);
final int maxLength = Math.max(a.length(), b.length());

return distance <= 1
|| 1.0 - (double) distance / maxLength >= 0.85;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ private final class ChatHighest implements ChatListener {
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerChat(AsyncPlayerChatEvent event) {
final AbstractChatEvent absEvent = wrap(event);
AntiSpam.getAntiSpam().newMessage(absEvent);
handleChatPostFormat(absEvent);
handleChatSubmit(absEvent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void onChatNormal(AbstractChatEvent event) {

@Override
public void onChatHighest(AbstractChatEvent event) {
AntiSpam.getAntiSpam().newMessage(event);
handleChatPostFormat(event);
handleChatSubmit(event);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.earth2me.essentials.chat.processing;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;

public class PlayerDisconnectListner implements Listener {
@EventHandler
public void onPlayerDisconnect(PlayerQuitEvent event){
AntiSpam.getAntiSpam().unloadPlayer(event.getPlayer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ public void removeRecipients(Predicate<Player> predicate) {
public void addRecipient(Player player) {
event.getRecipients().add(player);
}

}
3 changes: 3 additions & 0 deletions EssentialsChat/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ permissions:
essentials.chat.receive.question:
default: true
description: Allows access to receiving global question chat messages
essentials.chat.bypass.cooldown:
default: false
description: Allows a player to bypass chat cooldowns
Loading