Added history for commands/message to avoid bypassing afk by sending the same command/message regularly

This commit is contained in:
Teriuihi 2023-03-30 14:11:54 +02:00
parent 91a492b87f
commit c9c1ca2c51
3 changed files with 61 additions and 0 deletions

View File

@ -15,6 +15,7 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -158,22 +159,47 @@ public class AFKDetector extends JavaPlugin implements Listener {
}
}
StringHistory messageHistory = new StringHistory();
@EventHandler
public void onTalkCancel(AsyncChatEvent event) {
if (!Config.CHAT_WILL_CANCEL) {
return;
}
UUID uuid = event.getPlayer().getUniqueId();
String message = PlainTextComponentSerializer.plainText().serialize(event.message()).toLowerCase();
if (messageHistory.checkForMatch(uuid, message)) {
return;
}
Player player = event.getPlayer();
getPlayer(player).ResetAFK();
messageHistory.addEntry(uuid, message);
}
StringHistory commandHistory = new StringHistory();
@EventHandler
public void onCommandCancel(PlayerCommandPreprocessEvent event) {
if (!Config.COMMAND_WILL_CANCEL) {
return;
}
String[] s = event.getMessage().toLowerCase().split(" ");
if (s.length == 0)
return;
UUID uuid = event.getPlayer().getUniqueId();
String command = s[0];
if (commandHistory.checkForMatch(uuid, command)) {
return;
}
Player player = event.getPlayer();
getPlayer(player).ResetAFK();
commandHistory.addEntry(uuid, command);
}
/* @EventHandler

View File

@ -0,0 +1,33 @@
package com.alttd.afkdectector;
import com.alttd.afkdectector.config.Config;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import java.util.ArrayDeque;
import java.util.UUID;
public class StringHistory {
private final Object2ObjectOpenHashMap<UUID, ArrayDeque<String>> map = new Object2ObjectOpenHashMap<>();
public boolean checkForMatch(UUID uuid, String value) {
ArrayDeque<String> history = map.get(uuid);
if (history == null) {
return false;
}
for (String historyString : history) {
if (value.startsWith(historyString)) {
return true;
}
}
return false;
}
public void addEntry(UUID uuid, String value) {
ArrayDeque<String> history = map.computeIfAbsent(uuid, k -> new ArrayDeque<>());
history.addFirst(value);
if (history.size() > Config.MAX_DUPLICATE_HISTORY) {
history.removeLast();
}
}
}

View File

@ -26,9 +26,11 @@ public class Config extends AbstractConfiguration {
}
public static boolean DEBUG_MODE = false;
public static int MAX_DUPLICATE_HISTORY = 5;
private static void settings() {
DEBUG_MODE = config.getBoolean("debug-mode", DEBUG_MODE);
MAX_DUPLICATE_HISTORY = config.getInt("max-duplicate-history", MAX_DUPLICATE_HISTORY);
}
public static boolean SLEEP_IGNORE = false;