Add ChannelRegistry#channelOrThrow, rename ChannelRegistry#keyOrDefault to channelOrDefault

This commit is contained in:
Jason Penilla committed 2023-08-10 13:41:21 -07:00
1 parent 7f28a9d876
commit 40884685f8
4 files changed
+38 -14

No files matched your search

@@ -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<Key> 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
@@ -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<ChatChannel> 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<Key> action) {
for (final Key key : this.channelRegistry.keys()) {
@@ -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();
}
@@ -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;