Finish removing relocated libraries from API

This commit is contained in:
Draycia committed 2023-07-08 17:01:25 -07:00
1 parent 09054b7332
commit 4ce7a46391
28 files changed
+251 -180

No files matched your search

@@ -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<Key, ChatChannel> {
public class CarbonChannelRegistry implements ChannelRegistry {
private static @MonotonicNonNull ObjectMapper<ConfigChatChannel> MAPPER;
@@ -104,7 +99,9 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
private final CarbonEventHandler eventHandler;
private final Provider<CarbonChat> carbonChatProvider;
private final BiMap<Key, ChatChannel> channelMap = Maps.synchronizedBiMap(HashBiMap.create());
private final Registry<Key, ChatChannel> channelRegistry = Registry.create();
//
// private final BiMap<Key, ChatChannel> 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<String> 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<Commander> 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<Key> keySet() {
return Collections.unmodifiableSet(this.channelMap.keySet());
public @NonNull Set<Key> keys() {
return Collections.unmodifiableSet(this.channelRegistry.keys());
}
@Override
public @NonNull Iterator<ChatChannel> 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();
}
}
@@ -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()) {
@@ -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;
@@ -70,7 +70,8 @@ public class LeaveCommand extends CarbonCommand {
final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
.argument(StringArgument.<Commander>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;
@@ -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()) {
@@ -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()) {
@@ -35,5 +35,4 @@ public class CancellableImpl implements Cancellable, com.seiama.event.Cancellabl
this.cancelled = cancelled;
}
}
@@ -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 <https://www.gnu.org/licenses/>.
*/
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;
}
}
@@ -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<ChatChannel> channels;
private final Set<Key> 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<ChatChannel> channels, final CarbonChannelRegistry registry) {
this.channels = channels;
public ChannelRegisterEventImpl(final Set<Key> channelKeys, final CarbonChannelRegistry registry) {
this.channelKeys = channelKeys;
this.registry = registry;
}
public Iterable<ChatChannel> channels() {
return this.channels;
@Override
public Set<Key> 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);
@@ -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;
@@ -57,7 +57,7 @@ public class ChatChannelSerializerGson extends TypeAdapter<ChatChannel> {
@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;
@@ -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() {
@@ -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) {
@@ -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;
@@ -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;
@@ -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);
}
});