From 1747c8e0623ff9201cd27241c889b8cebd162f02 Mon Sep 17 00:00:00 2001 From: Draycia Date: Tue, 30 Nov 2021 19:04:37 -0800 Subject: [PATCH] allow reloading channel settings --- .../carbon/bukkit/CarbonChatBukkit.java | 2 +- .../channels/CarbonChannelRegistry.java | 240 ++++++++++++------ .../channels/ConfigChannelSettings.java | 62 +++++ .../command/commands/ReloadCommand.java | 5 + .../common/users/CarbonPlayerCommon.java | 8 +- .../carbon/fabric/CarbonChatFabric.java | 2 +- .../carbon/sponge/CarbonChatSponge.java | 2 +- .../carbon/velocity/CarbonChatVelocity.java | 2 +- 8 files changed, 233 insertions(+), 90 deletions(-) create mode 100644 common/src/main/java/net/draycia/carbon/common/channels/ConfigChannelSettings.java diff --git a/bukkit/src/main/java/net/draycia/carbon/bukkit/CarbonChatBukkit.java b/bukkit/src/main/java/net/draycia/carbon/bukkit/CarbonChatBukkit.java index 715b1b76..4e91fbb2 100644 --- a/bukkit/src/main/java/net/draycia/carbon/bukkit/CarbonChatBukkit.java +++ b/bukkit/src/main/java/net/draycia/carbon/bukkit/CarbonChatBukkit.java @@ -120,7 +120,7 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat { () -> PlayerUtils.saveLoggedInPlayers(this.carbonServerBukkit, this.userManager), saveDelay, saveDelay); // Load channels - ((CarbonChannelRegistry) this.channelRegistry()).loadChannels(); + ((CarbonChannelRegistry) this.channelRegistry()).loadConfigChannels(); } @Override 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 23182037..ad40d6c6 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 @@ -21,6 +21,10 @@ package net.draycia.carbon.common.channels; import cloud.commandframework.CommandManager; import cloud.commandframework.arguments.standard.StringArgument; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; +import com.google.common.collect.Iterators; +import com.google.common.collect.Maps; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.Singleton; @@ -29,7 +33,10 @@ import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Collections; +import java.util.Iterator; import java.util.Objects; +import java.util.Set; import java.util.stream.Stream; import net.draycia.carbon.api.channels.ChannelRegistry; import net.draycia.carbon.api.channels.ChatChannel; @@ -41,7 +48,6 @@ import net.draycia.carbon.common.messages.CarbonMessageService; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; import net.kyori.registry.DefaultedRegistry; -import net.kyori.registry.RegistryImpl; import org.apache.logging.log4j.Logger; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.NonNull; @@ -54,7 +60,7 @@ import org.spongepowered.configurate.serialize.SerializationException; @Singleton @DefaultQualifier(NonNull.class) -public class CarbonChannelRegistry extends RegistryImpl implements ChannelRegistry, DefaultedRegistry { +public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry { private static @MonotonicNonNull ObjectMapper MAPPER; @@ -67,7 +73,7 @@ public class CarbonChannelRegistry extends RegistryImpl implem } private final ConfigFactory configLoader; - private final Path dataDirectory; + private final Path configChannelDir; private final Injector injector; private final Logger logger; private final ConfigFactory configFactory; @@ -75,6 +81,8 @@ public class CarbonChannelRegistry extends RegistryImpl implem private @MonotonicNonNull ChatChannel basicChannel; private final CarbonMessageService messageService; + private final BiMap map = Maps.synchronizedBiMap(HashBiMap.create()); + @Inject public CarbonChannelRegistry( final ConfigFactory configLoader, @@ -82,48 +90,57 @@ public class CarbonChannelRegistry extends RegistryImpl implem final Injector injector, final Logger logger, final ConfigFactory configFactory, - final CarbonMessageService messageService + final CarbonMessageService messageService, + final BasicChatChannel basicChannel ) { this.configLoader = configLoader; - this.dataDirectory = dataDirectory; + this.configChannelDir = dataDirectory.resolve("channels"); this.injector = injector; this.logger = logger; this.configFactory = configFactory; this.messageService = messageService; + this.basicChannel = basicChannel; } - public void loadChannels() { - final Path channelDirectory = this.dataDirectory.resolve("channels"); - this.basicChannel = this.injector.getInstance(BasicChatChannel.class); + public void reloadRegisteredConfigChannels() { + try (final Stream paths = Files.walk(this.configChannelDir)) { + paths.forEach(path -> { + final String fileName = path.getFileName().toString(); + + if (!fileName.endsWith(".conf")) { + return; + } + + final @Nullable ChatChannel chatChannel = this.registerChannelFromPath(path); + + if (chatChannel == null) { + return; + } + + final @Nullable ChatChannel existingChannel = this.get(chatChannel.key()); + + if (existingChannel == null) { + return; + } + + this.register(chatChannel.key(), chatChannel); + }); + } catch (final IOException exception) { + exception.printStackTrace(); + } + } + + public void loadConfigChannels() { this.defaultKey = this.configFactory.primaryConfig().defaultChannel(); - if (!Files.exists(channelDirectory) || isPathEmpty(channelDirectory)) { - // folder doesn't exist, no channels setup - try { - Files.createDirectories(channelDirectory); - - this.register(this.basicChannel.key(), this.basicChannel); - - final Path configFile = channelDirectory.resolve("basic-channel.conf.example"); - - final var loader = this.configLoader.configurationLoader(configFile); - final var node = loader.load(); - - final var configChannel = this.injector.getInstance(ConfigChatChannel.class); - node.set(ConfigChatChannel.class, configChannel); - - loader.save(node); - - // TODO: create advanced-channel.conf.example - // TODO: log in console, "no channels found - adding example configs" - } catch (final IOException exception) { - exception.printStackTrace(); - } - + if (!Files.exists(this.configChannelDir) || this.isPathEmpty(this.configChannelDir)) { + // no channels to register, register default channel + this.registerDefaultChannel(); return; } - try (final Stream paths = Files.walk(channelDirectory)) { + // 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>() {})); @@ -134,59 +151,11 @@ public class CarbonChannelRegistry extends RegistryImpl implem return; } - final @Nullable ChatChannel channel = this.loadChannel(path); + final @Nullable ChatChannel chatChannel = this.registerChannelFromPath(path); - if (channel == null) { - return; + if (chatChannel != null && chatChannel.shouldRegisterCommands()) { + this.registerChannelCommands(chatChannel, commandManager); } - - final Key channelKey = channel.key(); - - this.logger.info("Registering channel with key [" + channelKey + "]"); - register(channelKey, channel); - - if (!channel.shouldRegisterCommands()) { - return; - } - - final var command = commandManager.commandBuilder(channel.commandName(), - channel.commandAliases(), commandManager.createDefaultCommandMeta()) - .argument(StringArgument.newBuilder("message").greedy().asOptional().build()) - .permission("carbon.channel." + channelKey) - .senderType(PlayerCommander.class) - .handler(handler -> { - final var sender = ((PlayerCommander) handler.getSender()).carbonPlayer(); - - if (sender.muted(channel)) { - // TODO: "you are muted in this channel!!!!" - return; - } - - if (handler.contains("message")) { - final String message = handler.get("message"); - final var component = Component.text(message); - - // TODO: trigger platform events related to chat - // TODO: also make sure carbon events are also emitted properly? - for (final var recipient : channel.recipients(sender)) { - final var renderedMessage = channel.render(sender, recipient, component, component); - recipient.sendMessage(renderedMessage.component(), renderedMessage.messageType()); - } - } else { - sender.selectedChannel(channel); - this.messageService.changedChannels(sender, channel.key().value()); - } - }) - .build(); // TODO: command aliases - - commandManager.command(command); - - final var channelCommand = commandManager.commandBuilder("channel", "ch") - .literal(channelKey.value()) - .proxies(command) - .build(); - - commandManager.command(channelCommand); }); } catch (final IOException exception) { exception.printStackTrace(); @@ -226,6 +195,85 @@ public class CarbonChannelRegistry extends RegistryImpl implem return this.defaultValue(); } + private void registerDefaultChannel() { + try { + Files.createDirectories(this.configChannelDir); + + this.register(this.basicChannel.key(), this.basicChannel); + + final Path configFile = this.configChannelDir.resolve("basic-channel.conf.example"); + + final var loader = this.configLoader.configurationLoader(configFile); + final var node = loader.load(); + + final var configChannel = this.injector.getInstance(ConfigChatChannel.class); + node.set(ConfigChatChannel.class, configChannel); + + loader.save(node); + + // TODO: create advanced-channel.conf.example + // TODO: log in console, "no channels found - adding example configs" + } catch (final IOException exception) { + exception.printStackTrace(); + } + } + + private @Nullable ChatChannel registerChannelFromPath(final Path channelPath) { + final @Nullable ChatChannel channel = this.loadChannel(channelPath); + + if (channel == null) { + return null; + } + + final Key channelKey = channel.key(); + + this.logger.info("Registering channel with key [" + channelKey + "]"); + this.register(channelKey, channel); + + return channel; + } + + private void registerChannelCommands(final ChatChannel channel, final CommandManager commandManager) { + final var command = commandManager.commandBuilder(channel.commandName(), + channel.commandAliases(), commandManager.createDefaultCommandMeta()) + .argument(StringArgument.newBuilder("message").greedy().asOptional().build()) + .permission("carbon.channel." + channel.key().value()) + .senderType(PlayerCommander.class) + .handler(handler -> { + final var sender = ((PlayerCommander) handler.getSender()).carbonPlayer(); + + if (sender.muted(channel)) { + // TODO: "you are muted in this channel!!!!" + return; + } + + if (handler.contains("message")) { + final String message = handler.get("message"); + final var component = Component.text(message); + + // TODO: trigger platform events related to chat + // TODO: also make sure carbon events are also emitted properly? + for (final var recipient : channel.recipients(sender)) { + final var renderedMessage = channel.render(sender, recipient, component, component); + recipient.sendMessage(renderedMessage.component(), renderedMessage.messageType()); + } + } else { + sender.selectedChannel(channel); + this.messageService.changedChannels(sender, channel.key().value()); + } + }) + .build(); // TODO: command aliases + + commandManager.command(command); + + final var channelCommand = commandManager.commandBuilder("channel", "ch") + .literal(channel.key().value()) + .proxies(command) + .build(); + + commandManager.command(channelCommand); + } + private boolean isPathEmpty(final Path path) { try (DirectoryStream directory = Files.newDirectoryStream(path)) { return !directory.iterator().hasNext(); @@ -236,4 +284,30 @@ public class CarbonChannelRegistry extends RegistryImpl implem return false; } + @Override + public @NonNull ChatChannel register(final @NonNull Key key, final @NonNull ChatChannel value) { + this.map.put(key, value); + return value; + } + + @Override + public @Nullable ChatChannel get(final @NonNull Key key) { + return this.map.get(key); + } + + @Override + public @Nullable Key key(final @NonNull ChatChannel value) { + return this.map.inverse().get(value); + } + + @Override + public @NonNull Set keySet() { + return Collections.unmodifiableSet(this.map.keySet()); + } + + @Override + public @NonNull Iterator iterator() { + return Iterators.unmodifiableIterator(this.map.values().iterator()); + } + } diff --git a/common/src/main/java/net/draycia/carbon/common/channels/ConfigChannelSettings.java b/common/src/main/java/net/draycia/carbon/common/channels/ConfigChannelSettings.java new file mode 100644 index 00000000..fced14f2 --- /dev/null +++ b/common/src/main/java/net/draycia/carbon/common/channels/ConfigChannelSettings.java @@ -0,0 +1,62 @@ +/* + * 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.channels; + +import java.util.Collections; +import java.util.List; +import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource; +import net.kyori.adventure.key.Key; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.spongepowered.configurate.objectmapping.ConfigSerializable; +import org.spongepowered.configurate.objectmapping.meta.Comment; +import org.spongepowered.configurate.objectmapping.meta.Setting; + +@ConfigSerializable +public final class ConfigChannelSettings { + + @Comment(""" + The channel's key, used to track the channel. + You only need to change the second part of the key. "global" by default. + The value is what's used in commands, this is probably what you want to change. + """) + private @Nullable Key key = Key.key("carbon", "basic"); + + @Comment(""" + The permission required to use the channel. + To read messages you must have the permission carbon.channel.basic.see + To send messages you must have the permission carbon.channel.basic.speak + If you want to give both, grant carbon.channel.basic or carbon.channel.basic.* + """) + private @Nullable String permission = "carbon.channel.basic"; + + @Setting("format") + @Comment("The chat formats for this channel.") + private @Nullable ConfigChannelMessageSource messageSource = new ConfigChannelMessageSource(); + + @Comment("Messages will be sent in this channel if they start with this prefix.") + private @Nullable String quickPrefix = ""; + + private @Nullable Boolean shouldRegisterCommands = true; + + private @Nullable String commandName = null; + + private @Nullable List commandAliases = Collections.emptyList(); + +} diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/ReloadCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/ReloadCommand.java index d94ef588..4bc38a5e 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/ReloadCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/ReloadCommand.java @@ -21,6 +21,7 @@ package net.draycia.carbon.common.command.commands; import cloud.commandframework.CommandManager; import com.google.inject.Inject; +import net.draycia.carbon.common.channels.CarbonChannelRegistry; import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.config.ConfigFactory; import net.draycia.carbon.common.config.PrimaryConfig; @@ -37,12 +38,15 @@ public class ReloadCommand { public ReloadCommand( final CommandManager commandManager, final ConfigFactory configFactory, + final CarbonChannelRegistry channelRegistry, final CarbonMessageService messageService ) { final var command = commandManager.commandBuilder("creload", "carbonreload") .permission("carbon.reload") .senderType(Commander.class) .handler(handler -> { + channelRegistry.reloadRegisteredConfigChannels(); + final @Nullable PrimaryConfig config = configFactory.reloadPrimaryConfig(); if (config != null) { @@ -50,6 +54,7 @@ public class ReloadCommand { } else { messageService.configReloadFailed(handler.getSender()); } + }) .build(); diff --git a/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java b/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java index d18ffac0..62eec6a9 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java +++ b/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java @@ -24,6 +24,7 @@ import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.UUID; +import net.draycia.carbon.api.CarbonChatProvider; import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.users.punishments.MuteEntry; @@ -31,6 +32,7 @@ import net.draycia.carbon.api.util.InventorySlot; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.ForwardingAudience; import net.kyori.adventure.identity.Identity; +import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.NonNull; @@ -42,7 +44,7 @@ import org.jetbrains.annotations.NotNull; public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Single { protected boolean deafened = false; - protected @Nullable ChatChannel selectedChannel = null; + protected @Nullable Key selectedChannel = null; // All players have these protected @MonotonicNonNull String username; @@ -267,12 +269,12 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing @Override public @Nullable ChatChannel selectedChannel() { - return this.selectedChannel; + return CarbonChatProvider.carbonChat().channelRegistry().get(this.selectedChannel); } @Override public void selectedChannel(final @Nullable ChatChannel chatChannel) { - this.selectedChannel = chatChannel; + this.selectedChannel = chatChannel.key(); } @Override diff --git a/fabric/src/main/java/net/draycia/carbon/fabric/CarbonChatFabric.java b/fabric/src/main/java/net/draycia/carbon/fabric/CarbonChatFabric.java index 0902d347..00b1215b 100644 --- a/fabric/src/main/java/net/draycia/carbon/fabric/CarbonChatFabric.java +++ b/fabric/src/main/java/net/draycia/carbon/fabric/CarbonChatFabric.java @@ -101,7 +101,7 @@ public final class CarbonChatFabric implements ModInitializer, CarbonChat { CloudUtils.registerCommands(this.injector); // Load channels - ((CarbonChannelRegistry) this.channelRegistry()).loadChannels(); + ((CarbonChannelRegistry) this.channelRegistry()).loadConfigChannels(); } private void registerChatListener() { diff --git a/sponge/src/main/java/net/draycia/carbon/sponge/CarbonChatSponge.java b/sponge/src/main/java/net/draycia/carbon/sponge/CarbonChatSponge.java index ffccd1c1..d44c88f5 100644 --- a/sponge/src/main/java/net/draycia/carbon/sponge/CarbonChatSponge.java +++ b/sponge/src/main/java/net/draycia/carbon/sponge/CarbonChatSponge.java @@ -110,7 +110,7 @@ public final class CarbonChatSponge implements CarbonChat { ListenerUtils.registerCommonListeners(this.injector); // Load channels - ((CarbonChannelRegistry) this.channelRegistry()).loadChannels(); + ((CarbonChannelRegistry) this.channelRegistry()).loadConfigChannels(); // Commands CloudUtils.registerCommands(this.injector); diff --git a/velocity/src/main/java/net/draycia/carbon/velocity/CarbonChatVelocity.java b/velocity/src/main/java/net/draycia/carbon/velocity/CarbonChatVelocity.java index e38e64b3..fa0c8915 100644 --- a/velocity/src/main/java/net/draycia/carbon/velocity/CarbonChatVelocity.java +++ b/velocity/src/main/java/net/draycia/carbon/velocity/CarbonChatVelocity.java @@ -114,7 +114,7 @@ public class CarbonChatVelocity implements CarbonChat { ListenerUtils.registerCommonListeners(this.injector); // Load channels - ((CarbonChannelRegistry) this.channelRegistry()).loadChannels(); + ((CarbonChannelRegistry) this.channelRegistry()).loadConfigChannels(); // Commands CloudUtils.registerCommands(this.injector);