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

@@ -154,6 +154,10 @@ public interface CarbonPlayer extends Audience, Identified {
*/ */
void selectedChannel(final @Nullable ChatChannel chatChannel); void selectedChannel(final @Nullable ChatChannel chatChannel);
ChannelMessage channelForMessage(final Component message);
record ChannelMessage(Component message, @Nullable ChatChannel channel) {}
/** /**
* Checks if the player has the specified permission. * Checks if the player has the specified permission.
* *
@@ -355,4 +359,5 @@ public interface CarbonPlayer extends Audience, Identified {
* @since 2.1.0 * @since 2.1.0
*/ */
void leaveChannel(final ChatChannel channel); void leaveChannel(final ChatChannel channel);
} }
@@ -90,6 +90,11 @@ class EmptyCarbonPlayer implements CarbonPlayer {
} }
@Override
public ChannelMessage channelForMessage(final Component message) {
return new ChannelMessage(message, null);
}
@Override @Override
public boolean hasPermission(final String permission) { public boolean hasPermission(final String permission) {
return false; return false;
@@ -39,6 +39,8 @@ import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.identity.Identity; import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component; 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.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; 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 @Override
public double distanceSquaredFrom(final CarbonPlayer other) { public double distanceSquaredFrom(final CarbonPlayer other) {
return -1; return -1;
@@ -149,6 +149,11 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer {
return this.carbonPlayerCommon().locale(); return this.carbonPlayerCommon().locale();
} }
@Override
public ChannelMessage channelForMessage(final Component message) {
return this.carbonPlayerCommon().channelForMessage(message);
}
@Override @Override
public @Nullable ChatChannel selectedChannel() { public @Nullable ChatChannel selectedChannel() {
return this.carbonPlayerCommon().selectedChannel(); return this.carbonPlayerCommon().selectedChannel();