Fix channel prefixes not working

This commit is contained in:
Draycia committed 2023-06-08 20:29:19 -07:00
1 parent 068ba26cfc
commit 14b229152e
4 files changed
+45 -45

No files matched your search

@@ -53,7 +53,6 @@ import net.kyori.moonshine.Moonshine;
import net.kyori.moonshine.exception.scan.UnscannableMethodException; import net.kyori.moonshine.exception.scan.UnscannableMethodException;
import net.kyori.moonshine.strategy.StandardPlaceholderResolverStrategy; import net.kyori.moonshine.strategy.StandardPlaceholderResolverStrategy;
import net.kyori.moonshine.strategy.supertype.StandardSupertypeThenInterfaceSupertypeStrategy; import net.kyori.moonshine.strategy.supertype.StandardSupertypeThenInterfaceSupertypeStrategy;
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;
import org.checkerframework.framework.qual.DefaultQualifier; import org.checkerframework.framework.qual.DefaultQualifier;
@@ -75,7 +74,7 @@ public final class ConfigChatChannel implements ChatChannel {
You only need to change the second part of the key. "global" by default. You only need to change the second part of the key. "global" by default.
The value is what's used in commands, this is probably what you want to change. The value is what's used in commands, this is probably what you want to change.
""") """)
private final @Nullable Key key = Key.key("carbon", "global"); private @Nullable Key key = Key.key("carbon", "global");
@Comment(""" @Comment("""
The permission required to use the /channel <channelname> and /<channelname> commands. The permission required to use the /channel <channelname> and /<channelname> commands.
@@ -84,23 +83,20 @@ public final class ConfigChatChannel implements ChatChannel {
To read messages you must have the permission carbon.channel.global.see To read messages you must have the permission carbon.channel.global.see
To send messages you must have the permission carbon.channel.global.speak To send messages you must have the permission carbon.channel.global.speak
""") """)
private final @Nullable String permission = "carbon.channel.global"; private @Nullable String permission = null;
@Setting("format") @Setting("format")
@Comment("The chat formats for this channel.") @Comment("The chat formats for this channel.")
private final @Nullable ConfigChannelMessageSource messageSource = new ConfigChannelMessageSource(); private @Nullable ConfigChannelMessageSource messageSource = new ConfigChannelMessageSource();
@Comment("Messages will be sent in this channel if they start with this prefix.") @Comment("Messages will be sent in this channel if they start with this prefix.")
private final @Nullable String quickPrefix = ""; private @Nullable String quickPrefix = null;
private final @Nullable private @Nullable Boolean shouldRegisterCommands = true;
Boolean shouldRegisterCommands = true;
private final @Nullable private @Nullable String commandName = null;
String commandName = null;
private final @Nullable private @Nullable List<String> commandAliases = Collections.emptyList();
List<String> commandAliases = Collections.emptyList();
private transient @Nullable ConfigChannelMessages carbonMessages = null; private transient @Nullable ConfigChannelMessages carbonMessages = null;
@@ -109,15 +105,13 @@ public final class ConfigChatChannel implements ChatChannel {
A value of '0' requires that both players are in the same world. A value of '0' requires that both players are in the same world.
On velocity, '0' requires that both players are in the same server. On velocity, '0' requires that both players are in the same server.
""") """)
private final int radius = -1; private int radius = -1;
@Comment(""" @Comment("""
If true, players will be able to see if they're not sending messages to anyone If true, players will be able to see if they're not sending messages to anyone
because they're out of range from the radius. because they're out of range from the radius.
""") """)
private final boolean emptyRadiusRecipientsMessage = true; private boolean emptyRadiusRecipientsMessage = true;
private final transient @Nullable ConfigChannelMessages messageService = null;
@Override @Override
public @Nullable String quickPrefix() { public @Nullable String quickPrefix() {
@@ -296,7 +290,11 @@ public final class ConfigChatChannel implements ChatChannel {
} }
@Override @Override
public @MonotonicNonNull String permission() { public String permission() {
if (this.permission == null) {
return "carbon.channel." + this.key().value();
}
return this.permission; return this.permission;
} }
@@ -39,8 +39,6 @@ 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;
@@ -179,7 +177,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override @Override
public boolean hasPermission(final String permission) { public boolean hasPermission(final String permission) {
return false; return this.carbonPlayer().hasPermission(permission);
} }
@Override @Override
@@ -369,30 +367,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override @Override
public ChannelMessage channelForMessage(final Component message) { public ChannelMessage channelForMessage(final Component message) {
final String text = PlainTextComponentSerializer.plainText().serialize(message); return new ChannelMessage(message, null);
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
@@ -23,14 +23,18 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.UUID; import java.util.UUID;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.InventorySlot; import net.draycia.carbon.api.util.InventorySlot;
import net.kyori.adventure.identity.Identity; import net.kyori.adventure.identity.Identity;
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 net.luckperms.api.LuckPermsProvider; import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.model.user.User; import net.luckperms.api.model.user.User;
import net.luckperms.api.util.Tristate; import net.luckperms.api.util.Tristate;
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;
import org.checkerframework.framework.qual.DefaultQualifier; import org.checkerframework.framework.qual.DefaultQualifier;
@@ -151,7 +155,30 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer {
@Override @Override
public ChannelMessage channelForMessage(final Component message) { public ChannelMessage channelForMessage(final Component message) {
return this.carbonPlayerCommon().channelForMessage(message); final String text = PlainTextComponentSerializer.plainText().serialize(message);
Component formattedMessage = message;
@Nullable ChatChannel channel = this.selectedChannel();
for (final ChatChannel chatChannel : CarbonChatProvider.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
@@ -62,7 +62,7 @@ public class DSRVChatHook implements ChatHook {
} }
final String messageContents = PlainTextComponentSerializer.plainText().serialize(messageComponent); final String messageContents = PlainTextComponentSerializer.plainText().serialize(messageComponent);
Component parsedMessage = ConfigChatChannel.parseMessageTags(carbonPlayer, messageContents); final Component parsedMessage = ConfigChatChannel.parseMessageTags(carbonPlayer, messageContents);
DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "Received a CarbonChatEvent (player: " + carbonPlayer.username() + ")"); DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "Received a CarbonChatEvent (player: " + carbonPlayer.username() + ")");