add blocked reason to the message when it gets blocked
This commit is contained in:
@ -7,6 +7,8 @@ import cc.fascinated.utils.Manager;
|
||||
import cc.fascinated.utils.MessageUtils;
|
||||
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;
|
||||
@ -20,6 +22,14 @@ 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.");
|
||||
|
||||
private final String reason;
|
||||
}
|
||||
|
||||
public class ChatManager extends Manager {
|
||||
|
||||
private final HashMap<Account, String> lastMessage = new HashMap<>();
|
||||
@ -30,15 +40,14 @@ public class ChatManager extends Manager {
|
||||
Player player = event.getPlayer();
|
||||
Account account = AccountManager.getAccount(player.getUniqueId());
|
||||
NamedTextColor color = account.getPlayerColorProfile().getColor();
|
||||
|
||||
boolean blockedMessage = false;
|
||||
BlockReason blockReason = null;
|
||||
|
||||
TextComponent textComponent = (TextComponent) event.message();
|
||||
String messageContent = textComponent.content();
|
||||
|
||||
// Check if the message contains a domain and is not the server's domain.
|
||||
if (domainPattern.matcher(messageContent).find() && !messageContent.contains("aetheria.cc")) {
|
||||
blockedMessage = true;
|
||||
blockReason = BlockReason.DOMAIN;
|
||||
MessageUtils.messageOps(Lang.BLOCKED_MESSAGE_ALERT.getAsString()
|
||||
.replace("%player%", account.getName())
|
||||
.replace("%message%", messageContent)
|
||||
@ -47,12 +56,12 @@ public class ChatManager extends Manager {
|
||||
|
||||
// Block the message if it is the same as the last message sent.
|
||||
if (lastMessage.containsKey(account) && lastMessage.get(account).equalsIgnoreCase(messageContent)) {
|
||||
blockedMessage = true;
|
||||
blockReason = BlockReason.DUPLICATE;
|
||||
}
|
||||
|
||||
if (blockedMessage) {
|
||||
if (blockReason != null) {
|
||||
event.setCancelled(true);
|
||||
account.sendMessage(Lang.BLOCKED_MESSAGE.getAsString());
|
||||
account.sendMessage(Lang.BLOCKED_MESSAGE.getAsString().replace("%reason%", blockReason.getReason()));
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@ prefix: "<gold><bold>AETHERIA</bold></gold> <gray>»</gray> <white>"
|
||||
admin-prefix: "<grey>[</grey><bold><red>ADMIN</red></bold><grey>]</grey> <white>"
|
||||
|
||||
chat-format: "<<%chatcolor%>%name%</%chatcolor%>> <hover:show_text:Click to message %name%><click:suggest_command:/msg %name% ><white>%message%"
|
||||
blocked-message: "<prefix><red>Your message has been blocked."
|
||||
blocked-message: "<prefix><hover:show_text:%reason%><red>Your message has been blocked."
|
||||
blocked-message-alert: "<admin-prefix><red>%player% <grey>tried to send a blocked message: <white>%message%"
|
||||
|
||||
help-command:
|
||||
|
Reference in New Issue
Block a user