diff --git a/api/src/main/java/net/draycia/carbon/api/channels/ChannelRegistry.java b/api/src/main/java/net/draycia/carbon/api/channels/ChannelRegistry.java
index 7571d2d2..441d1545 100644
--- a/api/src/main/java/net/draycia/carbon/api/channels/ChannelRegistry.java
+++ b/api/src/main/java/net/draycia/carbon/api/channels/ChannelRegistry.java
@@ -20,6 +20,7 @@
package net.draycia.carbon.api.channels;
import java.util.Set;
+import java.util.function.Consumer;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -32,15 +33,9 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public interface ChannelRegistry {
/**
- * Registers ingame commands for the channel.
+ * Registers the chat channel with its key.
*
- * @param channel the channel to register commands for
- * @since 2.1.0
- */
- void registerChannelCommands(final ChatChannel channel);
-
- /**
- * Registers the chat channel with it's key.
+ *
Registrations will persist when reloading Carbon's configuration.
*
* @param channel the channel to register
* @since 2.1.0
@@ -89,4 +84,15 @@ public interface ChannelRegistry {
*/
ChatChannel keyOrDefault(final Key key);
+ /**
+ * The provided action will be executed immediately for all currently registered
+ * channels.
+ *
+ * When new channels are registered, the action will be invoked again for each new channel.
+ *
+ * @param action action
+ * @since 2.1.0
+ */
+ void allKeys(Consumer action);
+
}
diff --git a/api/src/main/java/net/draycia/carbon/api/event/events/CarbonChannelRegisterEvent.java b/api/src/main/java/net/draycia/carbon/api/event/events/CarbonChannelRegisterEvent.java
index 12f0531e..ee732334 100644
--- a/api/src/main/java/net/draycia/carbon/api/event/events/CarbonChannelRegisterEvent.java
+++ b/api/src/main/java/net/draycia/carbon/api/event/events/CarbonChannelRegisterEvent.java
@@ -20,14 +20,19 @@
package net.draycia.carbon.api.event.events;
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.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull;
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.
+ *
+ * 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.
*
* @since 2.1.0
*/
@@ -35,21 +40,19 @@ import org.checkerframework.framework.qual.DefaultQualifier;
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
*/
- Set 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
- * @param channel the channel to register
- * @param registerCommands if commands for the channel should be registered
+ * @return key(s) registered
* @since 2.1.0
*/
- void register(final Key key, final ChatChannel channel, final boolean registerCommands);
+ Set registered();
}
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 766adb7a..e5ef7c12 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
@@ -32,14 +32,16 @@ import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
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.ChatChannel;
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.users.CarbonPlayer;
import net.draycia.carbon.api.util.KeyedRenderer;
@@ -93,7 +95,6 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
private volatile Registry channelRegistry = Registry.create();
private final Set configChannels = ConcurrentHashMap.newKeySet();
- private final AtomicBoolean firstRegister = new AtomicBoolean(true);
//
// private final BiMap channelMap = Maps.synchronizedBiMap(HashBiMap.create());
@@ -163,20 +164,52 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
public void reloadConfigChannels() {
final Registry newRegistry = Registry.create();
+
// Copy API registrations over
for (final Key registered : this.channelRegistry.keys()) {
if (!this.configChannels.contains(registered)) {
newRegistry.register(registered, this.channelRegistry.getHolder(registered).valueOrThrow());
}
}
+
+ final Set oldConfigChannels = Set.copyOf(this.configChannels);
this.configChannels.clear();
+
+ final Registry oldRegistry = this.channelRegistry;
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 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) {
- 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();
List channelConfigs = FileUtil.listDirectoryEntries(this.configChannelDir, "*.conf");
@@ -202,12 +235,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
this.injector.injectMembers(chatChannel);
this.configChannels.add(chatChannel.key());
- this.register(chatChannel);
-
- if (firstRegister // We only support commands being registered at startup currently
- && chatChannel.shouldRegisterCommands()) {
- this.registerChannelCommands(chatChannel);
- }
+ this.register(chatChannel, false);
}
if (this.channel(this.defaultKey) == null) {
@@ -223,8 +251,6 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
final String channels = String.join(", ", channelList);
this.logger.info("Registered channels: [" + channels + "]");
-
- this.eventHandler.emit(new ChannelRegisterEventImpl(this.keys(), this));
}
private void saveDefaultChannelConfig() {
@@ -276,10 +302,12 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
}
}
- @Override
- public void registerChannelCommands(final ChatChannel channel) {
+ private void registerChannelCommands(final ChatChannel channel) {
final CommandManager commandManager =
this.injector.getInstance(com.google.inject.Key.get(new TypeLiteral>() {}));
+ if (!commandManager.isCommandRegistrationAllowed() || commandManager.commandTree().getNamedNode(channel.commandName()) != null) {
+ return;
+ }
Command.Builder builder = commandManager.commandBuilder(channel.commandName(),
channel.commandAliases(), commandManager.createDefaultCommandMeta())
@@ -335,7 +363,17 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
@Override
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);
+ if (channel.shouldRegisterCommands()) {
+ this.registerChannelCommands(channel);
+ }
+ if (fireRegisterEvent) {
+ this.eventHandler.emit(new ChannelRegisterEventImpl(this, Set.of(channel.key())));
+ }
}
@Override
@@ -383,4 +421,19 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
return this.defaultChannel();
}
+ @Override
+ public void allKeys(final Consumer 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);
+ }
+ }
+ );
+ }
+
}
diff --git a/common/src/main/java/net/draycia/carbon/common/event/events/ChannelRegisterEventImpl.java b/common/src/main/java/net/draycia/carbon/common/event/events/ChannelRegisterEventImpl.java
index 1f8342b7..7eceb39a 100644
--- a/common/src/main/java/net/draycia/carbon/common/event/events/ChannelRegisterEventImpl.java
+++ b/common/src/main/java/net/draycia/carbon/common/event/events/ChannelRegisterEventImpl.java
@@ -20,48 +20,16 @@
package net.draycia.carbon.common.event.events;
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.common.channels.CarbonChannelRegistry;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
-/**
- * {@link CarbonEvent} that's called when channels are registered.
- *
- * @since 2.1.0
- */
@DefaultQualifier(NonNull.class)
-public class ChannelRegisterEventImpl implements CarbonChannelRegisterEvent {
-
- private final Set channelKeys;
- private final CarbonChannelRegistry registry;
-
- /**
- * {@link CarbonEvent} that's called when channels are registered.
- *
- * @param channelKeys the channels that were registered
- * @since 2.1.0
- */
- public ChannelRegisterEventImpl(final Set channelKeys, final CarbonChannelRegistry registry) {
- this.channelKeys = channelKeys;
- this.registry = registry;
- }
-
- @Override
- public Set 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);
- }
- }
+public record ChannelRegisterEventImpl(
+ CarbonChannelRegistry channelRegistry,
+ Set registered
+) implements CarbonChannelRegisterEvent {
}