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 72d5977f..1389d410 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 @@ -21,11 +21,21 @@ package net.draycia.carbon.api.channels; import net.draycia.carbon.api.util.DefaultedKeyValueRegistry; import net.kyori.adventure.key.Key; +import net.kyori.registry.Registry; /** * Registry for chat channels. * * @since 2.0.0 */ -public interface ChannelRegistry extends DefaultedKeyValueRegistry { +public interface ChannelRegistry extends Registry, DefaultedKeyValueRegistry { + + /** + * Registers ingame commands for the channel. + * + * @param channel the channel to register commands for + * @since 2.1.0 + */ + void registerChannelCommands(final ChatChannel channel); + } diff --git a/api/src/main/java/net/draycia/carbon/api/events/CarbonChannelRegisterEvent.java b/api/src/main/java/net/draycia/carbon/api/events/CarbonChannelRegisterEvent.java new file mode 100644 index 00000000..58e03f4a --- /dev/null +++ b/api/src/main/java/net/draycia/carbon/api/events/CarbonChannelRegisterEvent.java @@ -0,0 +1,53 @@ +/* + * CarbonChat + * + * Copyright (c) 2021 Josua Parks (Vicarious) + * Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.draycia.carbon.api.events; + +import net.draycia.carbon.api.channels.ChatChannel; +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 interface CarbonChannelRegisterEvent extends CarbonEvent { + + /** + * The channels that were registered. + * + * @return the registered channels + * @since 2.1.0 + */ + Iterable channels(); + + /** + * Registers additional channels to the registry. Does not re-emit the 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 + * @since 2.1.0 + */ + void register(final Key key, final ChatChannel channel, final boolean registerCommands); + +} 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 2246a246..605c73d8 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 @@ -53,6 +53,7 @@ import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.command.PlayerCommander; import net.draycia.carbon.common.config.ConfigFactory; import net.draycia.carbon.common.events.CarbonReloadEvent; +import net.draycia.carbon.common.events.ChannelRegisterEvent; import net.draycia.carbon.common.messages.CarbonMessages; import net.draycia.carbon.common.messaging.packets.ChatMessagePacket; import net.kyori.adventure.audience.MessageType; @@ -60,7 +61,7 @@ import net.kyori.adventure.identity.Identity; import net.kyori.adventure.key.Key; import net.kyori.adventure.permission.PermissionChecker; import net.kyori.adventure.text.Component; -import net.kyori.registry.DefaultedRegistry; +import net.kyori.registry.DefaultedRegistryGetter; import ninja.egg82.messenger.services.PacketService; import org.apache.logging.log4j.Logger; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; @@ -80,7 +81,7 @@ import static net.kyori.adventure.text.Component.empty; @Singleton @DefaultQualifier(NonNull.class) -public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry { +public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistryGetter { private static @MonotonicNonNull ObjectMapper MAPPER; @@ -97,7 +98,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry private final Logger logger; private final ConfigFactory configFactory; private @MonotonicNonNull Key defaultKey; - private @MonotonicNonNull ChatChannel basicChannel; + //private @MonotonicNonNull ChatChannel basicChannel; private final CarbonMessages carbonMessages; private final CarbonChat carbonChat; @@ -110,7 +111,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry final Logger logger, final ConfigFactory configFactory, final CarbonMessages carbonMessages, - final BasicChatChannel basicChannel, + //final BasicChatChannel basicChannel, final CarbonChat carbonChat ) { this.configChannelDir = dataDirectory.resolve("channels"); @@ -118,7 +119,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry this.logger = logger; this.configFactory = configFactory; this.carbonMessages = carbonMessages; - this.basicChannel = basicChannel; + //this.basicChannel = basicChannel; this.carbonChat = carbonChat; carbonChat.eventHandler().subscribe(CarbonReloadEvent.class, event -> { @@ -212,9 +213,6 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry // otherwise, register all channels found try (final Stream paths = Files.walk(this.configChannelDir)) { - final CommandManager commandManager = - this.injector.getInstance(com.google.inject.Key.get(new TypeLiteral>() {})); - paths.forEach(path -> { final String fileName = path.getFileName().toString(); @@ -230,7 +228,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry } if (chatChannel.shouldRegisterCommands()) { - this.registerChannelCommands(carbonMessages, chatChannel, commandManager); + this.registerChannelCommands(chatChannel); } }); @@ -250,6 +248,8 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry } catch (final IOException exception) { exception.printStackTrace(); } + + this.carbonChat.eventHandler().emit(new ChannelRegisterEvent(this.channelMap.values(), this)); } public @Nullable ChatChannel loadChannel(final Path channelFile) { @@ -266,27 +266,6 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry return null; } - @Override - public ChatChannel defaultValue() { - return Objects.requireNonNull(this.get(this.defaultKey)); - } - - @Override - public Key defaultKey() { - return this.defaultKey; - } - - @Override - public ChatChannel getOrDefault(final Key key) { - final @Nullable ChatChannel channel = this.get(key); - - if (channel != null) { - return channel; - } - - return this.defaultValue(); - } - private void registerDefaultChannel() { try { Files.createDirectories(this.configChannelDir); @@ -325,60 +304,6 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry return channel; } - private void registerChannelCommands( - final CarbonMessages carbonMessages, - final ChatChannel channel, - final CommandManager commandManager - ) { - var builder = commandManager.commandBuilder(channel.commandName(), - channel.commandAliases(), commandManager.createDefaultCommandMeta()) - .argument(StringArgument.newBuilder("message").greedy().asOptional().build()); - - if (channel.permission() != null) { - builder = builder.permission(channel.permission()); - - // Add to LuckPerms permission suggestions... lol - this.carbonChat.server().console().get(PermissionChecker.POINTER).ifPresent(checker -> { - checker.test(channel.permission()); - checker.test(channel.permission() + ".see"); - checker.test(channel.permission() + ".speak"); - }); - } - - final Key channelKey = channel.key(); - - final var command = builder.senderType(PlayerCommander.class) - .handler(handler -> { - final var sender = ((PlayerCommander) handler.getSender()).carbonPlayer(); - final @Nullable ChatChannel chatChannel = this.get(channelKey); - - if (sender.muted()) { - this.carbonMessages.muteCannotSpeak(sender); - return; - } - - if (handler.contains("message")) { - final String message = handler.get("message"); - - // TODO: trigger platform events related to chat - this.sendMessageInChannelAsPlayer(sender, chatChannel, message); - } else { - sender.selectedChannel(chatChannel); - this.carbonMessages.changedChannels(sender, channelKey.value()); - } - }) - .build(); - - commandManager.command(command); - - final var channelCommand = commandManager.commandBuilder("channel", "ch") - .literal(channelKey.value()) - .proxies(command) - .build(); - - commandManager.command(channelCommand); - } - private void sendMessageInChannelAsPlayer( final CarbonPlayer sender, final ChatChannel channel, @@ -447,6 +372,60 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry return false; } + @Override + public void registerChannelCommands(final ChatChannel channel) { + final CommandManager commandManager = + this.injector.getInstance(com.google.inject.Key.get(new TypeLiteral>() {})); + + var builder = commandManager.commandBuilder(channel.commandName(), + channel.commandAliases(), commandManager.createDefaultCommandMeta()) + .argument(StringArgument.newBuilder("message").greedy().asOptional().build()); + + if (channel.permission() != null) { + builder = builder.permission(channel.permission()); + + // Add to LuckPerms permission suggestions... lol + this.carbonChat.server().console().get(PermissionChecker.POINTER).ifPresent(checker -> { + checker.test(channel.permission()); + checker.test(channel.permission() + ".see"); + checker.test(channel.permission() + ".speak"); + }); + } + + final Key channelKey = channel.key(); + + final var command = builder.senderType(PlayerCommander.class) + .handler(handler -> { + final var sender = ((PlayerCommander) handler.getSender()).carbonPlayer(); + final @Nullable ChatChannel chatChannel = this.get(channelKey); + + if (sender.muted()) { + this.carbonMessages.muteCannotSpeak(sender); + return; + } + + if (handler.contains("message")) { + final String message = handler.get("message"); + + // TODO: trigger platform events related to chat + this.sendMessageInChannelAsPlayer(sender, chatChannel, message); + } else { + sender.selectedChannel(chatChannel); + this.carbonMessages.changedChannels(sender, channelKey.value()); + } + }) + .build(); + + commandManager.command(command); + + final var channelCommand = commandManager.commandBuilder("channel", "ch") + .literal(channelKey.value()) + .proxies(command) + .build(); + + commandManager.command(channelCommand); + } + @Override public @NonNull ChatChannel register(final @NonNull Key key, final @NonNull ChatChannel value) { this.channelMap.put(key, value); @@ -473,4 +452,25 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry return Iterators.unmodifiableIterator(this.channelMap.values().iterator()); } + @Override + public ChatChannel defaultValue() { + return Objects.requireNonNull(this.get(this.defaultKey)); + } + + @Override + public Key defaultKey() { + return this.defaultKey; + } + + @Override + public ChatChannel getOrDefault(final Key key) { + final @Nullable ChatChannel channel = this.get(key); + + if (channel != null) { + return channel; + } + + return this.defaultValue(); + } + } diff --git a/common/src/main/java/net/draycia/carbon/common/events/ChannelRegisterEvent.java b/common/src/main/java/net/draycia/carbon/common/events/ChannelRegisterEvent.java new file mode 100644 index 00000000..ce289735 --- /dev/null +++ b/common/src/main/java/net/draycia/carbon/common/events/ChannelRegisterEvent.java @@ -0,0 +1,64 @@ +/* + * CarbonChat + * + * Copyright (c) 2021 Josua Parks (Vicarious) + * Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.draycia.carbon.common.events; + +import net.draycia.carbon.api.channels.ChatChannel; +import net.draycia.carbon.api.events.CarbonChannelRegisterEvent; +import net.draycia.carbon.api.events.CarbonEvent; +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 ChannelRegisterEvent implements CarbonChannelRegisterEvent { + + private final Iterable channels; + private final CarbonChannelRegistry registry; + + /** + * {@link CarbonEvent} that's called when channels are registered. + * + * @param channels the channels that were registered + * @since 2.1.0 + */ + public ChannelRegisterEvent(final Iterable channels, final CarbonChannelRegistry registry) { + this.channels = channels; + this.registry = registry; + } + + public Iterable channels() { + return this.channels; + } + + public void register(final Key key, final ChatChannel channel, final boolean registerCommands) { + this.registry.register(key, channel); + + if (registerCommands) { + this.registry.registerChannelCommands(channel); + } + } + +}