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

@@ -42,7 +42,6 @@ import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;
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.ChatChannel;
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
// 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;
}
this.injector.injectMembers(chatChannel);
final @Nullable ChatChannel existingChannel = this.get(chatChannel.key());
if (existingChannel == null) {
@@ -226,6 +227,8 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
return;
}
this.injector.injectMembers(chatChannel);
if (chatChannel.shouldRegisterCommands()) {
this.registerChannelCommands(chatChannel);
}
@@ -474,4 +477,15 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
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;
import com.google.inject.Inject;
import io.leangen.geantyref.TypeToken;
import java.util.ArrayList;
import java.util.Collections;
@@ -28,7 +29,7 @@ import java.util.Objects;
import java.util.Set;
import java.util.UUID;
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.users.CarbonPlayer;
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.strategy.StandardPlaceholderResolverStrategy;
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.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -69,6 +71,8 @@ import static net.kyori.adventure.text.Component.text;
@DefaultQualifier(NonNull.class)
public final class ConfigChatChannel implements ChatChannel {
private transient @MonotonicNonNull @Inject CarbonChat carbonChat;
@Comment("""
The channel's key, used to track the channel.
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) {
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()) {
recipients.add(player);
}
}
// console too!
recipients.add(CarbonChatProvider.carbonChat().server().console());
recipients.add(this.carbonChat.server().console());
return recipients;
}
@@ -265,7 +269,7 @@ public final class ConfigChatChannel implements ChatChannel {
return Moonshine.<ConfigChannelMessages, SourcedAudience>builder(new TypeToken<ConfigChannelMessages>() {})
.receiverLocatorResolver(serverReceiverResolver, 0)
.sourced(this.messageSource)
.rendered(CarbonChatProvider.carbonChat().messageRenderer())
.rendered(this.carbonChat.messageRenderer())
.sent(carbonMessageSender)
.resolvingWithStrategy(new StandardPlaceholderResolverStrategy<>(new StandardSupertypeThenInterfaceSupertypeStrategy(false)))
.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.PlayerCommander;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.util.ChannelUtils;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -78,7 +77,7 @@ public class JoinCommand extends CarbonCommand {
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.carbonMessages.commandJoinDescription())
.handler(handler -> {
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) {
this.carbonMessages.channelNotFound(sender);
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.PlayerCommander;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.util.ChannelUtils;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -81,7 +80,7 @@ public class LeaveCommand extends CarbonCommand {
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.carbonMessages.commandLeaveDescription())
.handler(handler -> {
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) {
this.carbonMessages.channelNotFound(sender);
return;
@@ -29,9 +29,10 @@ import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
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.common.DataDirectory;
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
import net.draycia.carbon.common.messaging.MessagingManager;
import net.draycia.carbon.common.messaging.packets.PacketFactory;
import net.draycia.carbon.common.serialisation.gson.ChatChannelSerializerGson;
@@ -51,6 +52,7 @@ public class JSONUserManager extends CachingUserManager {
private final Gson serializer;
private final Path userDirectory;
private final ChannelRegistry channelRegistry;
@Inject
public JSONUserManager(
@@ -61,7 +63,8 @@ public class JSONUserManager extends CachingUserManager {
final ChatChannelSerializerGson channelSerializer,
final UUIDSerializerGson uuidSerializer,
final Provider<MessagingManager> messagingManager,
final PacketFactory packetFactory
final PacketFactory packetFactory,
final CarbonChannelRegistry channelRegistry
) throws IOException {
super(
logger,
@@ -71,6 +74,7 @@ public class JSONUserManager extends CachingUserManager {
packetFactory
);
this.userDirectory = dataDirectory.resolve("users");
this.channelRegistry = channelRegistry;
Files.createDirectories(this.userDirectory);
@@ -98,9 +102,7 @@ public class JSONUserManager extends CachingUserManager {
throw new IllegalStateException("Player file found but was empty.");
}
player.leftChannels().forEach(channel -> {
if (CarbonChatProvider.carbonChat()
.channelRegistry()
.get(channel) == null) {
if (this.channelRegistry.get(channel) == null) {
player.joinChannel(channel, true);
}
});
@@ -24,7 +24,6 @@ import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.checkerframework.checker.nullness.qual.Nullable;
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;
}
}