Extract channel prefix check to CarbonPlayer

This commit is contained in:
Draycia committed 2023-05-28 17:48:08 -07:00
1 parent d452106e0b
commit 76e3b68dfe
4 files changed
+45

No files matched your search

@@ -39,6 +39,8 @@ import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -365,6 +367,34 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
}
}
@Override
public ChannelMessage channelForMessage(final Component message) {
final String text = PlainTextComponentSerializer.plainText().serialize(message);
Component formattedMessage = message;
@Nullable ChatChannel channel = this.selectedChannel();
for (final ChatChannel chatChannel : this.carbonChat.channelRegistry()) {
final @MonotonicNonNull String prefix = chatChannel.quickPrefix();
if (prefix == null) {
continue;
}
if (text.startsWith(prefix) && chatChannel.speechPermitted(this).permitted()) {
channel = chatChannel;
formattedMessage = formattedMessage.replaceText(TextReplacementConfig.builder()
.once()
.matchLiteral(channel.quickPrefix())
.replacement(Component.empty())
.build());
break;
}
}
return new ChannelMessage(formattedMessage, channel);
}
@Override
public double distanceSquaredFrom(final CarbonPlayer other) {
return -1;
@@ -149,6 +149,11 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer {
return this.carbonPlayerCommon().locale();
}
@Override
public ChannelMessage channelForMessage(final Component message) {
return this.carbonPlayerCommon().channelForMessage(message);
}
@Override
public @Nullable ChatChannel selectedChannel() {
return this.carbonPlayerCommon().selectedChannel();