Add channel 'name' option, don't register channels with invalid keys/names

Also hides the key option by default
This commit is contained in:
Draycia committed 2023-07-16 20:58:24 -07:00
1 parent dcd1e0cc90
commit 9daac8eff3
2 files changed
+45 -10

No files matched your search

@@ -56,6 +56,7 @@ import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.util.Exceptions;
import net.draycia.carbon.common.util.FileUtil;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.key.InvalidKeyException;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.apache.logging.log4j.Logger;
@@ -224,10 +225,21 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
continue;
}
final @Nullable ChatChannel chatChannel = this.loadChannel(channelConfigFile);
final @Nullable ChatChannel chatChannel;
try {
chatChannel = this.loadChannel(channelConfigFile);
} catch (final ConfigurateException configurateException) {
if (this.findThrowableRootCause(configurateException) instanceof InvalidKeyException keyException) {
this.logger.error("Invalid channel key found: " + keyException.getMessage());
}
continue;
}
if (chatChannel == null) {
continue;
}
final Key channelKey = chatChannel.key();
if (this.defaultKey.equals(channelKey)) {
this.logger.info("Default channel is [" + channelKey + "]");
@@ -253,6 +265,15 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
this.logger.info("Registered channels: [" + channels + "]");
}
private Throwable findThrowableRootCause(Throwable throwable) {
Objects.requireNonNull(throwable);
Throwable rootCause = throwable;
while (rootCause.getCause() != null && rootCause.getCause() != rootCause) {
rootCause = rootCause.getCause();
}
return rootCause;
}
private void saveDefaultChannelConfig() {
try {
final Path configFile = this.configChannelDir.resolve("global.conf");
@@ -266,18 +287,17 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
}
}
private @Nullable ChatChannel loadChannel(final Path channelFile) {
private @Nullable ChatChannel loadChannel(final Path channelFile) throws ConfigurateException {
final ConfigurationLoader<?> loader = this.configFactory.configurationLoader(channelFile);
try {
final ConfigurationNode loaded = updateNode(loader.load());
loader.save(loaded);
return MAPPER.load(loaded);
} catch (final ConfigurateException exception) {
this.logger.warn("Failed to load channel from file '{}'", channelFile, exception);
} catch (final ConfigurateException configurateException) {
this.logger.warn("Failed to load channel from file '{}'", channelFile);
throw configurateException;
}
return null;
}
private void sendMessageInChannelAsPlayer(
@@ -43,6 +43,7 @@ import net.draycia.carbon.common.messages.placeholders.StringPlaceholderResolver
import net.draycia.carbon.common.messages.placeholders.UUIDPlaceholderResolver;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.KeyPattern;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.Moonshine;
import net.kyori.moonshine.exception.scan.UnscannableMethodException;
@@ -67,12 +68,21 @@ public final class ConfigChatChannel implements ChatChannel {
private transient @MonotonicNonNull @Inject CarbonChat carbonChat;
@Comment("""
The channel's name.
This is also what's used for channel commands.
The 'key' option overrides this.
Must follow the format of [a-z0-9/.-_]
""")
@KeyPattern
private String name = "global";
@Comment("""
The channel's key, used to track the channel.
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.
""")
private @Nullable Key key = Key.key("carbon", "global");
private @Nullable Key key = null;
@Comment("""
The permission required to use the /channel <channelname> and /<channelname> commands.
@@ -92,6 +102,7 @@ public final class ConfigChatChannel implements ChatChannel {
private @Nullable Boolean shouldRegisterCommands = true;
@Deprecated(since = "2.0", forRemoval = true)
private @Nullable String commandName = null;
private @Nullable List<String> commandAliases = Collections.emptyList();
@@ -128,7 +139,7 @@ public final class ConfigChatChannel implements ChatChannel {
@Override
public String commandName() {
return Objects.requireNonNullElse(this.commandName, this.key.value());
return Objects.requireNonNullElse(this.commandName, this.key().value());
}
@Override
@@ -161,7 +172,7 @@ public final class ConfigChatChannel implements ChatChannel {
@Override
public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) {
return ChannelPermissionResult.allowedIf(empty(), () -> player.hasPermission(this.permission() + ".see") && !player.leftChannels().contains(this.key));
return ChannelPermissionResult.allowedIf(empty(), () -> player.hasPermission(this.permission() + ".see") && !player.leftChannels().contains(this.key()));
}
@Override
@@ -193,7 +204,11 @@ public final class ConfigChatChannel implements ChatChannel {
@Override
public @NonNull Key key() {
return Objects.requireNonNull(this.key);
if (this.key != null) {
return this.key;
}
return Key.key("carbon", this.name);
}
public String messageFormat(final CarbonPlayer sender) {