Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go to default channel when entering already-selected channel #486

Merged
merged 3 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (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();
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 @@ -187,20 +194,10 @@ 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);

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);
Expand All @@ -213,6 +210,12 @@ public static void upgrade(final ConfigurationNode node) {
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 {

Expand Down