More cleanup

This commit is contained in:
Draycia committed 2023-06-23 13:49:11 -07:00
1 parent 547adcf0fe
commit 47b1ba4d7f
8 files changed
+47 -28

No files matched your search

@@ -22,6 +22,7 @@ package net.draycia.carbon.api.channels;
import net.draycia.carbon.api.util.DefaultedKeyValueRegistry; import net.draycia.carbon.api.util.DefaultedKeyValueRegistry;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.kyori.registry.Registry; import net.kyori.registry.Registry;
import org.checkerframework.checker.nullness.qual.Nullable;
/** /**
* Registry for chat channels. * Registry for chat channels.
@@ -38,4 +39,12 @@ public interface ChannelRegistry extends Registry<Key, ChatChannel>, DefaultedKe
*/ */
void registerChannelCommands(final ChatChannel channel); void registerChannelCommands(final ChatChannel channel);
/**
* Gets the channel with the corresponding command name.
*
* @param channelName the command used to speak in the channel
* @return the channel
*/
@Nullable ChatChannel byCommandName(final String channelName);
} }
@@ -42,7 +42,6 @@ import java.util.Objects;
import java.util.Set; import java.util.Set;
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.CarbonChatProvider;
import net.draycia.carbon.api.channels.ChannelRegistry; import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.event.CarbonEventHandler; import net.draycia.carbon.api.event.CarbonEventHandler;
@@ -162,7 +161,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
if (startVersion != endVersion) { // we might not have made any changes if (startVersion != endVersion) { // we might not have made any changes
// TODO: use logger // TODO: use logger
CarbonChatProvider.carbonChat().logger().info("Updated config schema from " + startVersion + " to " + endVersion); //CarbonChatProvider.carbonChat().logger().info("Updated config schema from " + startVersion + " to " + endVersion);
} }
} }
@@ -184,6 +183,8 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
return; return;
} }
this.injector.injectMembers(chatChannel);
final @Nullable ChatChannel existingChannel = this.get(chatChannel.key()); final @Nullable ChatChannel existingChannel = this.get(chatChannel.key());
if (existingChannel == null) { if (existingChannel == null) {
@@ -226,6 +227,8 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
return; return;
} }
this.injector.injectMembers(chatChannel);
if (chatChannel.shouldRegisterCommands()) { if (chatChannel.shouldRegisterCommands()) {
this.registerChannelCommands(chatChannel); this.registerChannelCommands(chatChannel);
} }
@@ -474,4 +477,15 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
return this.defaultValue(); return this.defaultValue();
} }
@Override
public @Nullable ChatChannel byCommandName(final String channelName) {
for (final ChatChannel channel : this) {
if (channel.commandName().equalsIgnoreCase(channelName)) {
return channel;
}
}
return null;
}
} }
@@ -19,6 +19,7 @@
*/ */
package net.draycia.carbon.common.channels; package net.draycia.carbon.common.channels;
import com.google.inject.Inject;
import io.leangen.geantyref.TypeToken; import io.leangen.geantyref.TypeToken;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@@ -28,7 +29,7 @@ import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.function.Predicate; import java.util.function.Predicate;
import net.draycia.carbon.api.CarbonChatProvider; import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.SourcedAudience; import net.draycia.carbon.api.util.SourcedAudience;
@@ -53,6 +54,7 @@ import net.kyori.moonshine.Moonshine;
import net.kyori.moonshine.exception.scan.UnscannableMethodException; import net.kyori.moonshine.exception.scan.UnscannableMethodException;
import net.kyori.moonshine.strategy.StandardPlaceholderResolverStrategy; import net.kyori.moonshine.strategy.StandardPlaceholderResolverStrategy;
import net.kyori.moonshine.strategy.supertype.StandardSupertypeThenInterfaceSupertypeStrategy; import net.kyori.moonshine.strategy.supertype.StandardSupertypeThenInterfaceSupertypeStrategy;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier; import org.checkerframework.framework.qual.DefaultQualifier;
@@ -69,6 +71,8 @@ import static net.kyori.adventure.text.Component.text;
@DefaultQualifier(NonNull.class) @DefaultQualifier(NonNull.class)
public final class ConfigChatChannel implements ChatChannel { public final class ConfigChatChannel implements ChatChannel {
private transient @MonotonicNonNull @Inject CarbonChat carbonChat;
@Comment(""" @Comment("""
The channel's key, used to track the channel. The channel's key, used to track the channel.
You only need to change the second part of the key. "global" by default. You only need to change the second part of the key. "global" by default.
@@ -220,14 +224,14 @@ public final class ConfigChatChannel implements ChatChannel {
public List<Audience> recipients(final CarbonPlayer sender) { public List<Audience> recipients(final CarbonPlayer sender) {
final List<Audience> recipients = new ArrayList<>(); final List<Audience> recipients = new ArrayList<>();
for (final CarbonPlayer player : CarbonChatProvider.carbonChat().server().players()) { for (final CarbonPlayer player : this.carbonChat.server().players()) {
if (this.hearingPermitted(player).permitted()) { if (this.hearingPermitted(player).permitted()) {
recipients.add(player); recipients.add(player);
} }
} }
// console too! // console too!
recipients.add(CarbonChatProvider.carbonChat().server().console()); recipients.add(this.carbonChat.server().console());
return recipients; return recipients;
} }
@@ -265,7 +269,7 @@ public final class ConfigChatChannel implements ChatChannel {
return Moonshine.<ConfigChannelMessages, SourcedAudience>builder(new TypeToken<ConfigChannelMessages>() {}) return Moonshine.<ConfigChannelMessages, SourcedAudience>builder(new TypeToken<ConfigChannelMessages>() {})
.receiverLocatorResolver(serverReceiverResolver, 0) .receiverLocatorResolver(serverReceiverResolver, 0)
.sourced(this.messageSource) .sourced(this.messageSource)
.rendered(CarbonChatProvider.carbonChat().messageRenderer()) .rendered(this.carbonChat.messageRenderer())
.sent(carbonMessageSender) .sent(carbonMessageSender)
.resolvingWithStrategy(new StandardPlaceholderResolverStrategy<>(new StandardSupertypeThenInterfaceSupertypeStrategy(false))) .resolvingWithStrategy(new StandardPlaceholderResolverStrategy<>(new StandardSupertypeThenInterfaceSupertypeStrategy(false)))
.weightedPlaceholderResolver(Component.class, componentPlaceholderResolver, 0) .weightedPlaceholderResolver(Component.class, componentPlaceholderResolver, 0)
@@ -32,7 +32,6 @@ import net.draycia.carbon.common.command.CommandSettings;
import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.PlayerCommander; import net.draycia.carbon.common.command.PlayerCommander;
import net.draycia.carbon.common.messages.CarbonMessages; import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.util.ChannelUtils;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
@@ -78,7 +77,7 @@ public class JoinCommand extends CarbonCommand {
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.carbonMessages.commandJoinDescription()) .meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.carbonMessages.commandJoinDescription())
.handler(handler -> { .handler(handler -> {
final CarbonPlayer sender = ((PlayerCommander) handler.getSender()).carbonPlayer(); final CarbonPlayer sender = ((PlayerCommander) handler.getSender()).carbonPlayer();
final @Nullable ChatChannel channel = ChannelUtils.locateChannel(handler.get("channel")); final @Nullable ChatChannel channel = this.carbonChat.channelRegistry().byCommandName(handler.get("channel"));
if (channel == null) { if (channel == null) {
this.carbonMessages.channelNotFound(sender); this.carbonMessages.channelNotFound(sender);
return; return;
@@ -32,7 +32,6 @@ import net.draycia.carbon.common.command.CommandSettings;
import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.PlayerCommander; import net.draycia.carbon.common.command.PlayerCommander;
import net.draycia.carbon.common.messages.CarbonMessages; import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.util.ChannelUtils;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
@@ -81,7 +80,7 @@ public class LeaveCommand extends CarbonCommand {
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.carbonMessages.commandLeaveDescription()) .meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.carbonMessages.commandLeaveDescription())
.handler(handler -> { .handler(handler -> {
final CarbonPlayer sender = ((PlayerCommander) handler.getSender()).carbonPlayer(); final CarbonPlayer sender = ((PlayerCommander) handler.getSender()).carbonPlayer();
final @Nullable ChatChannel channel = ChannelUtils.locateChannel(handler.get("channel")); final @Nullable ChatChannel channel = this.carbonChat.channelRegistry().byCommandName(handler.get("channel"));
if (channel == null) { if (channel == null) {
this.carbonMessages.channelNotFound(sender); this.carbonMessages.channelNotFound(sender);
return; return;
@@ -29,9 +29,10 @@ import java.io.Reader;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.UUID; import java.util.UUID;
import net.draycia.carbon.api.CarbonChatProvider; import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.common.DataDirectory; import net.draycia.carbon.common.DataDirectory;
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
import net.draycia.carbon.common.messaging.MessagingManager; import net.draycia.carbon.common.messaging.MessagingManager;
import net.draycia.carbon.common.messaging.packets.PacketFactory; import net.draycia.carbon.common.messaging.packets.PacketFactory;
import net.draycia.carbon.common.serialisation.gson.ChatChannelSerializerGson; import net.draycia.carbon.common.serialisation.gson.ChatChannelSerializerGson;
@@ -51,6 +52,7 @@ public class JSONUserManager extends CachingUserManager {
private final Gson serializer; private final Gson serializer;
private final Path userDirectory; private final Path userDirectory;
private final ChannelRegistry channelRegistry;
@Inject @Inject
public JSONUserManager( public JSONUserManager(
@@ -61,7 +63,8 @@ public class JSONUserManager extends CachingUserManager {
final ChatChannelSerializerGson channelSerializer, final ChatChannelSerializerGson channelSerializer,
final UUIDSerializerGson uuidSerializer, final UUIDSerializerGson uuidSerializer,
final Provider<MessagingManager> messagingManager, final Provider<MessagingManager> messagingManager,
final PacketFactory packetFactory final PacketFactory packetFactory,
final CarbonChannelRegistry channelRegistry
) throws IOException { ) throws IOException {
super( super(
logger, logger,
@@ -71,6 +74,7 @@ public class JSONUserManager extends CachingUserManager {
packetFactory packetFactory
); );
this.userDirectory = dataDirectory.resolve("users"); this.userDirectory = dataDirectory.resolve("users");
this.channelRegistry = channelRegistry;
Files.createDirectories(this.userDirectory); Files.createDirectories(this.userDirectory);
@@ -98,9 +102,7 @@ public class JSONUserManager extends CachingUserManager {
throw new IllegalStateException("Player file found but was empty."); throw new IllegalStateException("Player file found but was empty.");
} }
player.leftChannels().forEach(channel -> { player.leftChannels().forEach(channel -> {
if (CarbonChatProvider.carbonChat() if (this.channelRegistry.get(channel) == null) {
.channelRegistry()
.get(channel) == null) {
player.joinChannel(channel, true); player.joinChannel(channel, true);
} }
}); });
@@ -24,7 +24,6 @@ import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.users.CarbonPlayer;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import org.checkerframework.checker.nullness.qual.Nullable;
public final class ChannelUtils { public final class ChannelUtils {
@@ -44,14 +43,4 @@ public final class ChannelUtils {
} }
} }
public static @Nullable ChatChannel locateChannel(final String channelName) {
for (final ChatChannel channel : CarbonChatProvider.carbonChat().channelRegistry()) {
if (channel.commandName().equalsIgnoreCase(channelName)) {
return channel;
}
}
return null;
}
} }
@@ -46,14 +46,17 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public class DSRVChatHook implements ChatHook { public class DSRVChatHook implements ChatHook {
private final CarbonChat carbonChat;
@Inject @Inject
public DSRVChatHook(final CarbonChat carbonChat) { public DSRVChatHook(final CarbonChat carbonChat) {
this.carbonChat = carbonChat;
final Cache<ImmutablePair<CarbonPlayer, ChatChannel>, Component> awaitingEvent = Caffeine.newBuilder() final Cache<ImmutablePair<CarbonPlayer, ChatChannel>, Component> awaitingEvent = Caffeine.newBuilder()
.expireAfterWrite(Duration.ofMillis(25)) .expireAfterWrite(Duration.ofMillis(25))
.build(); .build();
carbonChat.eventHandler().subscribe(CarbonChatEvent.class, event -> { this.carbonChat.eventHandler().subscribe(CarbonChatEvent.class, event -> {
final ChatChannel chatChannel = event.chatChannel(); final ChatChannel chatChannel = event.chatChannel();
final CarbonPlayer carbonPlayer = event.sender(); final CarbonPlayer carbonPlayer = event.sender();
final ImmutablePair<CarbonPlayer, ChatChannel> pair = new ImmutablePair<>(carbonPlayer, chatChannel); final ImmutablePair<CarbonPlayer, ChatChannel> pair = new ImmutablePair<>(carbonPlayer, chatChannel);
@@ -92,7 +95,7 @@ public class DSRVChatHook implements ChatHook {
@Override @Override
public void broadcastMessageToChannel(final String channel, final github.scarsz.discordsrv.dependencies.kyori.adventure.text.Component message) { public void broadcastMessageToChannel(final String channel, final github.scarsz.discordsrv.dependencies.kyori.adventure.text.Component message) {
final String mmFormattedMessage = MiniMessage.miniMessage().serialize(message); final String mmFormattedMessage = MiniMessage.miniMessage().serialize(message);
ChannelUtils.broadcastMessageToChannel(mmFormattedMessage, ChannelUtils.locateChannel(channel)); ChannelUtils.broadcastMessageToChannel(mmFormattedMessage, this.carbonChat.channelRegistry().byCommandName(channel));
} }
@Override @Override