Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public SendOnLogin sendOnLogin() {
return this.sendOnLogin;
}

private Chat chat = new Chat();
public Chat chat() {
return this.chat;
}

private Commands commands = new Commands();
public Commands commands() {
return this.commands;
Expand Down Expand Up @@ -105,6 +110,21 @@ public SendMode sendMode() {
}
}

@ConfigSerializable
public static class Chat {
@Comment("Set up users who haven't signed in to send chat that start with the following")
private List<String> allowedChatPrefixes = List.of(".l ", ".reg ", ".email ");
public List<String> allowedChatPrefixes() {
return this.allowedChatPrefixes;
}

@Comment("Just blocked chat message tip")
private String blockedChatMessage = "<red>You cannot send this message if you are not logged in yet";
public String blockedChatMessage() {
return this.blockedChatMessage;
}
}

@ConfigSerializable
public static class Commands {
@Comment("Sets the commands that users who have not yet logged in can execute")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import com.velocitypowered.api.event.EventTask;
import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.player.PlayerChatEvent;
import com.velocitypowered.api.proxy.Player;
import io.github._4drian3d.authmevelocity.velocity.AuthMeVelocityPlugin;
import io.github._4drian3d.authmevelocity.velocity.listener.Listener;
import net.kyori.adventure.text.minimessage.MiniMessage;

public final class ChatListener implements Listener<PlayerChatEvent> {
@Inject
Expand All @@ -39,6 +41,7 @@ public void register() {
@Override
public EventTask executeAsync(final PlayerChatEvent event) {
return EventTask.withContinuation(continuation -> {
String message = event.getMessage();
if (plugin.isLogged(event.getPlayer())) {
plugin.logDebug(() -> "PlayerChatEvent | Player " + event.getPlayer().getUsername() + " is already logged");
continuation.resume();
Expand All @@ -47,8 +50,23 @@ public EventTask executeAsync(final PlayerChatEvent event) {

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

if (plugin.config().get().chat().allowedChatPrefixes().stream().anyMatch(message::startsWith)) {
plugin.logDebug(() -> "PlayerChatEvent | Message \"" + message + "\" is allowed by prefix rule.");
continuation.resume();
return;
}

plugin.logDebug(() -> "PlayerChatEvent | Message \"" + message + "\" is blocked.");
sendBlockedChatMessage(event.getPlayer());
event.setResult(PlayerChatEvent.ChatResult.denied());
continuation.resume();
});
}

private void sendBlockedChatMessage(final Player player){
final String blockedChatMessage = plugin.config().get().chat().blockedChatMessage();
if (!blockedChatMessage.isBlank()){
player.sendMessage(MiniMessage.miniMessage().deserialize(blockedChatMessage));
}
}
}