1
0
This commit is contained in:
Lee
2024-03-29 17:46:05 +00:00
parent f9268f8bd4
commit 641e40b35d
13 changed files with 485 additions and 37 deletions

View File

@ -9,23 +9,20 @@ import cc.fascinated.utils.Style;
import io.papermc.paper.event.player.AsyncChatEvent;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;
@RequiredArgsConstructor @Getter
enum BlockReason {
DOMAIN("Domain was detected in the message."),
DUPLICATE("Duplicate message was detected.");
DUPLICATE("Duplicate message was detected."),
IP("IP address was detected in the message.");
private final String reason;
}
@ -34,6 +31,7 @@ public class ChatManager extends Manager {
private final HashMap<Account, String> lastMessage = new HashMap<>();
private final Pattern domainPattern = Pattern.compile("^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,8}$");
private final Pattern ipPattern = Pattern.compile("\\b(?:\\d{1,3}\\.){1,3}\\d{1,3}\\b");
@EventHandler
public void onChat(AsyncChatEvent event) {
@ -48,10 +46,11 @@ public class ChatManager extends Manager {
// Check if the message contains a domain and is not the server's domain.
if (domainPattern.matcher(messageContent).find() && !messageContent.contains("aetheria.cc")) {
blockReason = BlockReason.DOMAIN;
MessageUtils.messageOps(Lang.BLOCKED_MESSAGE_ALERT.getAsString()
.replace("%player%", account.getName())
.replace("%message%", messageContent)
);
}
// Check if the message contains an IP address.
if (ipPattern.matcher(messageContent).find()) {
blockReason = BlockReason.IP;
}
// Block the message if it is the same as the last message sent.
@ -61,7 +60,16 @@ public class ChatManager extends Manager {
if (blockReason != null) {
event.setCancelled(true);
account.sendMessage(Lang.BLOCKED_MESSAGE.getAsString().replace("%reason%", blockReason.getReason()));
account.sendMessage(Lang.BLOCKED_MESSAGE.getAsString()
.replace("%reason%", blockReason.getReason())
);
if (blockReason != BlockReason.DUPLICATE) {
MessageUtils.messageOps(Lang.BLOCKED_MESSAGE_ALERT.getAsString()
.replace("%player%", account.getName())
.replace("%message%", messageContent)
);
}
return;
}