mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 06:28:54 +00:00
Fix reloads by recreating the channel registry
Currently, this persists API-registered entries. Not sure if that's what we want to do, or if we should have an event for registering channels instead... We only support registering commands for channels added at startup anyways.
This commit is contained in:
1 parent
d05cc65258
commit
e9d4abd84b
1 file changed
+26
-43
+26
-43
@@ -37,6 +37,7 @@ import java.util.Collections;
|
|||||||
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.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import net.draycia.carbon.api.CarbonChat;
|
import net.draycia.carbon.api.CarbonChat;
|
||||||
import net.draycia.carbon.api.channels.ChannelRegistry;
|
import net.draycia.carbon.api.channels.ChannelRegistry;
|
||||||
@@ -53,6 +54,7 @@ import net.draycia.carbon.common.event.events.CarbonReloadEvent;
|
|||||||
import net.draycia.carbon.common.event.events.ChannelRegisterEventImpl;
|
import net.draycia.carbon.common.event.events.ChannelRegisterEventImpl;
|
||||||
import net.draycia.carbon.common.listeners.ChatListenerInternal;
|
import net.draycia.carbon.common.listeners.ChatListenerInternal;
|
||||||
import net.draycia.carbon.common.messages.CarbonMessages;
|
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||||
|
import net.draycia.carbon.common.util.Exceptions;
|
||||||
import net.kyori.adventure.audience.Audience;
|
import net.kyori.adventure.audience.Audience;
|
||||||
import net.kyori.adventure.key.Key;
|
import net.kyori.adventure.key.Key;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
@@ -92,7 +94,8 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
|
|||||||
private final CarbonEventHandler eventHandler;
|
private final CarbonEventHandler eventHandler;
|
||||||
private final Provider<CarbonChat> carbonChatProvider;
|
private final Provider<CarbonChat> carbonChatProvider;
|
||||||
|
|
||||||
private final Registry<Key, ChatChannel> channelRegistry = Registry.create();
|
private volatile Registry<Key, ChatChannel> channelRegistry = Registry.create();
|
||||||
|
private final Set<Key> configChannels = ConcurrentHashMap.newKeySet();
|
||||||
//
|
//
|
||||||
// private final BiMap<Key, ChatChannel> channelMap = Maps.synchronizedBiMap(HashBiMap.create());
|
// private final BiMap<Key, ChatChannel> channelMap = Maps.synchronizedBiMap(HashBiMap.create());
|
||||||
|
|
||||||
@@ -115,7 +118,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
|
|||||||
this.eventHandler = events;
|
this.eventHandler = events;
|
||||||
this.carbonChatProvider = carbonChatProvider;
|
this.carbonChatProvider = carbonChatProvider;
|
||||||
|
|
||||||
events.subscribe(CarbonReloadEvent.class, event -> this.reloadRegisteredConfigChannels());
|
events.subscribe(CarbonReloadEvent.class, event -> this.reloadConfigChannels());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ConfigurationTransformation.Versioned versioned() {
|
public static ConfigurationTransformation.Versioned versioned() {
|
||||||
@@ -162,64 +165,45 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reloadRegisteredConfigChannels() {
|
public void reloadConfigChannels() {
|
||||||
try (final Stream<Path> paths = Files.walk(this.configChannelDir)) {
|
final Registry<Key, ChatChannel> newRegistry = Registry.create();
|
||||||
paths.forEach(path -> {
|
// Copy API registrations over
|
||||||
final String fileName = path.getFileName().toString();
|
for (final Key registered : this.channelRegistry.keys()) {
|
||||||
|
if (!this.configChannels.contains(registered)) {
|
||||||
if (!fileName.endsWith(".conf")) {
|
newRegistry.register(registered, this.channelRegistry.getHolder(registered).valueOrThrow());
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
final @Nullable ChatChannel chatChannel = this.registerChannelFromPath(path);
|
|
||||||
|
|
||||||
if (chatChannel == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.injector.injectMembers(chatChannel);
|
|
||||||
|
|
||||||
final @Nullable ChatChannel existingChannel = this.channel(chatChannel.key());
|
|
||||||
|
|
||||||
if (existingChannel == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.register(chatChannel);
|
|
||||||
});
|
|
||||||
} catch (final IOException exception) {
|
|
||||||
exception.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
this.configChannels.clear();
|
||||||
|
this.channelRegistry = newRegistry;
|
||||||
|
this.loadConfigChannels(this.carbonMessages);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadConfigChannels(final CarbonMessages carbonMessages) {
|
public void loadConfigChannels(final CarbonMessages messages) {
|
||||||
this.defaultKey = this.configFactory.primaryConfig().defaultChannel();
|
this.defaultKey = this.configFactory.primaryConfig().defaultChannel();
|
||||||
|
|
||||||
if (!Files.exists(this.configChannelDir)) {
|
if (!Files.exists(this.configChannelDir) || this.isPathEmpty(this.configChannelDir)) {
|
||||||
// no channels to register, register default channel
|
// no channels to register, register default channel
|
||||||
this.registerDefaultChannel();
|
this.registerDefaultChannel();
|
||||||
return;
|
return;
|
||||||
} else if (this.isPathEmpty(this.configChannelDir)) {
|
|
||||||
this.register(this.injector.getInstance(BasicChatChannel.class));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise, register all channels found
|
// otherwise, register all channels found
|
||||||
try (final Stream<Path> paths = Files.walk(this.configChannelDir)) {
|
try (final Stream<Path> paths = Files.walk(this.configChannelDir)) {
|
||||||
paths.forEach(path -> {
|
paths.forEach(path -> {
|
||||||
final String fileName = path.getFileName().toString();
|
final String fileName = path.getFileName().toString();
|
||||||
|
|
||||||
if (!fileName.endsWith(".conf")) {
|
if (!fileName.endsWith(".conf")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final @Nullable ChatChannel chatChannel = this.registerChannelFromPath(path);
|
final @Nullable ChatChannel chatChannel = this.channelFromPath(path);
|
||||||
|
|
||||||
if (chatChannel == null) {
|
if (chatChannel == null) {
|
||||||
this.logger.warn("Failed to load channel from file [" + fileName + "]");
|
this.logger.warn("Failed to load channel from file [" + fileName + "]");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.injector.injectMembers(chatChannel);
|
this.injector.injectMembers(chatChannel);
|
||||||
|
this.configChannels.add(chatChannel.key());
|
||||||
|
this.register(chatChannel);
|
||||||
|
|
||||||
if (chatChannel.shouldRegisterCommands()) {
|
if (chatChannel.shouldRegisterCommands()) {
|
||||||
this.registerChannelCommands(chatChannel);
|
this.registerChannelCommands(chatChannel);
|
||||||
@@ -240,7 +224,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
|
|||||||
|
|
||||||
this.logger.info("Registered channels: [" + channels + "]");
|
this.logger.info("Registered channels: [" + channels + "]");
|
||||||
} catch (final IOException exception) {
|
} catch (final IOException exception) {
|
||||||
exception.printStackTrace();
|
throw Exceptions.rethrow(exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.eventHandler.emit(new ChannelRegisterEventImpl(this.keys(), this));
|
this.eventHandler.emit(new ChannelRegisterEventImpl(this.keys(), this));
|
||||||
@@ -275,12 +259,13 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
|
|||||||
loader.save(node);
|
loader.save(node);
|
||||||
|
|
||||||
this.register(configChannel);
|
this.register(configChannel);
|
||||||
|
this.configChannels.add(configChannel.key());
|
||||||
} catch (final IOException exception) {
|
} catch (final IOException exception) {
|
||||||
exception.printStackTrace();
|
throw Exceptions.rethrow(exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private @Nullable ChatChannel registerChannelFromPath(final Path channelPath) {
|
private @Nullable ChatChannel channelFromPath(final Path channelPath) {
|
||||||
final @Nullable ChatChannel channel = this.loadChannel(channelPath);
|
final @Nullable ChatChannel channel = this.loadChannel(channelPath);
|
||||||
|
|
||||||
if (channel == null) {
|
if (channel == null) {
|
||||||
@@ -293,7 +278,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
|
|||||||
this.logger.info("Default channel is [" + channelKey + "]");
|
this.logger.info("Default channel is [" + channelKey + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.register(channel);
|
//this.register(channel);
|
||||||
|
|
||||||
return channel;
|
return channel;
|
||||||
}
|
}
|
||||||
@@ -324,10 +309,8 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
|
|||||||
try (DirectoryStream<Path> directory = Files.newDirectoryStream(path)) {
|
try (DirectoryStream<Path> directory = Files.newDirectoryStream(path)) {
|
||||||
return !directory.iterator().hasNext();
|
return !directory.iterator().hasNext();
|
||||||
} catch (final IOException exception) {
|
} catch (final IOException exception) {
|
||||||
exception.printStackTrace();
|
throw Exceptions.rethrow(exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in new issue
Block a user