mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
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:
1 parent
2f1023d9f0
commit
88402ed0a6
4 files changed
+98
-68
No files matched your search
+67
-14
@@ -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<Key, ChatChannel> channelRegistry = Registry.create();
|
||||
private final Set<Key> configChannels = ConcurrentHashMap.newKeySet();
|
||||
private final AtomicBoolean firstRegister = new AtomicBoolean(true);
|
||||
//
|
||||
// private final BiMap<Key, ChatChannel> channelMap = Maps.synchronizedBiMap(HashBiMap.create());
|
||||
|
||||
@@ -163,20 +164,52 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
|
||||
|
||||
public void reloadConfigChannels() {
|
||||
final Registry<Key, ChatChannel> 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<Key> oldConfigChannels = Set.copyOf(this.configChannels);
|
||||
this.configChannels.clear();
|
||||
|
||||
final Registry<Key, ChatChannel> 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<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) {
|
||||
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<Path> 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<Commander> commandManager =
|
||||
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(),
|
||||
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<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);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+4
-36
@@ -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<Key> 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<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);
|
||||
}
|
||||
}
|
||||
public record ChannelRegisterEventImpl(
|
||||
CarbonChannelRegistry channelRegistry,
|
||||
Set<Key> registered
|
||||
) implements CarbonChannelRegisterEvent {
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user