1717import org .bukkit .plugin .PluginManager ;
1818import org .bukkit .plugin .java .JavaPlugin ;
1919
20- import java .util .ArrayList ;
20+ import java .util .HashSet ;
2121import java .util .LinkedList ;
22- import java .util .List ;
22+ import java .util .Set ;
23+ import java .util .UUID ;
24+ import java .util .concurrent .ConcurrentHashMap ;
25+ import java .util .stream .Collectors ;
2326
2427public class AntiSpam extends JavaPlugin implements Listener , CommandExecutor {
25- public static List < String > bots = new ArrayList <>();
26- static List <String > whisperCommands = new ArrayList <>();
27- final ArrayList < Player > notMoved = new ArrayList <> ();
28+ private static final Set < UUID > bots = new HashSet <>();
29+ private static final Set <String > whisperCommands = new HashSet <>();
30+ private final Set < UUID > notMoved = ConcurrentHashMap . newKeySet ();
2831
2932 FileConfiguration config ;
3033
@@ -46,8 +49,8 @@ public void onEnable() {
4649 saveDefaultConfig ();
4750 config = this .getConfig ();
4851
49- bots = config .getStringList ("bots" );
50- whisperCommands = config .getStringList ("whisperCommands" );
52+ bots . addAll ( config .getStringList ("bots" ). stream (). map ( UUID :: fromString ). collect ( Collectors . toSet ()) );
53+ whisperCommands . addAll ( config .getStringList ("whisperCommands" ) );
5154 notMovedCheckEnabled = config .getBoolean ("not-moved-check-enabled" );
5255 messageCannotTalk = getConfig ().getString ("cannot-talk" );
5356 messageSpamTalk = getConfig ().getString ("spam-talk-message" );
@@ -73,7 +76,8 @@ public void onEnable() {
7376 isSpam = false ;
7477
7578 Bukkit .getScheduler ().scheduleSyncRepeatingTask (this , () -> {
76- int threshold = config .getInt ("maximum-characters-per-minute" ) * Bukkit .getOnlinePlayers ().size () / 2 ;
79+ // assume that only 10% of the players ever chat ;)
80+ int threshold = config .getInt ("maximum-characters-per-minute" ) * Bukkit .getOnlinePlayers ().size () / 10 ;
7781 isSpam = cumulatedMessageSize > threshold ;
7882
7983 log ("isSpam" , "Spam: " + isSpam + " Size: " + cumulatedMessageSize + " Threshold: " + threshold );
@@ -158,8 +162,7 @@ public void onPlayerDeath(PlayerDeathEvent e) {
158162 @ EventHandler
159163 public void onPlayerLeaveEvent (PlayerQuitEvent event ) {
160164 if (notMovedCheckEnabled ) {
161- Player player = event .getPlayer ();
162- notMovedRemove (player );
165+ notMoved .remove (event .getPlayer ().getUniqueId ());
163166 }
164167
165168 spamBotCheck .setPlayerCount (getServer ().getOnlinePlayers ().size ());
@@ -175,10 +178,7 @@ public void onPlayerLeaveEvent(PlayerQuitEvent event) {
175178 public void onPlayerChat (AsyncPlayerChatEvent event ) {
176179 cumulatedMessageSize += event .getMessage ().length ();
177180
178- Player player = event .getPlayer ();
179- String message = event .getMessage ();
180-
181- if (isSpam (player , message , false )) {
181+ if (isSpam (event .getPlayer (), event .getMessage (), false )) {
182182 event .setCancelled (true );
183183 }
184184 }
@@ -191,7 +191,6 @@ public void onPlayerChat(AsyncPlayerChatEvent event) {
191191 */
192192 @ EventHandler (priority = EventPriority .LOWEST , ignoreCancelled = true )
193193 public void onPlayerCommandPreprocess (PlayerCommandPreprocessEvent event ) {
194- Player player = event .getPlayer ();
195194 String message = event .getMessage ();
196195
197196 if (whisperCommands .stream ().anyMatch (cmd -> message .toLowerCase ().startsWith ("/" + cmd + " " ))) {
@@ -200,7 +199,7 @@ public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
200199 if (messagePart .length == 3 ) {
201200 cumulatedMessageSize += event .getMessage ().length ();
202201
203- if (isSpam (player , messagePart [2 ], true )) {
202+ if (isSpam (event . getPlayer () , messagePart [2 ], true )) {
204203 event .setCancelled (true );
205204 }
206205 }
@@ -222,9 +221,7 @@ public void onPlayerMove(PlayerMoveEvent e) {
222221
223222 Player player = e .getPlayer ();
224223
225- if (this .notMoved .contains (player )) {
226- notMovedRemove (player );
227- }
224+ notMoved .remove (e .getPlayer ().getUniqueId ());
228225 }
229226
230227 /**
@@ -245,7 +242,7 @@ private boolean isSpam(Player player, String message, boolean isWhispering) {
245242 return false ;
246243 }
247244
248- if (notMovedCheckEnabled && this .notMoved .contains (player )) {
245+ if (notMovedCheckEnabled && this .notMoved .contains (player . getUniqueId () )) {
249246 player .sendMessage (ChatColor .translateAlternateColorCodes ('&' , messageCannotTalk ));
250247 return true ;
251248 }
@@ -260,7 +257,7 @@ private boolean isSpam(Player player, String message, boolean isWhispering) {
260257 // cooldown for the new message is added even on violation
261258 isSpam = true ;
262259 failedTest = "Flood" ;
263- } else if (spamBotCheck .isNoBlanksSpam (message )) {
260+ } else if (spamBotCheck .isNoBlanksSpam (player . getUniqueId (), message )) {
264261 // checks the frequency of whitespaces
265262 isSpam = true ;
266263 failedTest = "No Blanks" ;
@@ -288,22 +285,22 @@ private boolean isSpam(Player player, String message, boolean isWhispering) {
288285
289286 /**
290287 * Checks, if a certain player is in the bot whitelist
291- * TODO: add UUID check
292288 *
293289 * @param player
294290 * @return
295291 */
296292 private boolean isBot (Player player ) {
297- for (String bot : AntiSpam .bots ) {
298- if (player .getName ().toLowerCase ().contentEquals (bot .toLowerCase ())) {
299- return true ;
300- }
301- //if(player.getUniqueId().equals(UUID.fromString(bot))) {
302- // return true;
303- //}
304- }
293+ return isBot (player .getUniqueId ());
294+ }
305295
306- return false ;
296+ /**
297+ * Checks, if a certain uuid is in the bot whitelist
298+ *
299+ * @param uuid
300+ * @return
301+ */
302+ private boolean isBot (UUID uuid ) {
303+ return bots .contains (uuid );
307304 }
308305
309306 /**
@@ -312,27 +309,16 @@ private boolean isBot(Player player) {
312309 * @param player
313310 */
314311 private void notMovedAdd (Player player ) {
315- for (String bot : bots ) {
316- if (bot .toLowerCase ().contentEquals (player .getName ().toLowerCase ())) {
317- log ("Bot" , player .getName ());
318- return ;
319- }
312+ if (bots .contains (player .getUniqueId ())) {
313+ log ("Bot" , player .getName ());
314+ return ;
320315 }
321316
322317 if (!player .hasPermission ("move.bypass" )) {
323- this .notMoved .add (player );
318+ this .notMoved .add (player . getUniqueId () );
324319 }
325320 }
326321
327- /**
328- * remove "not moved" flag
329- *
330- * @param player
331- */
332- private void notMovedRemove (Player player ) {
333- this .notMoved .remove (player );
334- }
335-
336322 /**
337323 * print a log message
338324 *
0 commit comments