Rework channel registration

Addons should now register any new channels as early as possible to allow for command registration.

Removing channels requires a plugin restart.
This commit is contained in:
Jason Penilla authored and Jason committed 2023-07-12 18:48:43 -07:00
1 parent 2f1023d9f0
commit 88402ed0a6
4 files changed
+98 -68

No files matched your search

@@ -20,6 +20,7 @@
package net.draycia.carbon.api.channels; package net.draycia.carbon.api.channels;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
@@ -32,15 +33,9 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public interface ChannelRegistry { public interface ChannelRegistry {
/** /**
* Registers ingame commands for the channel. * Registers the chat channel with its key.
* *
* @param channel the channel to register commands for * <p>Registrations will persist when reloading Carbon's configuration.</p>
* @since 2.1.0
*/
void registerChannelCommands(final ChatChannel channel);
/**
* Registers the chat channel with it's key.
* *
* @param channel the channel to register * @param channel the channel to register
* @since 2.1.0 * @since 2.1.0
@@ -89,4 +84,15 @@ public interface ChannelRegistry {
*/ */
ChatChannel keyOrDefault(final Key key); ChatChannel keyOrDefault(final Key key);
/**
* The provided action will be executed immediately for all currently registered
* channels.
*
* <p>When new channels are registered, the action will be invoked again for each new channel.</p>
*
* @param action action
* @since 2.1.0
*/
void allKeys(Consumer<Key> action);
} }
@@ -20,14 +20,19 @@
package net.draycia.carbon.api.event.events; package net.draycia.carbon.api.event.events;
import java.util.Set; import java.util.Set;
import net.draycia.carbon.api.channels.ChatChannel; import java.util.function.Consumer;
import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.event.CarbonEvent; import net.draycia.carbon.api.event.CarbonEvent;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier; import org.checkerframework.framework.qual.DefaultQualifier;
/** /**
* {@link CarbonEvent} that's called when channels are registered. * {@link CarbonEvent} that's called after new channels are registered.
*
* <p>Note that some invocations of this event may be too early for
* API consumers to be notified. Prefer using {@link ChannelRegistry#allKeys(Consumer)}
* when knowledge of all registered channels is needed.</p>
* *
* @since 2.1.0 * @since 2.1.0
*/ */
@@ -35,21 +40,19 @@ import org.checkerframework.framework.qual.DefaultQualifier;
public interface CarbonChannelRegisterEvent extends CarbonEvent { public interface CarbonChannelRegisterEvent extends CarbonEvent {
/** /**
* The channels that were registered. * Gets the channel registry.
* *
* @return the registered channels * @return the channel registry
* @since 2.1.0 * @since 2.1.0
*/ */
Set<Key> channelKeys(); ChannelRegistry channelRegistry();
/** /**
* Registers additional channels to the registry. Does not re-emit the event. * Gets the key(s) that were registered to trigger this event.
* *
* @param key The key the channel is identified with in the registry * @return key(s) registered
* @param channel the channel to register
* @param registerCommands if commands for the channel should be registered
* @since 2.1.0 * @since 2.1.0
*/ */
void register(final Key key, final ChatChannel channel, final boolean registerCommands); Set<Key> registered();
} }
@@ -32,14 +32,16 @@ import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer;
import net.draycia.carbon.api.channels.ChannelRegistry; import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.event.CarbonEventHandler; import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.event.events.CarbonChannelRegisterEvent;
import net.draycia.carbon.api.event.events.CarbonChatEvent; import net.draycia.carbon.api.event.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.KeyedRenderer; import net.draycia.carbon.api.util.KeyedRenderer;
@@ -93,7 +95,6 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
private volatile Registry<Key, ChatChannel> channelRegistry = Registry.create(); private volatile Registry<Key, ChatChannel> channelRegistry = Registry.create();
private final Set<Key> configChannels = ConcurrentHashMap.newKeySet(); private final Set<Key> configChannels = ConcurrentHashMap.newKeySet();
private final AtomicBoolean firstRegister = new AtomicBoolean(true);
// //
// private final BiMap<Key, ChatChannel> channelMap = Maps.synchronizedBiMap(HashBiMap.create()); // private final BiMap<Key, ChatChannel> channelMap = Maps.synchronizedBiMap(HashBiMap.create());
@@ -163,20 +164,52 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
public void reloadConfigChannels() { public void reloadConfigChannels() {
final Registry<Key, ChatChannel> newRegistry = Registry.create(); final Registry<Key, ChatChannel> newRegistry = Registry.create();
// Copy API registrations over // Copy API registrations over
for (final Key registered : this.channelRegistry.keys()) { for (final Key registered : this.channelRegistry.keys()) {
if (!this.configChannels.contains(registered)) { if (!this.configChannels.contains(registered)) {
newRegistry.register(registered, this.channelRegistry.getHolder(registered).valueOrThrow()); newRegistry.register(registered, this.channelRegistry.getHolder(registered).valueOrThrow());
} }
} }
final Set<Key> oldConfigChannels = Set.copyOf(this.configChannels);
this.configChannels.clear(); this.configChannels.clear();
final Registry<Key, ChatChannel> oldRegistry = this.channelRegistry;
this.channelRegistry = newRegistry; this.channelRegistry = newRegistry;
this.loadConfigChannels(this.carbonMessages);
this.loadConfigChannels_(this.carbonMessages);
// Re-add any deleted channels; users must restart for them to be removed
// (don't want to leave behind commands that just error, or confuse addons)
for (final Key old : oldConfigChannels) {
if (!this.configChannels.contains(old)) {
this.configChannels.add(old);
this.channelRegistry.register(old, oldRegistry.getHolder(old).valueOrThrow());
this.logger.warn("The config file for channel [{}] was deleted, but removing " +
"channels at runtime is not currently supported. You must restart the plugin " +
"for the removal to take effect.", old);
}
}
// Determine new channels and fire event if needed
final Set<Key> newConfigChannels = new HashSet<>();
for (final Key configChannel : this.configChannels) {
if (!oldConfigChannels.contains(configChannel)) {
newConfigChannels.add(configChannel);
}
}
if (!newConfigChannels.isEmpty()) {
this.eventHandler.emit(new ChannelRegisterEventImpl(this, Set.copyOf(newConfigChannels)));
}
} }
public void loadConfigChannels(final CarbonMessages messages) { public void loadConfigChannels(final CarbonMessages messages) {
final boolean firstRegister = this.firstRegister.compareAndSet(true, false); this.loadConfigChannels_(messages);
this.eventHandler.emit(new ChannelRegisterEventImpl(this, Set.copyOf(this.configChannels)));
}
private void loadConfigChannels_(final CarbonMessages messages) {
this.defaultKey = this.configFactory.primaryConfig().defaultChannel(); this.defaultKey = this.configFactory.primaryConfig().defaultChannel();
List<Path> channelConfigs = FileUtil.listDirectoryEntries(this.configChannelDir, "*.conf"); List<Path> channelConfigs = FileUtil.listDirectoryEntries(this.configChannelDir, "*.conf");
@@ -202,12 +235,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
this.injector.injectMembers(chatChannel); this.injector.injectMembers(chatChannel);
this.configChannels.add(chatChannel.key()); this.configChannels.add(chatChannel.key());
this.register(chatChannel); this.register(chatChannel, false);
if (firstRegister // We only support commands being registered at startup currently
&& chatChannel.shouldRegisterCommands()) {
this.registerChannelCommands(chatChannel);
}
} }
if (this.channel(this.defaultKey) == null) { if (this.channel(this.defaultKey) == null) {
@@ -223,8 +251,6 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
final String channels = String.join(", ", channelList); final String channels = String.join(", ", channelList);
this.logger.info("Registered channels: [" + channels + "]"); this.logger.info("Registered channels: [" + channels + "]");
this.eventHandler.emit(new ChannelRegisterEventImpl(this.keys(), this));
} }
private void saveDefaultChannelConfig() { private void saveDefaultChannelConfig() {
@@ -276,10 +302,12 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
} }
} }
@Override private void registerChannelCommands(final ChatChannel channel) {
public void registerChannelCommands(final ChatChannel channel) {
final CommandManager<Commander> commandManager = final CommandManager<Commander> commandManager =
this.injector.getInstance(com.google.inject.Key.get(new TypeLiteral<CommandManager<Commander>>() {})); this.injector.getInstance(com.google.inject.Key.get(new TypeLiteral<CommandManager<Commander>>() {}));
if (!commandManager.isCommandRegistrationAllowed() || commandManager.commandTree().getNamedNode(channel.commandName()) != null) {
return;
}
Command.Builder<Commander> builder = commandManager.commandBuilder(channel.commandName(), Command.Builder<Commander> builder = commandManager.commandBuilder(channel.commandName(),
channel.commandAliases(), commandManager.createDefaultCommandMeta()) channel.commandAliases(), commandManager.createDefaultCommandMeta())
@@ -335,7 +363,17 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
@Override @Override
public void register(final ChatChannel channel) { public void register(final ChatChannel channel) {
this.register(channel, true);
}
public void register(final ChatChannel channel, final boolean fireRegisterEvent) {
this.channelRegistry.register(channel.key(), channel); this.channelRegistry.register(channel.key(), channel);
if (channel.shouldRegisterCommands()) {
this.registerChannelCommands(channel);
}
if (fireRegisterEvent) {
this.eventHandler.emit(new ChannelRegisterEventImpl(this, Set.of(channel.key())));
}
} }
@Override @Override
@@ -383,4 +421,19 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
return this.defaultChannel(); return this.defaultChannel();
} }
@Override
public void allKeys(final Consumer<Key> action) {
for (final Key key : this.channelRegistry.keys()) {
action.accept(key);
}
this.eventHandler.subscribe(
CarbonChannelRegisterEvent.class,
event -> {
for (final Key key : event.registered()) {
action.accept(key);
}
}
);
}
} }
@@ -20,48 +20,16 @@
package net.draycia.carbon.common.event.events; package net.draycia.carbon.common.event.events;
import java.util.Set; import java.util.Set;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.event.CarbonEvent;
import net.draycia.carbon.api.event.events.CarbonChannelRegisterEvent; import net.draycia.carbon.api.event.events.CarbonChannelRegisterEvent;
import net.draycia.carbon.common.channels.CarbonChannelRegistry; import net.draycia.carbon.common.channels.CarbonChannelRegistry;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier; import org.checkerframework.framework.qual.DefaultQualifier;
/**
* {@link CarbonEvent} that's called when channels are registered.
*
* @since 2.1.0
*/
@DefaultQualifier(NonNull.class) @DefaultQualifier(NonNull.class)
public class ChannelRegisterEventImpl implements CarbonChannelRegisterEvent { public record ChannelRegisterEventImpl(
CarbonChannelRegistry channelRegistry,
private final Set<Key> channelKeys; Set<Key> registered
private final CarbonChannelRegistry registry; ) implements CarbonChannelRegisterEvent {
/**
* {@link CarbonEvent} that's called when channels are registered.
*
* @param channelKeys the channels that were registered
* @since 2.1.0
*/
public ChannelRegisterEventImpl(final Set<Key> channelKeys, final CarbonChannelRegistry registry) {
this.channelKeys = channelKeys;
this.registry = registry;
}
@Override
public Set<Key> channelKeys() {
return this.channelKeys;
}
@Override
public void register(final Key key, final ChatChannel channel, final boolean registerCommands) {
this.registry.register(channel);
if (registerCommands) {
this.registry.registerChannelCommands(channel);
}
}
} }