Skip to content

Commit

Permalink
Add channel 'name' option, don't register channels with invalid keys/…
Browse files Browse the repository at this point in the history
…names

Also hides the key option by default
  • Loading branch information
Draycia committed Jul 17, 2023
1 parent dcd1e0c commit 9daac8e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
Expand Up @@ -56,6 +56,7 @@
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;
Expand Down Expand Up @@ -224,10 +225,21 @@ private void loadConfigChannels_(final CarbonMessages messages) {
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 + "]");
Expand All @@ -253,6 +265,15 @@ private void loadConfigChannels_(final CarbonMessages messages) {
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");
Expand All @@ -266,18 +287,17 @@ private void saveDefaultChannelConfig() {
}
}

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(
Expand Down
Expand Up @@ -43,6 +43,7 @@
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;
Expand All @@ -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.
Expand All @@ -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();
Expand Down Expand Up @@ -128,7 +139,7 @@ public boolean shouldRegisterCommands() {

@Override
public String commandName() {
return Objects.requireNonNullElse(this.commandName, this.key.value());
return Objects.requireNonNullElse(this.commandName, this.key().value());
}

@Override
Expand Down Expand Up @@ -161,7 +172,7 @@ public ChannelPermissionResult speechPermitted(final CarbonPlayer carbonPlayer)

@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
Expand Down Expand Up @@ -193,7 +204,11 @@ public Set<CarbonPlayer> filterRecipients(final CarbonPlayer sender, final Set<C

@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) {
Expand Down

0 comments on commit 9daac8e

Please sign in to comment.