Skip to content

Commit

Permalink
Go to default channel when entering already-selected channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Draycia committed May 20, 2024
1 parent af12f92 commit cc3a165
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private void registerChannelCommands(final ChatChannel channel) {
final Command<Commander> 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")) {
Expand Down Expand Up @@ -408,11 +408,15 @@ private void registerChannelCommands(final ChatChannel channel) {
// TODO: trigger platform events related to chat
this.sendMessageInChannel(player, chatChannel, message);
} else {
if (config.primaryConfig().returnToDefaultChannel() && player.selectedChannel().key().equals(channelKey)) {
chatChannel = 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -114,6 +117,10 @@ public Key defaultChannel() {
return this.defaultChannel;
}

public boolean returnToDefaultChannel() {
return this.returnToDefaultChannel;
}

public StorageType storageType() {
return this.storageType;
}
Expand Down Expand Up @@ -186,14 +193,14 @@ public boolean updateChecker() {
public static void upgrade(final ConfigurationNode node) {
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);

insertAddition(builder, "use-carbon-nicknames", "nickname-settings", "use-carbon-nicknames");
// This looks confusing, wouldn't this add "party-chat.party-chat = enabled"?
// Should produce "party-chat.enabled = true"
insertAddition(builder, "party-chat", "party-chat", "enabled");
// TODO: make sure this is working
insertAddition(builder, "return-to-default-channel", "return-to-default-channel", false);

final ConfigurationTransformation.Versioned upgrader = builder.build();
final int from = upgrader.version(node);
try {
Expand All @@ -205,6 +212,20 @@ public static void upgrade(final ConfigurationNode node) {
ConfigManager.configVersionComment(node, upgrader);
}

private static int upgradeIndex = 0;

private static void addUpgradeVersion(final ConfigurationTransformation.VersionedBuilder builder, final ConfigurationTransformation transformation) {
builder.addVersion(upgradeIndex++, transformation);
}

private static void insertAddition(final ConfigurationTransformation.VersionedBuilder builder, final String path, final Object key, final Object value) {
final ConfigurationTransformation transformation = ConfigurationTransformation.builder()
.addAction(NodePath.path(path), ($, $$) -> new Object[]{key, value})
.build();

addUpgradeVersion(builder, transformation);
}

@ConfigSerializable
public static final class NicknameSettings {

Expand Down

0 comments on commit cc3a165

Please sign in to comment.