Skip to content

Commit 5f35aeb

Browse files
committed
fix: Apply review suggestions
1 parent eea6ff5 commit 5f35aeb

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

common/src/main/java/io/github/_4drian3d/authmevelocity/common/configuration/ProxyConfiguration.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,20 @@ public SendMode sendMode() {
112112

113113
@ConfigSerializable
114114
public static class Chat {
115-
@Comment("Set up users who haven't signed in to send chat that start with the following")
115+
116+
@Comment("Enable chat filter")
117+
private boolean enableAllowedChatPrefixes = false;
118+
public boolean enableAllowedChatPrefixes() {
119+
return this.enableAllowedChatPrefixes;
120+
}
121+
122+
@Comment("Sets which messages players can send before they have logged in")
116123
private List<String> allowedChatPrefixes = List.of(".l ", ".reg ", ".email ");
117124
public List<String> allowedChatPrefixes() {
118125
return this.allowedChatPrefixes;
119126
}
120127

121-
@Comment("Just blocked chat message tip")
128+
@Comment("Sets the message to send to players who use messages that are not allowed. \\nLeave this option blank if you do not want the message to be sent.")
122129
private String blockedChatMessage = "<red>You cannot send this message if you are not logged in yet";
123130
public String blockedChatMessage() {
124131
return this.blockedChatMessage;

velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/input/ChatListener.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import io.github._4drian3d.authmevelocity.velocity.listener.Listener;
2828
import net.kyori.adventure.text.minimessage.MiniMessage;
2929

30+
import java.util.List;
31+
3032
public final class ChatListener implements Listener<PlayerChatEvent> {
3133
@Inject
3234
private AuthMeVelocityPlugin plugin;
@@ -41,7 +43,6 @@ public void register() {
4143
@Override
4244
public EventTask executeAsync(final PlayerChatEvent event) {
4345
return EventTask.withContinuation(continuation -> {
44-
String message = event.getMessage();
4546
if (plugin.isLogged(event.getPlayer())) {
4647
plugin.logDebug(() -> "PlayerChatEvent | Player " + event.getPlayer().getUsername() + " is already logged");
4748
continuation.resume();
@@ -50,23 +51,26 @@ public EventTask executeAsync(final PlayerChatEvent event) {
5051

5152
plugin.logDebug(() -> "PlayerChatEvent | Player " + event.getPlayer().getUsername() + " is not logged");
5253

53-
if (plugin.config().get().chat().allowedChatPrefixes().stream().anyMatch(message::startsWith)) {
54-
plugin.logDebug(() -> "PlayerChatEvent | Message \"" + message + "\" is allowed by prefix rule.");
54+
if (plugin.config().get().chat().enableAllowedChatPrefixes()
55+
&& !plugin.config().get().chat().allowedChatPrefixes().isEmpty()
56+
&& isMessageAllowed(event.getMessage(), plugin.config().get().chat().allowedChatPrefixes())) {
57+
plugin.logDebug(() -> "PlayerChatEvent | Allowed message: " + event.getMessage());
5558
continuation.resume();
5659
return;
5760
}
5861

59-
plugin.logDebug(() -> "PlayerChatEvent | Message \"" + message + "\" is blocked.");
60-
sendBlockedChatMessage(event.getPlayer());
62+
plugin.logDebug(() -> "PlayerChatEvent | Blocked message: " + event.getMessage());
6163
event.setResult(PlayerChatEvent.ChatResult.denied());
6264
continuation.resume();
6365
});
6466
}
6567

66-
private void sendBlockedChatMessage(final Player player){
67-
final String blockedChatMessage = plugin.config().get().chat().blockedChatMessage();
68-
if (!blockedChatMessage.isBlank()){
69-
player.sendMessage(MiniMessage.miniMessage().deserialize(blockedChatMessage));
68+
private boolean isMessageAllowed(String message, List<String> allowedPrefixes) {
69+
for (String prefix : allowedPrefixes) {
70+
if (message.startsWith(prefix)) {
71+
return true;
72+
}
7073
}
74+
return false;
7175
}
7276
}

0 commit comments

Comments
 (0)