From 40884685f80d0a5cd13571c1c85b36191dcca2d5 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Thu, 10 Aug 2023 13:34:52 -0700 Subject: [PATCH] Add ChannelRegistry#channelOrThrow, rename ChannelRegistry#keyOrDefault to channelOrDefault --- .../carbon/api/channels/ChannelRegistry.java | 26 ++++++++++++++----- .../channels/CarbonChannelRegistry.java | 16 ++++++++++-- .../common/users/CarbonPlayerCommon.java | 3 +-- .../common/users/WrappedCarbonPlayer.java | 7 +++-- 4 files changed, 38 insertions(+), 14 deletions(-) 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 c8d096b6..b314be73 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,6 +19,7 @@ */ package net.draycia.carbon.api.channels; +import java.util.NoSuchElementException; import java.util.Set; import java.util.function.Consumer; import net.kyori.adventure.key.Key; @@ -26,7 +27,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; /** - * Registry for chat channels. + * Registry for {@link ChatChannel chat channels}. * * @since 2.0.0 */ @@ -43,7 +44,8 @@ public interface ChannelRegistry { void register(ChatChannel channel); /** - * Gets the channel with the matching key. + * Retrieve a channel by its key. If there is no matching channel, + * returns {@code null}. * * @param key the channel's key * @return the channel @@ -52,7 +54,7 @@ public interface ChannelRegistry { @Nullable ChatChannel channel(Key key); /** - * Gets the default key. + * Gets the key for the default channel. * * @return the default key * @since 2.1.0 @@ -60,7 +62,7 @@ public interface ChannelRegistry { @NonNull Key defaultKey(); /** - * Gets the default value. + * Gets the default channel. * * @return the default value * @since 2.1.0 @@ -76,13 +78,25 @@ public interface ChannelRegistry { @NonNull Set keys(); /** - * Gets the channel with the matching key, otherwise the default channel. + * Retrieve a channel by its key. If there is no matching channel, + * returns {@link #defaultChannel() the default channel}. * * @param key the channel key * @return the channel, or the default one * @since 2.1.0 */ - ChatChannel keyOrDefault(Key key); + ChatChannel channelOrDefault(Key key); + + /** + * Retrieve a channel by its key. If there is no matching channel, + * throws {@link NoSuchElementException}. + * + * @param key channel key + * @return channel + * @throws NoSuchElementException when no matching channel is found + * @since 2.1.0 + */ + ChatChannel channelOrThrow(Key key); /** * The provided action will be executed immediately for all currently registered 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 8876fe2d..a66bfb64 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 @@ -26,6 +26,7 @@ import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.Singleton; import com.google.inject.TypeLiteral; +import com.seiama.registry.Holder; import com.seiama.registry.Registry; import java.io.IOException; import java.nio.file.Path; @@ -33,6 +34,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.NoSuchElementException; import java.util.Objects; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -406,7 +408,8 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann @Override public @Nullable ChatChannel channel(final Key key) { - return this.channelRegistry.getOrCreateHolder(key).value(); + final @Nullable Holder holder = this.channelRegistry.getHolder(key); + return holder == null ? null : holder.value(); } public @Nullable ChatChannel channelByValue(final String value) { @@ -439,7 +442,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann } @Override - public ChatChannel keyOrDefault(final Key key) { + public ChatChannel channelOrDefault(final Key key) { final @Nullable ChatChannel channel = this.channel(key); if (channel != null) { @@ -449,6 +452,15 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann return this.defaultChannel(); } + @Override + public ChatChannel channelOrThrow(final Key key) { + final @Nullable ChatChannel channel = this.channel(key); + if (channel != null) { + return channel; + } + throw new NoSuchElementException("No channel registered with key '" + key.asString() + "'"); + } + @Override public void allKeys(final Consumer action) { for (final Key key : this.channelRegistry.keys()) { 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 558f335e..036ec65a 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 @@ -44,7 +44,6 @@ 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; -import org.jetbrains.annotations.NotNull; @DefaultQualifier(NonNull.class) public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Single { @@ -154,7 +153,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing } @Override - public @NotNull Audience audience() { + public Audience audience() { return Audience.empty(); } 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 7d721c80..9262577b 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 @@ -42,7 +42,6 @@ import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.luckperms.api.LuckPermsProvider; import net.luckperms.api.model.user.User; import net.luckperms.api.util.Tristate; -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; @@ -256,11 +255,11 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer { final String text = PlainTextComponentSerializer.plainText().serialize(message); Component formattedMessage = message; - ChatChannel channel = requireNonNullElse(this.selectedChannel(), this.carbonPlayerCommon().channelRegistry().defaultChannel()); + ChatChannel channel = requireNonNullElse(this.selectedChannel(), this.carbonPlayerCommon.channelRegistry().defaultChannel()); for (final Key channelKey : this.carbonPlayerCommon.channelRegistry().keys()) { - final @MonotonicNonNull ChatChannel chatChannel = this.carbonPlayerCommon.channelRegistry().channel(channelKey); - final @MonotonicNonNull String prefix = chatChannel.quickPrefix(); + final ChatChannel chatChannel = this.carbonPlayerCommon.channelRegistry().channelOrThrow(channelKey); + final @Nullable String prefix = chatChannel.quickPrefix(); if (prefix == null) { continue;