mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
allow reloading channel settings
This commit is contained in:
8 files changed
+233
-90
No files matched your search
+157
-83
@@ -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<Key, ChatChannel> implements ChannelRegistry, DefaultedRegistry<Key, ChatChannel> {
|
||||
public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry<Key, ChatChannel> {
|
||||
|
||||
private static @MonotonicNonNull ObjectMapper<ConfigChatChannel> MAPPER;
|
||||
|
||||
@@ -67,7 +73,7 @@ public class CarbonChannelRegistry extends RegistryImpl<Key, ChatChannel> 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<Key, ChatChannel> implem
|
||||
private @MonotonicNonNull ChatChannel basicChannel;
|
||||
private final CarbonMessageService messageService;
|
||||
|
||||
private final BiMap<Key, ChatChannel> map = Maps.synchronizedBiMap(HashBiMap.create());
|
||||
|
||||
@Inject
|
||||
public CarbonChannelRegistry(
|
||||
final ConfigFactory configLoader,
|
||||
@@ -82,48 +90,57 @@ public class CarbonChannelRegistry extends RegistryImpl<Key, ChatChannel> 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<Path> 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<Path> paths = Files.walk(channelDirectory)) {
|
||||
// otherwise, register all channels found
|
||||
try (final Stream<Path> paths = Files.walk(this.configChannelDir)) {
|
||||
final CommandManager<Commander> commandManager =
|
||||
this.injector.getInstance(com.google.inject.Key.get(new TypeLiteral<CommandManager<Commander>>() {}));
|
||||
|
||||
@@ -134,59 +151,11 @@ public class CarbonChannelRegistry extends RegistryImpl<Key, ChatChannel> 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.<Commander>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<Key, ChatChannel> 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<Commander> commandManager) {
|
||||
final var command = commandManager.commandBuilder(channel.commandName(),
|
||||
channel.commandAliases(), commandManager.createDefaultCommandMeta())
|
||||
.argument(StringArgument.<Commander>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<Path> directory = Files.newDirectoryStream(path)) {
|
||||
return !directory.iterator().hasNext();
|
||||
@@ -236,4 +284,30 @@ public class CarbonChannelRegistry extends RegistryImpl<Key, ChatChannel> 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<Key> keySet() {
|
||||
return Collections.unmodifiableSet(this.map.keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Iterator<ChatChannel> iterator() {
|
||||
return Iterators.unmodifiableIterator(this.map.values().iterator());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<String> commandAliases = Collections.emptyList();
|
||||
|
||||
}
|
||||
@@ -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<Commander> 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();
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user