From efab5f9c732271e107472453a4946dc691b2f1c4 Mon Sep 17 00:00:00 2001 From: Josua Parks Date: Mon, 20 May 2024 02:29:24 -0700 Subject: [PATCH] Go to default channel when entering already-selected channel (#486) * Go to default channel when entering already-selected channel * Manually index config versions again --- .../channels/CarbonChannelRegistry.java | 8 +++-- .../carbon/common/config/PrimaryConfig.java | 31 ++++++++++--------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java b/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java index 48d5c9eb..0776eb1e 100644 --- a/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java +++ b/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java @@ -379,7 +379,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann final Command command = builder.senderType(Commander.class) .handler(handler -> { final Commander commander = handler.sender(); - final @Nullable ChatChannel chatChannel = this.channel(channelKey); + @Nullable ChatChannel chatChannel = this.channel(channelKey); if (!(commander instanceof PlayerCommander playerCommander)) { if (chatChannel != null && handler.contains("message")) { @@ -408,11 +408,15 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann // TODO: trigger platform events related to chat this.sendMessageInChannel(player, chatChannel, message); } else { + if (this.config.primaryConfig().returnToDefaultChannel() && player.selectedChannel().key().equals(channelKey)) { + chatChannel = this.defaultChannel(); + } + final ChannelSwitchEvent switchEvent = new ChannelSwitchEventImpl(player, chatChannel); this.eventHandler.emit(switchEvent); player.selectedChannel(switchEvent.channel()); - this.carbonMessages.changedChannels(player, channelKey.value()); + this.carbonMessages.changedChannels(player, chatChannel.key().value()); } }) .build(); diff --git a/common/src/main/java/net/draycia/carbon/common/config/PrimaryConfig.java b/common/src/main/java/net/draycia/carbon/common/config/PrimaryConfig.java index 193b3a0d..0e401676 100644 --- a/common/src/main/java/net/draycia/carbon/common/config/PrimaryConfig.java +++ b/common/src/main/java/net/draycia/carbon/common/config/PrimaryConfig.java @@ -47,6 +47,9 @@ public class PrimaryConfig { If the channel is not found or the player cannot use the channel, they will speak in basic non-channel chat.""") private Key defaultChannel = Key.key("carbon", "global"); + @Comment("Returns you to the default channel when you use a channel's command while you have that channel active.") + private boolean returnToDefaultChannel = false; + @Comment(""" The service that will be used to store and load player information. One of: JSON, H2, MYSQL, PSQL @@ -114,6 +117,10 @@ public class PrimaryConfig { return this.defaultChannel; } + public boolean returnToDefaultChannel() { + return this.returnToDefaultChannel; + } + public StorageType storageType() { return this.storageType; } @@ -187,20 +194,10 @@ public class PrimaryConfig { final ConfigurationTransformation.VersionedBuilder builder = ConfigurationTransformation.versionedBuilder() .versionKey(ConfigManager.CONFIG_VERSION_KEY); - final ConfigurationTransformation initial = ConfigurationTransformation.builder() - .addAction(NodePath.path("use-carbon-nicknames"), (path, value) -> new Object[]{"nickname-settings", "use-carbon-nicknames"}) - .build(); - builder.addVersion(0, initial); - - final ConfigurationTransformation one = ConfigurationTransformation.builder() - .addAction(NodePath.path("party-chat"), (path, value) -> new Object[]{"party-chat", "enabled"}) - .build(); - builder.addVersion(1, one); - - final ConfigurationTransformation two = ConfigurationTransformation.builder() - .addAction(NodePath.path("nickname-settings"), (path, value) -> new Object[]{"filter", ".*"}) - .build(); - builder.addVersion(2, two); + builder.addVersion(0, insertAddition("use-carbon-nicknames", "nickname-settings", "use-carbon-nicknames")); + builder.addVersion(1, insertAddition("party-chat", "party-chat", "enabled")); + builder.addVersion(2, insertAddition("nickname-settings", "filter", ".*")); + builder.addVersion(3, insertAddition("return-to-default-channel", "return-to-default-channel", false)); final ConfigurationTransformation.Versioned upgrader = builder.build(); final int from = upgrader.version(node); @@ -213,6 +210,12 @@ public class PrimaryConfig { ConfigManager.configVersionComment(node, upgrader); } + private static ConfigurationTransformation insertAddition(final String path, final Object key, final Object value) { + return ConfigurationTransformation.builder() + .addAction(NodePath.path(path), ($, $$) -> new Object[]{key, value}) + .build(); + } + @ConfigSerializable public static final class NicknameSettings {