From 4ce7a46391f30a33dcce9374d511993963cfa98e Mon Sep 17 00:00:00 2001 From: Draycia Date: Sat, 8 Jul 2023 17:01:25 -0700 Subject: [PATCH] Finish removing relocated libraries from API --- .../carbon/api/channels/ChannelRegistry.java | 66 +++++++++++++- .../draycia/carbon/api/event/Cancellable.java | 17 ++++ .../api/event/CarbonEventSubscriber.java | 6 ++ .../api/event/CarbonEventSubscription.java | 6 ++ .../events/CarbonChannelRegisterEvent.java | 3 +- .../event/events/CarbonPrivateChatEvent.java | 52 ++--------- .../api/util/DefaultedKeyValueRegistry.java | 42 --------- common/build.gradle.kts | 12 +-- .../channels/CarbonChannelRegistry.java | 87 ++++++++----------- .../command/commands/ContinueCommand.java | 3 +- .../common/command/commands/JoinCommand.java | 2 +- .../common/command/commands/LeaveCommand.java | 5 +- .../common/command/commands/ReplyCommand.java | 3 +- .../command/commands/WhisperCommand.java | 3 +- .../carbon/common/event/CancellableImpl.java | 1 - .../events/CarbonPrivateChatEventImpl.java | 65 ++++++++++++++ .../events/ChannelRegisterEventImpl.java | 17 ++-- .../messaging/CarbonChatPacketHandler.java | 2 +- .../gson/ChatChannelSerializerGson.java | 2 +- .../common/users/CarbonPlayerCommon.java | 4 +- .../common/users/WrappedCarbonPlayer.java | 5 +- .../users/db/mysql/MySQLUserManager.java | 2 +- .../db/postgresql/PostgreSQLUserManager.java | 2 +- .../common/users/json/JSONUserManager.java | 2 +- gradle/libs.versions.yml | 10 +-- .../carbon/paper/hooks/DSRVChatHook.java | 2 +- .../carbon/paper/hooks/PAPIChatHook.java | 2 +- .../listeners/DiscordMessageListener.java | 8 +- 28 files changed, 251 insertions(+), 180 deletions(-) delete mode 100644 api/src/main/java/net/draycia/carbon/api/util/DefaultedKeyValueRegistry.java create mode 100644 common/src/main/java/net/draycia/carbon/common/event/events/CarbonPrivateChatEventImpl.java 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 7886c25f..4a6629dc 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 @@ -19,16 +19,17 @@ */ package net.draycia.carbon.api.channels; -import net.draycia.carbon.api.util.DefaultedKeyValueRegistry; +import java.util.Set; import net.kyori.adventure.key.Key; -import net.kyori.registry.Registry; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Registry for chat channels. * * @since 2.0.0 */ -public interface ChannelRegistry extends Registry, DefaultedKeyValueRegistry { +public interface ChannelRegistry { /** * Registers ingame commands for the channel. @@ -38,4 +39,63 @@ public interface ChannelRegistry extends Registry, DefaultedKe */ void registerChannelCommands(final ChatChannel channel); + /** + * Registers the chat channel with it's key. + * + * @param channel the channel to register + * @since 2.1.0 + */ + void register(final ChatChannel channel); + + /** + * Gets the channel with the matching key. + * + * @param key the channel's key + * @return the channel + * @since 2.1.0 + */ + @Nullable ChatChannel channel(final Key key); + + /** + * Gets the channel with the matching key's value. + * + * @param value the channel's key's value + * @return the channel + * @since 2.1.0 + */ + @Nullable ChatChannel channelByValue(final String value); + + /** + * Gets the default key. + * + * @return the default key + * @since 2.1.0 + */ + @NonNull Key defaultKey(); + + /** + * Gets the default value. + * + * @return the default value + * @since 2.1.0 + */ + @NonNull ChatChannel defaultChannel(); + + /** + * Gets the list of registered channel keys. + * + * @return the registered channel keys + * @since 2.1.0 + */ + @NonNull Set keys(); + + /** + * Gets the channel with the matching key, otherwise the default channel. + * + * @param key the channel key + * @return the channel, or the default one + * @since 2.1.0 + */ + ChatChannel keyOrDefault(final Key key); + } diff --git a/api/src/main/java/net/draycia/carbon/api/event/Cancellable.java b/api/src/main/java/net/draycia/carbon/api/event/Cancellable.java index 6bc008bb..35485809 100644 --- a/api/src/main/java/net/draycia/carbon/api/event/Cancellable.java +++ b/api/src/main/java/net/draycia/carbon/api/event/Cancellable.java @@ -19,10 +19,27 @@ */ package net.draycia.carbon.api.event; +/** + * Marks an event as cancellable. + * + * @since 2.1.0 + */ public interface Cancellable { + /** + * Gets if the event is cancelled. + * + * @return if the event is cancelled + * @since 2.1.0 + */ boolean cancelled(); + /** + * Sets the cancelled state. + * + * @param cancelled sets the cancelled state + * @since 2.1.0 + */ void cancelled(final boolean cancelled); } diff --git a/api/src/main/java/net/draycia/carbon/api/event/CarbonEventSubscriber.java b/api/src/main/java/net/draycia/carbon/api/event/CarbonEventSubscriber.java index 540674fa..91050a80 100644 --- a/api/src/main/java/net/draycia/carbon/api/event/CarbonEventSubscriber.java +++ b/api/src/main/java/net/draycia/carbon/api/event/CarbonEventSubscriber.java @@ -23,6 +23,12 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.framework.qual.DefaultQualifier; import org.jetbrains.annotations.NotNull; +/** + * An EventSubscriber. + * + * @param CarbonEvent implementations + * @since 2.1.0 + */ @DefaultQualifier(NonNull.class) public interface CarbonEventSubscriber { diff --git a/api/src/main/java/net/draycia/carbon/api/event/CarbonEventSubscription.java b/api/src/main/java/net/draycia/carbon/api/event/CarbonEventSubscription.java index e0e02290..6081c08b 100644 --- a/api/src/main/java/net/draycia/carbon/api/event/CarbonEventSubscription.java +++ b/api/src/main/java/net/draycia/carbon/api/event/CarbonEventSubscription.java @@ -22,6 +22,12 @@ package net.draycia.carbon.api.event; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.framework.qual.DefaultQualifier; +/** + * An EventSubscription. + * + * @param CarbonEvent implementations + * @since 2.1.0 + */ @DefaultQualifier(NonNull.class) public interface CarbonEventSubscription { diff --git a/api/src/main/java/net/draycia/carbon/api/event/events/CarbonChannelRegisterEvent.java b/api/src/main/java/net/draycia/carbon/api/event/events/CarbonChannelRegisterEvent.java index f210e429..12f0531e 100644 --- a/api/src/main/java/net/draycia/carbon/api/event/events/CarbonChannelRegisterEvent.java +++ b/api/src/main/java/net/draycia/carbon/api/event/events/CarbonChannelRegisterEvent.java @@ -19,6 +19,7 @@ */ package net.draycia.carbon.api.event.events; +import java.util.Set; import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.event.CarbonEvent; import net.kyori.adventure.key.Key; @@ -39,7 +40,7 @@ public interface CarbonChannelRegisterEvent extends CarbonEvent { * @return the registered channels * @since 2.1.0 */ - Iterable channels(); + Set channelKeys(); /** * Registers additional channels to the registry. Does not re-emit the event. diff --git a/api/src/main/java/net/draycia/carbon/api/event/events/CarbonPrivateChatEvent.java b/api/src/main/java/net/draycia/carbon/api/event/events/CarbonPrivateChatEvent.java index 09242b4a..3f7eb350 100644 --- a/api/src/main/java/net/draycia/carbon/api/event/events/CarbonPrivateChatEvent.java +++ b/api/src/main/java/net/draycia/carbon/api/event/events/CarbonPrivateChatEvent.java @@ -19,8 +19,7 @@ */ package net.draycia.carbon.api.event.events; -import com.seiama.event.Cancellable; -import java.util.Objects; +import net.draycia.carbon.api.event.Cancellable; import net.draycia.carbon.api.event.CarbonEvent; import net.draycia.carbon.api.users.CarbonPlayer; import net.kyori.adventure.text.Component; @@ -33,28 +32,7 @@ import org.checkerframework.framework.qual.DefaultQualifier; * @since 2.1.0 */ @DefaultQualifier(NonNull.class) -public class CarbonPrivateChatEvent implements CarbonEvent, Cancellable { - - private final CarbonPlayer sender; - private final CarbonPlayer recipient; - - private Component message; - - private boolean cancelled = false; - - /** - * Called whenever a player privately messages another player. - * - * @param sender the message sender - * @param recipient the message recipient - * @param message the message - * @since 2.1.0 - */ - public CarbonPrivateChatEvent(final CarbonPlayer sender, final CarbonPlayer recipient, final Component message) { - this.sender = sender; - this.recipient = recipient; - this.message = message; - } +public interface CarbonPrivateChatEvent extends CarbonEvent, Cancellable { /** * Sets the message that will be sent. @@ -63,9 +41,7 @@ public class CarbonPrivateChatEvent implements CarbonEvent, Cancellable { * @throws NullPointerException if message is null * @since 2.1.0 */ - public void message(final Component message) { - this.message = Objects.requireNonNull(message); - } + void message(final Component message); /** * The message that will be sent. @@ -73,9 +49,7 @@ public class CarbonPrivateChatEvent implements CarbonEvent, Cancellable { * @return the message * @since 2.1.0 */ - public Component message() { - return this.message; - } + Component message(); /** * The message sender. @@ -83,9 +57,7 @@ public class CarbonPrivateChatEvent implements CarbonEvent, Cancellable { * @return the sender of the message * @since 2.1.0 */ - public CarbonPlayer sender() { - return this.sender; - } + CarbonPlayer sender(); /** * The message recipient. @@ -93,18 +65,6 @@ public class CarbonPrivateChatEvent implements CarbonEvent, Cancellable { * @return the recipient of the message * @since 2.1.0 */ - public CarbonPlayer recipient() { - return this.recipient; - } - - @Override - public boolean cancelled() { - return this.cancelled; - } - - @Override - public void cancelled(final boolean cancelled) { - this.cancelled = cancelled; - } + CarbonPlayer recipient(); } diff --git a/api/src/main/java/net/draycia/carbon/api/util/DefaultedKeyValueRegistry.java b/api/src/main/java/net/draycia/carbon/api/util/DefaultedKeyValueRegistry.java deleted file mode 100644 index 79e249a0..00000000 --- a/api/src/main/java/net/draycia/carbon/api/util/DefaultedKeyValueRegistry.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * CarbonChat - * - * Copyright (c) 2023 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.util; - -import net.kyori.registry.DefaultedRegistryGetter; -import org.checkerframework.checker.nullness.qual.NonNull; - -/** - * An extension of the DefaultedRegistryGetter that declares a defaultValue method. - * - * @param the key type - * @param the value type - * @since 1.0.0 - */ -public interface DefaultedKeyValueRegistry extends DefaultedRegistryGetter { - - /** - * Gets the default value. - * - * @return the default value - * @since 1.0.0 - */ - @NonNull V defaultValue(); - -} diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 73826f33..2cf45118 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -1,3 +1,5 @@ +import org.gradle.internal.impldep.org.junit.experimental.categories.Categories.CategoryFilter.exclude + plugins { id("carbon.base-conventions") } @@ -44,13 +46,13 @@ dependencies { api(libs.messengerRedis) compileOnlyApi(libs.netty) - implementation(libs.event) - implementation(libs.kyoriRegistry) { + api(libs.event) + api(libs.registry) { exclude("com.google.guava") } - implementation(libs.kyoriMoonshine) - implementation(libs.kyoriMoonshineCore) - implementation(libs.kyoriMoonshineStandard) + api(libs.kyoriMoonshine) + api(libs.kyoriMoonshineCore) + api(libs.kyoriMoonshineStandard) compileOnlyApi(libs.jarRelocator) compileOnlyApi(libs.caffeine) 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 9c6e1364..20750805 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 @@ -22,22 +22,18 @@ package net.draycia.carbon.common.channels; import cloud.commandframework.Command; 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.Provider; import com.google.inject.Singleton; import com.google.inject.TypeLiteral; +import com.seiama.registry.Registry; import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; -import java.util.Iterator; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -65,7 +61,6 @@ import net.kyori.adventure.audience.Audience; import net.kyori.adventure.identity.Identity; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; -import net.kyori.registry.DefaultedRegistryGetter; import org.apache.logging.log4j.Logger; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.NonNull; @@ -83,7 +78,7 @@ import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer; @Singleton @DefaultQualifier(NonNull.class) -public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistryGetter { +public class CarbonChannelRegistry implements ChannelRegistry { private static @MonotonicNonNull ObjectMapper MAPPER; @@ -104,7 +99,9 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry private final CarbonEventHandler eventHandler; private final Provider carbonChatProvider; - private final BiMap channelMap = Maps.synchronizedBiMap(HashBiMap.create()); + private final Registry channelRegistry = Registry.create(); + // + // private final BiMap channelMap = Maps.synchronizedBiMap(HashBiMap.create()); @Inject public CarbonChannelRegistry( @@ -188,13 +185,13 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry this.injector.injectMembers(chatChannel); - final @Nullable ChatChannel existingChannel = this.get(chatChannel.key()); + final @Nullable ChatChannel existingChannel = this.channel(chatChannel.key()); if (existingChannel == null) { return; } - this.register(chatChannel.key(), chatChannel); + this.register(chatChannel); }); } catch (final IOException exception) { exception.printStackTrace(); @@ -209,9 +206,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry this.registerDefaultChannel(); return; } else if (this.isPathEmpty(this.configChannelDir)) { - final ChatChannel basicChannel = this.injector.getInstance(BasicChatChannel.class); - - this.register(basicChannel.key(), basicChannel); + this.register(this.injector.getInstance(BasicChatChannel.class)); } // otherwise, register all channels found @@ -237,14 +232,14 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry } }); - if (!this.channelMap.containsKey(this.defaultKey)) { + if (this.channel(this.defaultKey) == null) { this.logger.warn("No default channel found! Default channel key: [" + this.defaultKey().asString() + "]"); } final List channelList = new ArrayList<>(); - for (final ChatChannel chatChannel : this.channelMap.values()) { - channelList.add(chatChannel.key().asString()); + for (final Key key : this.keys()) { + channelList.add(key.asString()); } final String channels = String.join(", ", channelList); @@ -254,7 +249,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry exception.printStackTrace(); } - this.eventHandler.emit(new ChannelRegisterEventImpl(this.channelMap.values(), this)); + this.eventHandler.emit(new ChannelRegisterEventImpl(this.keys(), this)); } public @Nullable ChatChannel loadChannel(final Path channelFile) { @@ -285,7 +280,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry node.set(ConfigChatChannel.class, configChannel); loader.save(node); - this.register(configChannel.key(), configChannel); + this.register(configChannel); } catch (final IOException exception) { exception.printStackTrace(); } @@ -304,7 +299,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry this.logger.info("Default channel is [" + channelKey + "]"); } - this.register(channelKey, channel); + this.register(channel); return channel; } @@ -390,7 +385,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry final Command command = builder.senderType(PlayerCommander.class) .handler(handler -> { final CarbonPlayer sender = ((PlayerCommander) handler.getSender()).carbonPlayer(); - final @Nullable ChatChannel chatChannel = this.get(channelKey); + final @Nullable ChatChannel chatChannel = this.channel(channelKey); if (sender.muted()) { this.carbonMessages.muteCannotSpeak(sender); @@ -423,34 +418,38 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry } @Override - public @NonNull ChatChannel register(final @NonNull Key key, final @NonNull ChatChannel value) { - this.channelMap.put(key, value); - return value; + public void register(final ChatChannel channel) { + this.channelRegistry.register(channel.key(), channel); } @Override - public @Nullable ChatChannel get(final @NonNull Key key) { - return this.channelMap.get(key); + public @Nullable ChatChannel channel(final Key key) { + return this.channelRegistry.getOrCreateHolder(key).value(); } @Override - public @Nullable Key key(final @NonNull ChatChannel value) { - return this.channelMap.inverse().get(value); + public @Nullable ChatChannel channelByValue(final String value) { + if (value.contains(":")) { + return this.channel(Key.key(value)); + } + + for (final Key key : this.keys()) { + if (key.value().equalsIgnoreCase(value)) { + return this.channel(key); + } + } + + return null; } @Override - public @NonNull Set keySet() { - return Collections.unmodifiableSet(this.channelMap.keySet()); + public @NonNull Set keys() { + return Collections.unmodifiableSet(this.channelRegistry.keys()); } @Override - public @NonNull Iterator iterator() { - return Iterators.unmodifiableIterator(this.channelMap.values().iterator()); - } - - @Override - public ChatChannel defaultValue() { - return Objects.requireNonNull(this.get(this.defaultKey)); + public ChatChannel defaultChannel() { + return Objects.requireNonNull(this.channel(this.defaultKey)); } @Override @@ -459,24 +458,14 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry } @Override - public ChatChannel getOrDefault(final Key key) { - final @Nullable ChatChannel channel = this.get(key); + public ChatChannel keyOrDefault(final Key key) { + final @Nullable ChatChannel channel = this.channel(key); if (channel != null) { return channel; } - return this.defaultValue(); - } - - public @Nullable ChatChannel byCommandName(final String channelName) { - for (final ChatChannel channel : this) { - if (channel.commandName().equalsIgnoreCase(channelName)) { - return channel; - } - } - - return null; + return this.defaultChannel(); } } diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/ContinueCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/ContinueCommand.java index 1b845020..863bca1d 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/ContinueCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/ContinueCommand.java @@ -35,6 +35,7 @@ import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.command.PlayerCommander; import net.draycia.carbon.common.command.argument.CarbonPlayerArgument; import net.draycia.carbon.common.config.ConfigFactory; +import net.draycia.carbon.common.event.events.CarbonPrivateChatEventImpl; import net.draycia.carbon.common.messages.CarbonMessages; import net.draycia.carbon.common.util.CloudUtils; import net.kyori.adventure.key.Key; @@ -129,7 +130,7 @@ public class ContinueCommand extends CarbonCommand { final Component senderName = CarbonPlayer.renderName(sender); final Component recipientName = CarbonPlayer.renderName(recipient); - final CarbonPrivateChatEvent privateChatEvent = new CarbonPrivateChatEvent(sender, recipient, Component.text(message)); + final CarbonPrivateChatEvent privateChatEvent = new CarbonPrivateChatEventImpl(sender, recipient, Component.text(message)); this.carbonChat.eventHandler().emit(privateChatEvent); if (privateChatEvent.cancelled()) { diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/JoinCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/JoinCommand.java index 4acd7fb1..28b990da 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/JoinCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/JoinCommand.java @@ -77,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 = this.channelRegistry.byCommandName(handler.get("channel")); + final @Nullable ChatChannel channel = this.channelRegistry.channelByValue(handler.get("channel")); if (channel == null) { this.carbonMessages.channelNotFound(sender); return; diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/LeaveCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/LeaveCommand.java index 3b66af2f..cb16cecf 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/LeaveCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/LeaveCommand.java @@ -70,7 +70,8 @@ public class LeaveCommand extends CarbonCommand { final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases()) .argument(StringArgument.builder("channel").greedy().withSuggestionsProvider((context, s) -> { final CarbonPlayer sender = ((PlayerCommander) context.getSender()).carbonPlayer(); - return this.channelRegistry.stream() + return this.channelRegistry.keys().stream() + .map(this.channelRegistry::channel) .filter(x -> !sender.leftChannels().contains(x.key()) && x.speechPermitted(sender).permitted()) .map(x -> x.key().value()) .toList(); @@ -80,7 +81,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 = this.channelRegistry.byCommandName(handler.get("channel")); + final @Nullable ChatChannel channel = this.channelRegistry.channelByValue(handler.get("channel")); if (channel == null) { this.carbonMessages.channelNotFound(sender); return; diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/ReplyCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/ReplyCommand.java index fb092409..6f98ca4b 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/ReplyCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/ReplyCommand.java @@ -35,6 +35,7 @@ import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.command.PlayerCommander; import net.draycia.carbon.common.command.argument.CarbonPlayerArgument; import net.draycia.carbon.common.config.ConfigFactory; +import net.draycia.carbon.common.event.events.CarbonPrivateChatEventImpl; import net.draycia.carbon.common.messages.CarbonMessages; import net.draycia.carbon.common.util.CloudUtils; import net.kyori.adventure.key.Key; @@ -128,7 +129,7 @@ public class ReplyCommand extends CarbonCommand { final Component senderName = CarbonPlayer.renderName(sender); final Component recipientName = CarbonPlayer.renderName(recipient); - final CarbonPrivateChatEvent privateChatEvent = new CarbonPrivateChatEvent(sender, recipient, Component.text(message)); + final CarbonPrivateChatEvent privateChatEvent = new CarbonPrivateChatEventImpl(sender, recipient, Component.text(message)); this.carbonChat.eventHandler().emit(privateChatEvent); if (privateChatEvent.cancelled()) { diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/WhisperCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/WhisperCommand.java index 29160914..f1846eba 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/WhisperCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/WhisperCommand.java @@ -35,6 +35,7 @@ import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.command.PlayerCommander; import net.draycia.carbon.common.command.argument.CarbonPlayerArgument; import net.draycia.carbon.common.config.ConfigFactory; +import net.draycia.carbon.common.event.events.CarbonPrivateChatEventImpl; import net.draycia.carbon.common.messages.CarbonMessages; import net.draycia.carbon.common.util.CloudUtils; import net.kyori.adventure.key.Key; @@ -125,7 +126,7 @@ public class WhisperCommand extends CarbonCommand { final Component senderName = CarbonPlayer.renderName(sender); final Component recipientName = CarbonPlayer.renderName(recipient); - final CarbonPrivateChatEvent privateChatEvent = new CarbonPrivateChatEvent(sender, recipient, Component.text(message)); + final CarbonPrivateChatEvent privateChatEvent = new CarbonPrivateChatEventImpl(sender, recipient, Component.text(message)); this.carbonChat.eventHandler().emit(privateChatEvent); if (privateChatEvent.cancelled()) { diff --git a/common/src/main/java/net/draycia/carbon/common/event/CancellableImpl.java b/common/src/main/java/net/draycia/carbon/common/event/CancellableImpl.java index b5fa64cb..e8ccbd54 100644 --- a/common/src/main/java/net/draycia/carbon/common/event/CancellableImpl.java +++ b/common/src/main/java/net/draycia/carbon/common/event/CancellableImpl.java @@ -35,5 +35,4 @@ public class CancellableImpl implements Cancellable, com.seiama.event.Cancellabl this.cancelled = cancelled; } - } diff --git a/common/src/main/java/net/draycia/carbon/common/event/events/CarbonPrivateChatEventImpl.java b/common/src/main/java/net/draycia/carbon/common/event/events/CarbonPrivateChatEventImpl.java new file mode 100644 index 00000000..4141ad89 --- /dev/null +++ b/common/src/main/java/net/draycia/carbon/common/event/events/CarbonPrivateChatEventImpl.java @@ -0,0 +1,65 @@ +/* + * CarbonChat + * + * Copyright (c) 2023 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.event.events; + +import java.util.Objects; +import net.draycia.carbon.api.event.events.CarbonPrivateChatEvent; +import net.draycia.carbon.api.users.CarbonPlayer; +import net.draycia.carbon.common.event.CancellableImpl; +import net.kyori.adventure.text.Component; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.framework.qual.DefaultQualifier; + +/** + * Called whenever a player privately messages another player. + * + * @since 2.1.0 + */ +@DefaultQualifier(NonNull.class) +public class CarbonPrivateChatEventImpl extends CancellableImpl implements CarbonPrivateChatEvent { + + private final CarbonPlayer sender; + private final CarbonPlayer recipient; + + private Component message; + + public CarbonPrivateChatEventImpl(final CarbonPlayer sender, final CarbonPlayer recipient, final Component message) { + this.sender = sender; + this.recipient = recipient; + this.message = message; + } + + public void message(final Component message) { + this.message = Objects.requireNonNull(message); + } + + public Component message() { + return this.message; + } + + public CarbonPlayer sender() { + return this.sender; + } + + public CarbonPlayer recipient() { + return this.recipient; + } + +} diff --git a/common/src/main/java/net/draycia/carbon/common/event/events/ChannelRegisterEventImpl.java b/common/src/main/java/net/draycia/carbon/common/event/events/ChannelRegisterEventImpl.java index c3a78f04..1f8342b7 100644 --- a/common/src/main/java/net/draycia/carbon/common/event/events/ChannelRegisterEventImpl.java +++ b/common/src/main/java/net/draycia/carbon/common/event/events/ChannelRegisterEventImpl.java @@ -19,6 +19,7 @@ */ package net.draycia.carbon.common.event.events; +import java.util.Set; import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.event.CarbonEvent; import net.draycia.carbon.api.event.events.CarbonChannelRegisterEvent; @@ -35,26 +36,28 @@ import org.checkerframework.framework.qual.DefaultQualifier; @DefaultQualifier(NonNull.class) public class ChannelRegisterEventImpl implements CarbonChannelRegisterEvent { - private final Iterable channels; + private final Set channelKeys; private final CarbonChannelRegistry registry; /** * {@link CarbonEvent} that's called when channels are registered. * - * @param channels the channels that were registered + * @param channelKeys the channels that were registered * @since 2.1.0 */ - public ChannelRegisterEventImpl(final Iterable channels, final CarbonChannelRegistry registry) { - this.channels = channels; + public ChannelRegisterEventImpl(final Set channelKeys, final CarbonChannelRegistry registry) { + this.channelKeys = channelKeys; this.registry = registry; } - public Iterable channels() { - return this.channels; + @Override + public Set channelKeys() { + return this.channelKeys; } + @Override public void register(final Key key, final ChatChannel channel, final boolean registerCommands) { - this.registry.register(key, channel); + this.registry.register(channel); if (registerCommands) { this.registry.registerChannelCommands(channel); diff --git a/common/src/main/java/net/draycia/carbon/common/messaging/CarbonChatPacketHandler.java b/common/src/main/java/net/draycia/carbon/common/messaging/CarbonChatPacketHandler.java index 92dab365..711011f7 100644 --- a/common/src/main/java/net/draycia/carbon/common/messaging/CarbonChatPacketHandler.java +++ b/common/src/main/java/net/draycia/carbon/common/messaging/CarbonChatPacketHandler.java @@ -68,7 +68,7 @@ public final class CarbonChatPacketHandler extends AbstractMessagingHandler { final CarbonPlayer sender = this.carbonChat.userManager().user(messagePacket.userId()).join(); - final @Nullable ChatChannel channel = this.carbonChat.channelRegistry().get(messagePacket.channelKey()); + final @Nullable ChatChannel channel = this.carbonChat.channelRegistry().channel(messagePacket.channelKey()); if (channel == null) { return false; diff --git a/common/src/main/java/net/draycia/carbon/common/serialisation/gson/ChatChannelSerializerGson.java b/common/src/main/java/net/draycia/carbon/common/serialisation/gson/ChatChannelSerializerGson.java index 22e88749..9ccde4e9 100644 --- a/common/src/main/java/net/draycia/carbon/common/serialisation/gson/ChatChannelSerializerGson.java +++ b/common/src/main/java/net/draycia/carbon/common/serialisation/gson/ChatChannelSerializerGson.java @@ -57,7 +57,7 @@ public class ChatChannelSerializerGson extends TypeAdapter { @Subst("namespace:value") final @Nullable String channelName = in.nextString(); if (channelName != null) { - return this.registry.get(key(channelName)); + return this.registry.channel(key(channelName)); } return null; 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 e692acdb..092c4b4e 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 @@ -351,10 +351,10 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing @Override public @Nullable ChatChannel selectedChannel() { if (!this.selectedChannel.hasValue()) { - return this.channelRegistry.defaultValue(); + return this.channelRegistry.defaultChannel(); } - return this.channelRegistry.get(this.selectedChannel.get()); + return this.channelRegistry.channel(this.selectedChannel.get()); } public ChannelRegistry channelRegistry() { diff --git a/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java b/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java index 76d2ef62..aba5e412 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java +++ b/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java @@ -241,9 +241,10 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer { final String text = PlainTextComponentSerializer.plainText().serialize(message); Component formattedMessage = message; - ChatChannel channel = requireNonNullElse(this.selectedChannel(), this.carbonPlayerCommon().channelRegistry().defaultValue()); + ChatChannel channel = requireNonNullElse(this.selectedChannel(), this.carbonPlayerCommon().channelRegistry().defaultChannel()); - for (final ChatChannel chatChannel : this.carbonPlayerCommon.channelRegistry()) { + for (final Key channelKey : this.carbonPlayerCommon.channelRegistry().keys()) { + final @MonotonicNonNull ChatChannel chatChannel = this.carbonPlayerCommon.channelRegistry().channel(channelKey); final @MonotonicNonNull String prefix = chatChannel.quickPrefix(); if (prefix == null) { diff --git a/common/src/main/java/net/draycia/carbon/common/users/db/mysql/MySQLUserManager.java b/common/src/main/java/net/draycia/carbon/common/users/db/mysql/MySQLUserManager.java index d02c0f31..2c0e3f97 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/db/mysql/MySQLUserManager.java +++ b/common/src/main/java/net/draycia/carbon/common/users/db/mysql/MySQLUserManager.java @@ -98,7 +98,7 @@ public final class MySQLUserManager extends DatabaseUserManager { .bind("id", uuid) .mapTo(Key.class) .forEach(channel -> { - final @Nullable ChatChannel chatChannel = this.channelRegistry.get(channel); + final @Nullable ChatChannel chatChannel = this.channelRegistry.channel(channel); if (chatChannel == null) { return; diff --git a/common/src/main/java/net/draycia/carbon/common/users/db/postgresql/PostgreSQLUserManager.java b/common/src/main/java/net/draycia/carbon/common/users/db/postgresql/PostgreSQLUserManager.java index 361c346f..32c69f02 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/db/postgresql/PostgreSQLUserManager.java +++ b/common/src/main/java/net/draycia/carbon/common/users/db/postgresql/PostgreSQLUserManager.java @@ -101,7 +101,7 @@ public final class PostgreSQLUserManager extends DatabaseUserManager { .bind("id", uuid) .mapTo(Key.class) .forEach(channel -> { - final @Nullable ChatChannel chatChannel = this.channelRegistry.get(channel); + final @Nullable ChatChannel chatChannel = this.channelRegistry.channel(channel); if (chatChannel == null) { return; diff --git a/common/src/main/java/net/draycia/carbon/common/users/json/JSONUserManager.java b/common/src/main/java/net/draycia/carbon/common/users/json/JSONUserManager.java index 50adba96..f77c01cc 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/json/JSONUserManager.java +++ b/common/src/main/java/net/draycia/carbon/common/users/json/JSONUserManager.java @@ -102,7 +102,7 @@ public class JSONUserManager extends CachingUserManager { throw new IllegalStateException("Player file found but was empty."); } player.leftChannels().forEach(channel -> { - if (this.channelRegistry.get(channel) == null) { + if (this.channelRegistry.channel(channel) == null) { player.joinChannel(channel, true); } }); diff --git a/gradle/libs.versions.yml b/gradle/libs.versions.yml index 6546db9a..93636913 100644 --- a/gradle/libs.versions.yml +++ b/gradle/libs.versions.yml @@ -29,7 +29,7 @@ versions: paperTrail: 0.0.1-SNAPSHOT foliaApi: 1.19.4-R0.1-SNAPSHOT event: 1.0.0-SNAPSHOT - kyoriRegistry: 1.0.0-SNAPSHOT + registry: 1.0.0-SNAPSHOT kyoriMoonshine: 2.0.4 log4j: 2.17.2 guice: 5.1.0 @@ -107,11 +107,11 @@ dependencies: group: com.seiama name: event-api version: { ref: event } - - kyoriRegistry: - group: net.kyori + registry: + group: com.seiama name: registry - version: { ref: kyoriRegistry } + version: { ref: registry } + kyoriMoonshine: group: net.kyori.moonshine name: moonshine diff --git a/paper/src/main/java/net/draycia/carbon/paper/hooks/DSRVChatHook.java b/paper/src/main/java/net/draycia/carbon/paper/hooks/DSRVChatHook.java index a8699fbb..890c52ba 100644 --- a/paper/src/main/java/net/draycia/carbon/paper/hooks/DSRVChatHook.java +++ b/paper/src/main/java/net/draycia/carbon/paper/hooks/DSRVChatHook.java @@ -111,7 +111,7 @@ public final class DSRVChatHook implements ChatHook { @Override public void broadcastMessageToChannel(final String channel, final github.scarsz.discordsrv.dependencies.kyori.adventure.text.Component message) { final String mmFormattedMessage = MiniMessage.miniMessage().serialize(message); - final ChatChannel chatChannel = this.channelRegistry.byCommandName(channel); + final ChatChannel chatChannel = this.channelRegistry.channelByValue(channel); if (chatChannel == null) { this.plugin.getLogger().warning("Error sending message from Discord to Minecraft, no matching channel found for [" + channel + "]"); diff --git a/paper/src/main/java/net/draycia/carbon/paper/hooks/PAPIChatHook.java b/paper/src/main/java/net/draycia/carbon/paper/hooks/PAPIChatHook.java index 882413af..2c2710d3 100644 --- a/paper/src/main/java/net/draycia/carbon/paper/hooks/PAPIChatHook.java +++ b/paper/src/main/java/net/draycia/carbon/paper/hooks/PAPIChatHook.java @@ -22,9 +22,9 @@ package net.draycia.carbon.paper.hooks; import com.google.inject.Inject; import me.clip.placeholderapi.PlaceholderAPI; import net.draycia.carbon.api.event.CarbonEventHandler; -import net.draycia.carbon.common.util.ColorUtils; import net.draycia.carbon.common.event.events.CarbonEarlyChatEvent; import net.draycia.carbon.common.listeners.Listener; +import net.draycia.carbon.common.util.ColorUtils; import net.draycia.carbon.paper.CarbonChatPaper; import net.draycia.carbon.paper.users.CarbonPlayerPaper; import org.checkerframework.checker.nullness.qual.NonNull; diff --git a/paper/src/main/java/net/draycia/carbon/paper/listeners/DiscordMessageListener.java b/paper/src/main/java/net/draycia/carbon/paper/listeners/DiscordMessageListener.java index 48227136..ebf703b7 100644 --- a/paper/src/main/java/net/draycia/carbon/paper/listeners/DiscordMessageListener.java +++ b/paper/src/main/java/net/draycia/carbon/paper/listeners/DiscordMessageListener.java @@ -64,7 +64,7 @@ public final class DiscordMessageListener implements Listener { var channel = result.selectedChannel(); if (channel == null) { - channel = this.carbonChat.channelRegistry().defaultValue(); + channel = this.carbonChat.channelRegistry().defaultChannel(); } final var messageType = this.channelMessageTypes.get(channel.key()); @@ -80,10 +80,10 @@ public final class DiscordMessageListener implements Listener { final @Nullable DiscordService discord = Bukkit.getServicesManager().load(DiscordService.class); if (discord != null) { - this.carbonChat.channelRegistry().forEach(channel -> { - final MessageType channelMessageType = new MessageType(channel.key().value()); + this.carbonChat.channelRegistry().keys().forEach(key -> { + final MessageType channelMessageType = new MessageType(key.value()); discord.registerMessageType(this.plugin, channelMessageType); - this.channelMessageTypes.put(channel.key(), channelMessageType); + this.channelMessageTypes.put(key, channelMessageType); }); } }