API cleanup & other various refactoring

This commit is contained in:
Jason Penilla committed 2023-08-09 23:50:20 -07:00
1 parent cdb9d618f9
commit 1267b3bd4b
58 files changed
+340 -614

No files matched your search

-2
View File
@@ -7,7 +7,6 @@ description = "API for interfacing with the CarbonChat Minecraft mod/plugin"
dependencies {
// Doesn't add any dependencies, only version constraints
api(platform(libs.adventureBom))
api(platform(libs.log4jBom))
// Provided by platform
compileOnlyApi(libs.adventureApi)
@@ -22,5 +21,4 @@ dependencies {
// Provided by Minecraft
compileOnlyApi(libs.gson)
compileOnlyApi(libs.log4jApi)
}
@@ -19,17 +19,16 @@
*/
package net.draycia.carbon.api;
import java.nio.file.Path;
import java.util.UUID;
import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.event.CarbonEvent;
import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.users.UserManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
/**
* The main plugin interface.
* The {@link CarbonChat} interface is the gateway to interacting with the majority of the CarbonChat API.
*
* <p>Instances may be obtained through {@link CarbonChatProvider#carbonChat()} once Carbon is loaded.</p>
* <p>On most platforms, you should use the provided load order mechanism to ensure your addon loads after
@@ -51,25 +50,8 @@ public interface CarbonChat {
UUID serverId();
/**
* The plugin's logger.<br>
* All messages will be logged through this.
*
* @return the plugin's logger
* @since 2.0.0
*/
Logger logger();
/**
* The plugin's data storage directory.<br>
* This is where configs and misc files will be stored.
*
* @return the plugin's data directory
* @since 2.0.0
*/
Path dataDirectory();
/**
* The event handler, used for listening to and emitting events.
* The {@link CarbonEventHandler event handler}, used for listening to
* and emitting {@link CarbonEvent events}.
*
* @return the event handler
* @since 2.0.0
@@ -22,9 +22,10 @@ package net.draycia.carbon.api;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.jetbrains.annotations.ApiStatus;
/**
* Static accessor for the {@link CarbonChat} class.
* Static accessor for the {@link CarbonChat} instance.
*
* @since 1.0.0
*/
@@ -43,6 +44,7 @@ public final class CarbonChatProvider {
* @param carbonChat the carbon implementation
* @since 1.0.0
*/
@ApiStatus.Internal
public static void register(final CarbonChat carbonChat) {
CarbonChatProvider.instance = carbonChat;
}
@@ -55,7 +57,7 @@ public final class CarbonChatProvider {
*/
public static CarbonChat carbonChat() {
if (CarbonChatProvider.instance == null) {
throw new IllegalStateException("CarbonChat not initialized!"); // LuckPerms design go brrrr
throw new IllegalStateException("CarbonChat not initialized!");
}
return CarbonChatProvider.instance;
@@ -40,7 +40,7 @@ public interface ChannelRegistry {
* @param channel the channel to register
* @since 2.1.0
*/
void register(final ChatChannel channel);
void register(ChatChannel channel);
/**
* Gets the channel with the matching key.
@@ -49,7 +49,7 @@ public interface ChannelRegistry {
* @return the channel
* @since 2.1.0
*/
@Nullable ChatChannel channel(final Key key);
@Nullable ChatChannel channel(Key key);
/**
* Gets the default key.
@@ -82,7 +82,7 @@ public interface ChannelRegistry {
* @return the channel, or the default one
* @since 2.1.0
*/
ChatChannel keyOrDefault(final Key key);
ChatChannel keyOrDefault(Key key);
/**
* The provided action will be executed immediately for all currently registered
@@ -50,7 +50,7 @@ public interface ChatChannel extends Keyed, ChatComponentRenderer {
* @return if the player may speak
* @since 2.0.0
*/
ChannelPermissionResult speechPermitted(final CarbonPlayer carbonPlayer);
ChannelPermissionResult speechPermitted(CarbonPlayer carbonPlayer);
/**
* Checks if the player may receive messages from this channel.
@@ -59,7 +59,7 @@ public interface ChatChannel extends Keyed, ChatComponentRenderer {
* @return if the player may receive messages
* @since 2.0.0
*/
ChannelPermissionResult hearingPermitted(final CarbonPlayer player);
ChannelPermissionResult hearingPermitted(CarbonPlayer player);
/**
* Returns a list of all recipients that will receive messages from the sender.
@@ -68,7 +68,7 @@ public interface ChatChannel extends Keyed, ChatComponentRenderer {
* @return the recipients
* @since 2.0.0
*/
List<Audience> recipients(final CarbonPlayer sender);
List<Audience> recipients(CarbonPlayer sender);
/**
* Filters the given recipients and removes entries that may not see messages in this channel.
@@ -78,7 +78,7 @@ public interface ChatChannel extends Keyed, ChatComponentRenderer {
* @return the recipients that may receive messages
* @since 2.0.0
*/
Set<CarbonPlayer> filterRecipients(final CarbonPlayer sender, final Set<CarbonPlayer> recipients);
Set<CarbonPlayer> filterRecipients(CarbonPlayer sender, Set<CarbonPlayer> recipients);
/**
* Messages will be sent in this channel if they start with this prefix.
@@ -37,9 +37,9 @@ public interface Cancellable {
/**
* Sets the cancelled state.
*
* @param cancelled sets the cancelled state
* @param cancelled new cancelled state
* @since 2.1.0
*/
void cancelled(final boolean cancelled);
void cancelled(boolean cancelled);
}
@@ -20,7 +20,7 @@
package net.draycia.carbon.api.event;
/**
* Interface to represent that an event belongs to carbon.
* Marker interface for events.
*
* @since 1.0.0
*/
@@ -23,7 +23,8 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
/**
* Carbon event dispatcher.
* The {@link CarbonEventHandler} is responsible for managing {@link CarbonEventSubscription event subscriptions}
* and emitting {@link CarbonEvent events}.
*
* @since 2.1.0
*/
@@ -40,8 +41,8 @@ public interface CarbonEventHandler {
* @since 2.0.0
*/
<T extends CarbonEvent> CarbonEventSubscription<T> subscribe(
final Class<T> eventClass,
final CarbonEventSubscriber<T> subscriber
Class<T> eventClass,
CarbonEventSubscriber<T> subscriber
);
/**
@@ -57,21 +58,22 @@ public interface CarbonEventHandler {
* @since 2.0.0
*/
<T extends CarbonEvent> CarbonEventSubscription<T> subscribe(
final Class<T> eventClass,
final int order,
final boolean acceptsCancelled,
final CarbonEventSubscriber<T> subscriber
Class<T> eventClass,
int order,
boolean acceptsCancelled,
CarbonEventSubscriber<T> subscriber
);
/**
* Emits the supplied event.<br>
* Events are modified in place, so care should be taken to keep a reference to
* the event while it's being emitted.
* Emits the supplied event.
*
* <p>Events are modified in place, meaning you must keep a reference to the event
* yourself if you wish to inspect it's state after this call.</p>
*
* @param event the event to be emitted
* @param <T> the class to emit
* @since 2.0.0
*/
<T extends CarbonEvent> void emit(final T event);
<T extends CarbonEvent> void emit(T event);
}
@@ -21,7 +21,6 @@ package net.draycia.carbon.api.event;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.jetbrains.annotations.NotNull;
/**
* An EventSubscriber.
@@ -39,6 +38,6 @@ public interface CarbonEventSubscriber<T extends CarbonEvent> {
* @throws Throwable if an exception is thrown
* @since 1.0.0
*/
void on(final @NotNull T event) throws Throwable;
void on(T event) throws Throwable;
}
@@ -23,9 +23,9 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
/**
* An EventSubscription.
* A subscription to a specific event type.
*
* @param <T> CarbonEvent implementations
* @param <T> event type
* @since 2.1.0
*/
@DefaultQualifier(NonNull.class)
@@ -40,11 +40,20 @@ public interface CarbonEventSubscription<T extends CarbonEvent> {
Class<T> event();
/**
* Gets the subscriber.
* Gets the {@link CarbonEventSubscriber subscriber}.
*
* @return the subscriber
* @since 2.1.0
*/
CarbonEventSubscriber<T> subscriber();
/**
* Disposes this subscription.
*
* <p>The subscriber held by this subscription will no longer receive events.</p>
*
* @since 2.1.0
*/
void dispose();
}
@@ -31,8 +31,9 @@ import org.checkerframework.framework.qual.DefaultQualifier;
* {@link CarbonEvent} that's called after new channels are registered.
*
* <p>Note that some invocations of this event may be too early for
* API consumers to be notified. Prefer using {@link ChannelRegistry#allKeys(Consumer)}
* when knowledge of all registered channels is needed.</p>
* API consumers to be notified. {@link ChannelRegistry#allKeys(Consumer)}
* is provided as a helper for when knowledge of all registered channels
* is needed.</p>
*
* @since 2.1.0
*/
@@ -41,9 +41,10 @@ import org.checkerframework.framework.qual.DefaultQualifier;
public interface CarbonChatEvent extends CarbonEvent, Cancellable {
/**
* Get the renderers used to construct components for each of the recipients.
* Get the renderers used to construct components for each of the recipients. The returned list
* is mutable.
*
* @return The per-recipient component renderers.
* @return renderers
* @since 2.0.0
*/
List<KeyedRenderer> renderers();
@@ -41,7 +41,7 @@ public interface CarbonPrivateChatEvent extends CarbonEvent, Cancellable {
* @throws NullPointerException if message is null
* @since 2.1.0
*/
void message(final Component message);
void message(Component message);
/**
* The message that will be sent.
@@ -42,8 +42,6 @@ import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public interface CarbonPlayer extends Audience, Identified {
CarbonPlayer EMPTY = new EmptyCarbonPlayer();
/**
* Returns the appropriate {@link Component} to represent the player's name.
*
@@ -66,7 +64,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @return the distance from the other player, or -1
* @since 2.1.0
*/
double distanceSquaredFrom(final CarbonPlayer other);
double distanceSquaredFrom(CarbonPlayer other);
/**
* Returns if both players are in the same world or server.
@@ -75,7 +73,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @return if both players are in the same world/server
* @since 2.1.0
*/
boolean sameWorldAs(final CarbonPlayer other);
boolean sameWorldAs(CarbonPlayer other);
/**
* Gets the player's username.
@@ -112,7 +110,7 @@ public interface CarbonPlayer extends Audience, Identified {
/**
* Sets the player's display name.
*
* <p>Setting {@code null} is equivalent to setting the display name to the username.</p>
* <p>Setting {@code null} will remove the current display name.</p>
* <p>Won't have any visible effect when Carbon's nickname management is disabled.</p>
*
* @param displayName the new display name
@@ -120,7 +118,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @see #hasCustomDisplayName()
* @since 2.0.0
*/
void displayName(final @Nullable Component displayName);
void displayName(@Nullable Component displayName);
/**
* The player's UUID, often used for identification purposes.
@@ -137,7 +135,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @return the chat component for the item in the slot, or null if the slot is empty
* @since 2.0.0
*/
@Nullable Component createItemHoverComponent(final InventorySlot slot);
@Nullable Component createItemHoverComponent(InventorySlot slot);
/**
* The player's locale.
@@ -161,7 +159,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @param chatChannel the channel
* @since 2.0.0
*/
void selectedChannel(final @Nullable ChatChannel chatChannel);
void selectedChannel(@Nullable ChatChannel chatChannel);
/**
* Determines which channel the message should go to, and removes any channel prefixes from the message.
@@ -170,7 +168,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @return the channel and message
* @since 2.1.0
*/
ChannelMessage channelForMessage(final Component message);
ChannelMessage channelForMessage(Component message);
/**
* A message and which channel it should be sent in.
@@ -188,7 +186,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @return if the player has the permission
* @since 2.0.0
*/
boolean hasPermission(final String permission);
boolean hasPermission(String permission);
/**
* Returns the player's primary group.
@@ -229,7 +227,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @return if this player is ignoring the sender
* @since 2.0.5
*/
boolean ignoring(final UUID player);
boolean ignoring(UUID player);
/**
* Checks if the other player is being ignored by this player.
@@ -238,7 +236,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @return if this player is ignoring the sender
* @since 2.0.0
*/
boolean ignoring(final CarbonPlayer player);
boolean ignoring(CarbonPlayer player);
/**
* Adds the player to and removes the player from the ignore list.
@@ -247,7 +245,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @param nowIgnoring if the player should be ignored
* @since 2.0.0
*/
void ignoring(final UUID player, final boolean nowIgnoring);
void ignoring(UUID player, boolean nowIgnoring);
/**
* Adds the player to and removes the player from the ignore list.
@@ -256,7 +254,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @param nowIgnoring if the player should be ignored
* @since 2.0.0
*/
void ignoring(final CarbonPlayer player, boolean nowIgnoring);
void ignoring(CarbonPlayer player, boolean nowIgnoring);
/**
* Returns if the player is deafened and unable to read messages.
@@ -271,7 +269,7 @@ public interface CarbonPlayer extends Audience, Identified {
*
* @since 2.0.0
*/
void deafened(final boolean deafened);
void deafened(boolean deafened);
/**
* Returns if the player is spying on messages and able to read muted/private messages.
@@ -286,7 +284,7 @@ public interface CarbonPlayer extends Audience, Identified {
*
* @since 2.0.0
*/
void spying(final boolean spying);
void spying(boolean spying);
/**
* Sends the message as the player.
@@ -294,7 +292,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @param message the message to be sent
* @since 2.0.0
*/
void sendMessageAsPlayer(final String message);
void sendMessageAsPlayer(String message);
/**
* Returns whether the player is online.
@@ -318,7 +316,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @param uuid the uuid of the reply target
* @since 2.0.0
*/
void whisperReplyTarget(final @Nullable UUID uuid);
void whisperReplyTarget(@Nullable UUID uuid);
/**
* The last player this player has whispered.
@@ -334,7 +332,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @param uuid the uuid of the whisper target
* @since 2.0.0
*/
void lastWhisperTarget(final @Nullable UUID uuid);
void lastWhisperTarget(@Nullable UUID uuid);
/**
* If this player is vanished in another supported plugin.
@@ -353,7 +351,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @return if this player is aware of the other player
* @since 2.0.0
*/
boolean awareOf(final CarbonPlayer other);
boolean awareOf(CarbonPlayer other);
/**
* A list of all the channels the player has left
@@ -373,7 +371,7 @@ public interface CarbonPlayer extends Audience, Identified {
* @param channel the channel to join.
* @since 2.1.0
*/
void joinChannel(final ChatChannel channel);
void joinChannel(ChatChannel channel);
/**
* Leave a channel for this player.
@@ -381,6 +379,6 @@ public interface CarbonPlayer extends Audience, Identified {
* @param channel the channel to leave.
* @since 2.1.0
*/
void leaveChannel(final ChatChannel channel);
void leaveChannel(ChatChannel channel);
}
@@ -1,223 +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 <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.api.users;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.util.InventorySlot;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
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)
class EmptyCarbonPlayer implements CarbonPlayer {
@Override
public double distanceSquaredFrom(final CarbonPlayer other) {
return 0;
}
@Override
public boolean sameWorldAs(final CarbonPlayer other) {
return false;
}
@Override
public String username() {
return "";
}
@Override
public boolean hasCustomDisplayName() {
return false;
}
@Override
public @Nullable Component displayName() {
return null;
}
@Override
public void displayName(final @Nullable Component displayName) {
}
@Override
public UUID uuid() {
return new UUID(0, 0);
}
@Override
public @Nullable Component createItemHoverComponent(final InventorySlot slot) {
return null;
}
@Override
public @Nullable Locale locale() {
return null;
}
@Override
public @Nullable ChatChannel selectedChannel() {
return null;
}
@Override
public void selectedChannel(final @Nullable ChatChannel chatChannel) {
}
@Override
public ChannelMessage channelForMessage(final Component message) {
return new ChannelMessage(message, null);
}
@Override
public boolean hasPermission(final String permission) {
return false;
}
@Override
public String primaryGroup() {
return "";
}
@Override
public List<String> groups() {
return List.of();
}
@Override
public boolean muted() {
return false;
}
@Override
public void muted(final boolean muted) {
}
@Override
public boolean ignoring(final UUID player) {
return false;
}
@Override
public boolean ignoring(final CarbonPlayer sender) {
return false;
}
@Override
public void ignoring(final UUID player, final boolean nowIgnoring) {
}
@Override
public void ignoring(final CarbonPlayer player, final boolean nowIgnoring) {
}
@Override
public boolean deafened() {
return false;
}
@Override
public void deafened(final boolean deafened) {
}
@Override
public boolean spying() {
return false;
}
@Override
public void spying(final boolean spying) {
}
@Override
public void sendMessageAsPlayer(final String message) {
}
@Override
public boolean online() {
return false;
}
@Override
public @Nullable UUID whisperReplyTarget() {
return null;
}
@Override
public void whisperReplyTarget(final @Nullable UUID uuid) {
}
@Override
public @Nullable UUID lastWhisperTarget() {
return null;
}
@Override
public void lastWhisperTarget(final @Nullable UUID uuid) {
}
@Override
public boolean vanished() {
return false;
}
@Override
public boolean awareOf(final CarbonPlayer other) {
return true;
}
@Override
public List<Key> leftChannels() {
return List.of();
}
@Override
public void joinChannel(final ChatChannel channel) {
}
@Override
public void leaveChannel(final ChatChannel channel) {
}
@Override
public @NotNull Identity identity() {
return Identity.nil();
}
}
@@ -24,7 +24,6 @@ import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.jetbrains.annotations.NotNull;
/**
* Renderer used to construct chat components on a per-player basis.
@@ -47,10 +46,9 @@ public interface ChatComponentRenderer {
* or empty if the recipient should not receive the message
* @since 2.0.0
*/
@NotNull
Component render(final CarbonPlayer sender,
final Audience recipient,
final Component message,
final Component originalMessage);
Component render(CarbonPlayer sender,
Audience recipient,
Component message,
Component originalMessage);
}
@@ -39,11 +39,7 @@ public final class InventorySlot {
return new InventorySlot(placeholders);
}
private List<String> placeholders;
private InventorySlot() {
}
private final List<String> placeholders;
private InventorySlot(final String... placeholders) {
this.placeholders = List.of(placeholders);
@@ -19,17 +19,13 @@
*/
package net.draycia.carbon.api.util;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.jetbrains.annotations.NotNull;
/**
* A chat renderer that's identifiable by key.
* A {@link ChatComponentRenderer chat renderer} that's identifiable by key.
*
* @since 2.0.0
*/
@@ -45,26 +41,7 @@ public interface KeyedRenderer extends Keyed, ChatComponentRenderer {
* @since 2.0.0
*/
static KeyedRenderer keyedRenderer(final Key key, final ChatComponentRenderer renderer) {
return new Impl(key, renderer);
}
/**
* Implementation of the keyed renderer.
*
* @since 2.0.0
*/
record Impl(Key key, ChatComponentRenderer renderer) implements KeyedRenderer {
@Override
public @NotNull Component render(
final CarbonPlayer sender,
final Audience recipient,
final Component message,
final Component originalMessage
) {
return this.renderer.render(sender, recipient, message, originalMessage);
}
return new KeyedRendererImpl(key, renderer);
}
}
@@ -19,32 +19,24 @@
*/
package net.draycia.carbon.api.util;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import org.jetbrains.annotations.NotNull;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
/**
* An audience, where messages are sent from another Audience.
*
* @since 2.0.0
*/
public record SourcedAudience(Audience sender, Audience recipient) implements ForwardingAudience.Single {
public static final SourcedAudience EMPTY = new SourcedAudience(Audience.empty(), Audience.empty());
/**
* An empty {@link SourcedAudience}, with an empty sender and recipient.
*
* @return an empty sourced audience
* @since 2.0.0
*/
public static SourcedAudience empty() {
return EMPTY;
}
@DefaultQualifier(NonNull.class)
record KeyedRendererImpl(Key key, ChatComponentRenderer renderer) implements KeyedRenderer {
@Override
public @NotNull Audience audience() {
return this.recipient;
public Component render(
final CarbonPlayer sender,
final Audience recipient,
final Component message,
final Component originalMessage
) {
return this.renderer.render(sender, recipient, message, originalMessage);
}
}
+2 -2
View File
@@ -1,11 +1,11 @@
import org.gradle.internal.impldep.org.junit.experimental.categories.Categories.CategoryFilter.exclude
plugins {
id("carbon.base-conventions")
}
dependencies {
api(projects.carbonchatApi)
compileOnlyApi(platform(libs.log4jBom))
compileOnlyApi(libs.log4jApi)
// Configs
api(libs.configurateHocon)
@@ -23,7 +23,6 @@ import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import java.nio.file.Path;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService;
@@ -44,9 +43,6 @@ import net.draycia.carbon.common.users.UserManagerInternal;
import net.draycia.carbon.common.util.CloudUtils;
import net.draycia.carbon.common.util.ConcurrentUtil;
import net.draycia.carbon.common.util.PlayerUtils;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.message.IMessageRenderer;
import ninja.egg82.messenger.services.PacketService;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -59,7 +55,6 @@ public abstract class CarbonChatInternal<C extends CarbonPlayer> implements Carb
private final UUID serverId;
private final Injector injector;
private final Logger logger;
private final Path dataDirectory;
private final ScheduledExecutorService periodicTasks;
private final ProfileCache profileCache;
private final ProfileResolver profileResolver;
@@ -69,13 +64,11 @@ public abstract class CarbonChatInternal<C extends CarbonPlayer> implements Carb
private final CarbonMessages carbonMessages;
private final CarbonEventHandler eventHandler;
private final CarbonChannelRegistry channelRegistry;
private final IMessageRenderer<Audience, String, Component, Component> renderer;
private final Provider<MessagingManager> messagingManager;
protected CarbonChatInternal(
final Injector injector,
final Logger logger,
final Path dataDirectory,
final ScheduledExecutorService periodicTasks,
final ProfileCache profileCache,
final ProfileResolver profileResolver,
@@ -85,13 +78,11 @@ public abstract class CarbonChatInternal<C extends CarbonPlayer> implements Carb
final CarbonMessages carbonMessages,
final CarbonEventHandler eventHandler,
final CarbonChannelRegistry channelRegistry,
final IMessageRenderer<Audience, String, Component, Component> renderer,
final Provider<MessagingManager> messagingManagerProvider
) {
this.serverId = UUID.randomUUID();
this.injector = injector;
this.logger = logger;
this.dataDirectory = dataDirectory;
this.periodicTasks = periodicTasks;
this.profileCache = profileCache;
this.profileResolver = profileResolver;
@@ -101,7 +92,6 @@ public abstract class CarbonChatInternal<C extends CarbonPlayer> implements Carb
this.carbonMessages = carbonMessages;
this.eventHandler = eventHandler;
this.channelRegistry = channelRegistry;
this.renderer = renderer;
this.messagingManager = messagingManagerProvider;
}
@@ -155,12 +145,6 @@ public abstract class CarbonChatInternal<C extends CarbonPlayer> implements Carb
return this.serverId;
}
@Override
public Path dataDirectory() {
return this.dataDirectory;
}
@Override
public Logger logger() {
return this.logger;
}
@@ -185,11 +169,6 @@ public abstract class CarbonChatInternal<C extends CarbonPlayer> implements Carb
return this.channelRegistry;
}
@SuppressWarnings("unchecked")
public <T extends Audience> IMessageRenderer<T, String, Component, Component> messageRenderer() {
return (IMessageRenderer<T, String, Component, Component>) this.renderer;
}
public @Nullable PacketService packetService() {
return this.messagingManager.get().packetService();
}
@@ -51,6 +51,7 @@ import net.draycia.carbon.common.listeners.MessagePacketHandler;
import net.draycia.carbon.common.listeners.MuteHandler;
import net.draycia.carbon.common.listeners.PingHandler;
import net.draycia.carbon.common.listeners.RadiusListener;
import net.draycia.carbon.common.messages.CarbonMessageRenderer;
import net.draycia.carbon.common.messages.CarbonMessageSender;
import net.draycia.carbon.common.messages.CarbonMessageSource;
import net.draycia.carbon.common.messages.CarbonMessages;
@@ -76,7 +77,6 @@ import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.Moonshine;
import net.kyori.moonshine.exception.scan.UnscannableMethodException;
import net.kyori.moonshine.message.IMessageRenderer;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -113,7 +113,7 @@ public final class CarbonCommonModule extends AbstractModule {
final BooleanPlaceholderResolver<Audience> booleanPlaceholderResolver,
final CarbonMessageSource carbonMessageSource,
final CarbonMessageSender carbonMessageSender,
final IMessageRenderer<Audience, String, Component, Component> messageRenderer
final CarbonMessageRenderer messageRenderer
) throws UnscannableMethodException {
return Moonshine.<CarbonMessages, Audience>builder(new TypeToken<>() {})
.receiverLocatorResolver(receiverResolver, 0)
@@ -26,7 +26,6 @@ import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import com.seiama.event.EventConfig;
import com.seiama.registry.Registry;
import java.io.IOException;
import java.nio.file.Path;
@@ -44,13 +43,12 @@ import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.event.events.CarbonChannelRegisterEvent;
import net.draycia.carbon.api.event.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.KeyedRenderer;
import net.draycia.carbon.common.DataDirectory;
import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.PlayerCommander;
import net.draycia.carbon.common.config.ConfigFactory;
import net.draycia.carbon.common.event.events.CarbonChatEventImpl;
import net.draycia.carbon.common.event.events.CarbonReloadEvent;
import net.draycia.carbon.common.event.events.ChannelRegisterEventImpl;
import net.draycia.carbon.common.listeners.ChatListenerInternal;
@@ -60,7 +58,6 @@ import net.draycia.carbon.common.util.Exceptions;
import net.draycia.carbon.common.util.FileUtil;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -118,7 +115,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
this.carbonMessages = carbonMessages;
this.eventHandler = events;
events.subscribe(CarbonReloadEvent.class, -99, EventConfig.DEFAULT_ACCEPTS_CANCELLED, event -> this.reloadConfigChannels());
events.subscribe(CarbonReloadEvent.class, -99, true, event -> this.reloadConfigChannels());
}
public static ConfigurationTransformation.Versioned versioned() {
@@ -309,20 +306,14 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
final List<Audience> recipients,
final String plainMessage
) {
final @Nullable CarbonChatEvent chatEvent = this.prepareAndEmitChatEvent(sender, plainMessage, null, channel);
final @Nullable CarbonChatEventImpl chatEvent = this.prepareAndEmitChatEvent(sender, plainMessage, null, channel);
if (chatEvent == null || chatEvent.cancelled()) {
return;
}
for (final Audience recipient : recipients) {
Component renderedMessage = chatEvent.message();
for (final KeyedRenderer renderer : chatEvent.renderers()) {
renderedMessage = renderer.render(sender, recipient, renderedMessage, chatEvent.message());
}
recipient.sendMessage(renderedMessage);
recipient.sendMessage(chatEvent.renderFor(recipient));
}
}
@@ -27,13 +27,13 @@ import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.CarbonServer;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.CarbonChatInternal;
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
import net.draycia.carbon.common.channels.messages.ConfigChannelMessages;
import net.draycia.carbon.common.messages.CarbonMessageRenderer;
import net.draycia.carbon.common.messages.SourcedAudience;
import net.draycia.carbon.common.messages.SourcedMessageSender;
import net.draycia.carbon.common.messages.SourcedReceiverResolver;
import net.draycia.carbon.common.messages.placeholders.BooleanPlaceholderResolver;
@@ -41,6 +41,7 @@ import net.draycia.carbon.common.messages.placeholders.ComponentPlaceholderResol
import net.draycia.carbon.common.messages.placeholders.KeyPlaceholderResolver;
import net.draycia.carbon.common.messages.placeholders.StringPlaceholderResolver;
import net.draycia.carbon.common.messages.placeholders.UUIDPlaceholderResolver;
import net.draycia.carbon.common.util.Exceptions;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
@@ -65,7 +66,8 @@ import static net.kyori.adventure.text.Component.text;
@DefaultQualifier(NonNull.class)
public final class ConfigChatChannel implements ChatChannel {
private transient @MonotonicNonNull @Inject CarbonChat carbonChat;
private transient @MonotonicNonNull @Inject CarbonServer server;
private transient @MonotonicNonNull @Inject CarbonMessageRenderer renderer;
@Comment("""
The channel's key, used to track the channel.
@@ -144,7 +146,7 @@ public final class ConfigChatChannel implements ChatChannel {
final Component originalMessage
) {
return this.carbonMessages().chatFormat(
new SourcedAudience(sender, recipient),
SourcedAudience.of(sender, recipient),
sender.uuid(),
this.key(),
Objects.requireNonNull(CarbonPlayer.renderName(sender)),
@@ -168,14 +170,14 @@ public final class ConfigChatChannel implements ChatChannel {
public List<Audience> recipients(final CarbonPlayer sender) {
final List<Audience> recipients = new ArrayList<>();
for (final CarbonPlayer player : this.carbonChat.server().players()) {
for (final CarbonPlayer player : this.server.players()) {
if (this.hearingPermitted(player).permitted()) {
recipients.add(player);
}
}
// console too!
recipients.add(this.carbonChat.server().console());
recipients.add(this.server.console());
return recipients;
}
@@ -197,10 +199,10 @@ public final class ConfigChatChannel implements ChatChannel {
}
public String messageFormat(final CarbonPlayer sender) {
return this.messageSource.messageOf(new SourcedAudience(sender, sender), "");
return this.messageSource.messageOf(SourcedAudience.of(sender, sender), "");
}
private @Nullable ConfigChannelMessages loadMessages() {
private ConfigChannelMessages loadMessages() {
final SourcedReceiverResolver serverReceiverResolver = new SourcedReceiverResolver();
final ComponentPlaceholderResolver<SourcedAudience> componentPlaceholderResolver = new ComponentPlaceholderResolver<>();
final UUIDPlaceholderResolver<SourcedAudience> uuidPlaceholderResolver = new UUIDPlaceholderResolver<>();
@@ -213,7 +215,7 @@ public final class ConfigChatChannel implements ChatChannel {
return Moonshine.<ConfigChannelMessages, SourcedAudience>builder(new TypeToken<ConfigChannelMessages>() {})
.receiverLocatorResolver(serverReceiverResolver, 0)
.sourced(this.messageSource)
.rendered(((CarbonChatInternal<?>) this.carbonChat).messageRenderer())
.rendered(this.renderer.asSourced())
.sent(carbonMessageSender)
.resolvingWithStrategy(new StandardPlaceholderResolverStrategy<>(new StandardSupertypeThenInterfaceSupertypeStrategy(false)))
.weightedPlaceholderResolver(Component.class, componentPlaceholderResolver, 0)
@@ -223,10 +225,8 @@ public final class ConfigChatChannel implements ChatChannel {
.weightedPlaceholderResolver(Boolean.class, booleanPlaceholderResolver, 0)
.create(this.getClass().getClassLoader());
} catch (final UnscannableMethodException e) {
e.printStackTrace();
throw Exceptions.rethrow(e);
}
return null;
}
private ConfigChannelMessages carbonMessages() {
@@ -23,7 +23,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.messages.SourcedAudience;
import net.draycia.carbon.common.util.DiscordRecipient;
import net.kyori.adventure.audience.Audience;
import net.kyori.moonshine.message.IMessageSource;
@@ -20,7 +20,7 @@
package net.draycia.carbon.common.channels.messages;
import java.util.UUID;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.messages.SourcedAudience;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.annotation.Message;
@@ -30,7 +30,6 @@ import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.event.events.CarbonPrivateChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.command.ArgumentFactory;
import net.draycia.carbon.common.command.CarbonCommand;
import net.draycia.carbon.common.command.CommandSettings;
@@ -40,6 +39,7 @@ 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.messages.SourcedAudience;
import net.draycia.carbon.common.messaging.MessagingManager;
import net.draycia.carbon.common.messaging.packets.PacketFactory;
import net.draycia.carbon.common.messaging.packets.WhisperPacket;
@@ -198,9 +198,9 @@ public class WhisperCommand extends CarbonCommand {
return;
}
this.messages.whisperSender(new SourcedAudience(sender, sender), senderName, recipientName, privateChatEvent.message());
this.messages.whisperSender(SourcedAudience.of(sender, sender), senderName, recipientName, privateChatEvent.message());
if (localRecipient) {
this.messages.whisperRecipient(new SourcedAudience(sender, recipient), senderName, recipientName, privateChatEvent.message());
this.messages.whisperRecipient(SourcedAudience.of(sender, recipient), senderName, recipientName, privateChatEvent.message());
}
this.messages.whisperConsoleLog(this.server.console(), senderName, recipientName, privateChatEvent.message());
@@ -233,7 +233,7 @@ public class WhisperCommand extends CarbonCommand {
final Component recipientName = CarbonPlayer.renderName(recipient);
recipient.whisperReplyTarget(sender.uuid());
this.messages.whisperRecipient(new SourcedAudience(sender, recipient), senderName, recipientName, packet.message());
this.messages.whisperRecipient(SourcedAudience.of(sender, recipient), senderName, recipientName, packet.message());
this.messages.whisperConsoleLog(this.server.console(), senderName, recipientName, packet.message());
final @Nullable Sound messageSound = this.configFactory.primaryConfig().messageSound();
if (messageSound != null) {
@@ -21,7 +21,6 @@ package net.draycia.carbon.common.config;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.seiama.event.EventConfig;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -57,7 +56,7 @@ public class ConfigFactory {
this.dataDirectory = dataDirectory;
this.locale = locale;
events.subscribe(CarbonReloadEvent.class, -100, EventConfig.DEFAULT_ACCEPTS_CANCELLED, event -> this.reloadPrimaryConfig());
events.subscribe(CarbonReloadEvent.class, -100, true, event -> this.reloadPrimaryConfig());
}
public @Nullable PrimaryConfig reloadPrimaryConfig() {
@@ -27,6 +27,7 @@ import com.seiama.event.bus.EventBus;
import com.seiama.event.bus.SimpleEventBus;
import com.seiama.event.registry.EventRegistry;
import com.seiama.event.registry.SimpleEventRegistry;
import net.draycia.carbon.api.event.Cancellable;
import net.draycia.carbon.api.event.CarbonEvent;
import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.event.CarbonEventSubscriber;
@@ -60,8 +61,11 @@ public final class CarbonEventHandlerImpl implements CarbonEventHandler {
final Class<T> eventClass,
final CarbonEventSubscriber<T> subscriber
) {
this.eventRegistry.subscribe(eventClass, subscriber::on);
return new CarbonEventSubscriptionImpl<>(eventClass, subscriber);
return new CarbonEventSubscriptionImpl<>(
eventClass,
subscriber,
this.eventRegistry.subscribe(eventClass, subscriber::on)
);
}
// TODO: support EventConfig#exact()
@@ -73,8 +77,17 @@ public final class CarbonEventHandlerImpl implements CarbonEventHandler {
final CarbonEventSubscriber<T> subscriber
) {
final EventConfig eventConfig = EventConfig.defaults().order(order).acceptsCancelled(acceptsCancelled);
this.eventRegistry.subscribe(eventClass, eventConfig, subscriber::on);
return new CarbonEventSubscriptionImpl<>(eventClass, subscriber);
return new CarbonEventSubscriptionImpl<>(
eventClass,
subscriber,
this.eventRegistry.subscribe(eventClass, eventConfig, event -> {
// Our events implement seiama Cancellable; but API consumers won't be able to do that
if (!acceptsCancelled && event instanceof Cancellable cancellable && cancellable.cancelled()) {
return;
}
subscriber.on(event);
})
);
}
@Override
@@ -19,6 +19,7 @@
*/
package net.draycia.carbon.common.event;
import com.seiama.event.EventSubscription;
import net.draycia.carbon.api.event.CarbonEvent;
import net.draycia.carbon.api.event.CarbonEventSubscriber;
import net.draycia.carbon.api.event.CarbonEventSubscription;
@@ -26,6 +27,15 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
record CarbonEventSubscriptionImpl<T extends CarbonEvent>(Class<T> event, CarbonEventSubscriber<T> subscriber) implements CarbonEventSubscription<T> {
record CarbonEventSubscriptionImpl<T extends CarbonEvent>(
Class<T> event,
CarbonEventSubscriber<T> subscriber,
EventSubscription<T> backingSubscription
) implements CarbonEventSubscription<T> {
@Override
public void dispose() {
this.backingSubscription.dispose();
}
}
@@ -80,36 +80,52 @@ public class CarbonChatEventImpl extends CancellableImpl implements CarbonChatEv
this.origin = origin;
}
@Override
public List<KeyedRenderer> renderers() {
return this.renderers;
}
@Override
public @MonotonicNonNull SignedMessage signedMessage() {
return this.signedMessage;
}
@Override
public CarbonPlayer sender() {
return this.sender;
}
@Override
public Component originalMessage() {
return this.originalMessage;
}
@Override
public Component message() {
return this.message;
}
@Override
public void message(final Component message) {
this.message = message;
}
@Override
public @MonotonicNonNull ChatChannel chatChannel() {
return this.chatChannel;
}
@Override
public List<? extends Audience> recipients() {
return this.recipients;
}
public Component renderFor(final Audience viewer) {
Component renderedMessage = this.message();
for (final var renderer : this.renderers()) {
renderedMessage = renderer.render(this.sender, viewer, renderedMessage, this.message());
}
return renderedMessage;
}
}
@@ -24,7 +24,6 @@ import java.util.Iterator;
import java.util.List;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.event.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.KeyedRenderer;
import net.draycia.carbon.common.config.ConfigFactory;
@@ -60,7 +59,7 @@ public abstract class ChatListenerInternal {
this.carbonEventHandler = carbonEventHandler;
}
protected @Nullable CarbonChatEvent prepareAndEmitChatEvent(final CarbonPlayer sender, final String messageContent, final SignedMessage signedMessage) {
protected @Nullable CarbonChatEventImpl prepareAndEmitChatEvent(final CarbonPlayer sender, final String messageContent, final @Nullable SignedMessage signedMessage) {
final CarbonPlayer.ChannelMessage channelMessage = sender.channelForMessage(Component.text(messageContent));
final ChatChannel channel = channelMessage.channel();
final String message = PlainTextComponentSerializer.plainText().serialize(channelMessage.message());
@@ -68,7 +67,7 @@ public abstract class ChatListenerInternal {
return this.prepareAndEmitChatEvent(sender, message, signedMessage, channel);
}
protected @Nullable CarbonChatEvent prepareAndEmitChatEvent(final CarbonPlayer sender, final String messageContent, final SignedMessage signedMessage, final ChatChannel channel) {
protected @Nullable CarbonChatEventImpl prepareAndEmitChatEvent(final CarbonPlayer sender, final String messageContent, final @Nullable SignedMessage signedMessage, final ChatChannel channel) {
String content = this.configFactory.primaryConfig().applyChatPlaceholders(messageContent);
content = this.configFactory.primaryConfig().applyChatFilters(content);
@@ -98,7 +97,7 @@ public abstract class ChatListenerInternal {
final List<Audience> recipients = channel.recipients(sender);
final CarbonChatEvent chatEvent = new CarbonChatEventImpl(sender, message, recipients, renderers, channel, signedMessage);
final var chatEvent = new CarbonChatEventImpl(sender, message, recipients, renderers, channel, signedMessage);
this.carbonEventHandler.emit(chatEvent);
@@ -40,15 +40,11 @@ public class ItemLinkHandler implements Listener {
event.message()
.replaceText(TextReplacementConfig.builder()
.matchLiteral("<" + placeholder + ">")
.once()
// .once()
.replacement(builder -> {
final var itemComponent = event.sender().createItemHoverComponent(slot);
if (itemComponent == null) {
return builder;
}
return event.sender().createItemHoverComponent(slot);
return itemComponent == null ? builder : itemComponent;
})
.build())
);
@@ -52,11 +52,7 @@ public class MessagePacketHandler implements Listener {
messaging.get().withPacketService(packetService -> {
final CarbonPlayer sender = event.sender();
Component networkMessage = event.message();
for (final var renderer : event.renderers()) {
networkMessage = renderer.render(sender, sender, networkMessage, event.originalMessage());
}
final Component networkMessage = e.renderFor(sender);
packetService.queuePacket(new ChatMessagePacket(carbonChat.serverId(), sender.uuid(),
event.chatChannel().permission(), event.chatChannel().key(), sender.username(), networkMessage));
@@ -0,0 +1,33 @@
/*
* 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.messages;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.message.IMessageRenderer;
@FunctionalInterface
public interface CarbonMessageRenderer extends IMessageRenderer<Audience, String, Component, Component> {
default IMessageRenderer<SourcedAudience, String, Component, Component> asSourced() {
return this::render;
}
}
@@ -19,7 +19,6 @@
*/
package net.draycia.carbon.common.messages;
import net.draycia.carbon.api.util.SourcedAudience;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.message.IMessageSender;
@@ -21,7 +21,6 @@ package net.draycia.carbon.common.messages;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.seiama.event.EventConfig;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -87,7 +86,7 @@ public final class CarbonMessageSource implements IMessageSource<Audience, Strin
this.reloadTranslations();
events.subscribe(CarbonReloadEvent.class, -99, EventConfig.DEFAULT_ACCEPTS_CANCELLED, event -> {
events.subscribe(CarbonReloadEvent.class, -99, true, event -> {
this.reloadTranslations();
});
}
@@ -194,6 +193,10 @@ public final class CarbonMessageSource implements IMessageSource<Audience, Strin
public String messageOf(final Audience receiver, final String messageKey) {
Audience audience = receiver;
if (audience instanceof SourcedAudience sourced) {
audience = sourced.recipient();
}
// Unwrap PlayerCommanders
if (audience instanceof PlayerCommander playerCommander) {
audience = playerCommander.carbonPlayer();
@@ -202,7 +205,7 @@ public final class CarbonMessageSource implements IMessageSource<Audience, Strin
if (audience instanceof CarbonPlayer player) {
return this.forPlayer(messageKey, player);
} else {
return this.forAudience(messageKey, audience);
return this.fromDefaultLocale(messageKey);
}
}
@@ -219,10 +222,10 @@ public final class CarbonMessageSource implements IMessageSource<Audience, Strin
}
}
return this.forAudience(key, player);
return this.fromDefaultLocale(key);
}
private String forAudience(final String key, final Audience audience) {
private String fromDefaultLocale(final String key) {
final Properties defaultProperties = this.locales.get(this.defaultLocale);
if (defaultProperties != null) {
@@ -20,7 +20,6 @@
package net.draycia.carbon.common.messages;
import java.util.UUID;
import net.draycia.carbon.api.util.SourcedAudience;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.annotation.Message;
@@ -0,0 +1,72 @@
/*
* 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.messages;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
/**
* An audience, where messages are sent from another Audience.
*/
@DefaultQualifier(NonNull.class)
public interface SourcedAudience extends ForwardingAudience.Single {
/**
* The source audience.
*
* @return source
*/
Audience sender();
/**
* The recipient audience. The audience that this sourced audience forwards to.
*
* @return recipient
*/
Audience recipient();
@Override
default Audience audience() {
return this.recipient();
}
/**
* Create a new {@link SourcedAudience} instance.
*
* @param sender sender
* @param recipient recipient
* @return sourced audience
*/
static SourcedAudience of(final Audience sender, final Audience recipient) {
return new SourcedAudienceImpl(sender, recipient);
}
/**
* The empty {@link SourcedAudience}, with an empty sender and recipient.
*
* @return the empty sourced audience
*/
static SourcedAudience empty() {
return SourcedAudienceImpl.EMPTY;
}
}
@@ -17,16 +17,12 @@
* 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.api.event.events;
package net.draycia.carbon.common.messages;
// TODO: emit event on server shutdown
import net.kyori.adventure.audience.Audience;
import net.draycia.carbon.api.event.CarbonEvent;
record SourcedAudienceImpl(Audience sender, Audience recipient) implements SourcedAudience {
static final SourcedAudience EMPTY = new SourcedAudienceImpl(Audience.empty(), Audience.empty());
/**
* An event that's called when the server shuts down.
*
* @since 2.1.0
*/
public class CarbonShutdownEvent implements CarbonEvent {
}
@@ -19,7 +19,6 @@
*/
package net.draycia.carbon.common.messages;
import net.draycia.carbon.api.util.SourcedAudience;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.message.IMessageSender;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -42,7 +42,6 @@ package net.draycia.carbon.common.messages;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import javax.inject.Singleton;
import net.draycia.carbon.api.util.SourcedAudience;
import net.kyori.adventure.audience.Audience;
import net.kyori.moonshine.receiver.IReceiverLocator;
import net.kyori.moonshine.receiver.IReceiverLocatorResolver;
@@ -71,7 +70,7 @@ public final class SourcedReceiverResolver implements IReceiverLocatorResolver<S
if (parameter instanceof SourcedAudience sourcedAudience) {
return sourcedAudience;
} else if (parameter instanceof Audience audience) {
return new SourcedAudience(audience, audience);
return SourcedAudience.of(audience, audience);
}
return SourcedAudience.empty();
@@ -26,7 +26,6 @@ import net.draycia.carbon.api.CarbonServer;
import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.event.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.KeyedRenderer;
import net.draycia.carbon.common.command.commands.WhisperCommand;
@@ -106,7 +105,7 @@ public final class CarbonChatPacketHandler extends AbstractMessagingHandler {
final List<KeyedRenderer> renderers = new ArrayList<>();
final List<Audience> recipients = channel.recipients(sender);
final CarbonChatEvent chatEvent = new CarbonChatEventImpl(sender, messagePacket.message(), recipients, renderers, channel, null, false);
final CarbonChatEventImpl chatEvent = new CarbonChatEventImpl(sender, messagePacket.message(), recipients, renderers, channel, null, false);
this.events.emit(chatEvent);
for (final Audience recipient : recipients) {
@@ -115,13 +114,7 @@ public final class CarbonChatPacketHandler extends AbstractMessagingHandler {
continue;
}
Component renderedMessage = chatEvent.message();
for (final KeyedRenderer renderer : chatEvent.renderers()) {
renderedMessage = renderer.render(sender, recipient, renderedMessage, messagePacket.message());
}
recipient.sendMessage(renderedMessage);
recipient.sendMessage(chatEvent.renderFor(recipient));
}
this.server.console().sendMessage(Component.text("[Cross-Server] ").append(chatEvent.message()));
@@ -79,6 +79,7 @@ public class MessagingManager {
private static final byte protocolVersion = 0;
private final CarbonChat carbonChat;
private final Logger logger;
private final @MonotonicNonNull ScheduledExecutorService scheduledExecutor;
private final @MonotonicNonNull PacketService packetService;
private @MonotonicNonNull MessagingService messagingService;
@@ -94,13 +95,14 @@ public class MessagingManager {
final WhisperCommand.WhisperHandler whisper,
final PacketFactory packetFactory
) {
this.carbonChat = carbonChat;
this.logger = logger;
if (!configFactory.primaryConfig().messagingSettings().enabled()) {
if (!((CarbonChatInternal<?>) carbonChat).isProxy()) {
logger.info("Messaging services disabled in config. Cross-server will not work without this!");
}
this.messagingService = EMPTY_MESSAGING_SERVICE;
this.packetService = null;
this.carbonChat = carbonChat;
this.scheduledExecutor = null;
return;
}
@@ -126,8 +128,7 @@ public class MessagingManager {
}
};
this.scheduledExecutor = new ExceptionLoggingScheduledThreadPoolExecutor(4,
ConcurrentUtil.carbonThreadFactory(carbonChat.logger(), "MessagingManager"), carbonChat.logger());
this.carbonChat = carbonChat;
ConcurrentUtil.carbonThreadFactory(logger, "MessagingManager"), logger);
final MessagingHandlerImpl handlerImpl = new MessagingHandlerImpl(this.packetService);
handlerImpl.addHandler(new CarbonServerHandler(server, carbonChat.serverId(), this.packetService, handlerImpl, packetFactory));
@@ -197,7 +198,7 @@ public class MessagingManager {
switch (messagingSettings.brokerType()) {
case RABBITMQ -> {
this.carbonChat.logger().info("Initializing RabbitMQ Messaging services...");
this.logger.info("Initializing RabbitMQ Messaging services...");
final RabbitMQMessagingService.Builder builder = RabbitMQMessagingService.builder(packetService, name, channelName, this.carbonChat.serverId(), handlerImpl, 0L, false, packetDir)
.url(messagingSettings.url(), messagingSettings.port(), messagingSettings.vhost())
@@ -210,7 +211,7 @@ public class MessagingManager {
this.messagingService = builder.build();
}
case NATS -> {
this.carbonChat.logger().info("Initializing NATS Messaging services...");
this.logger.info("Initializing NATS Messaging services...");
final NATSMessagingService.Builder builder = NATSMessagingService.builder(packetService, name, channelName, this.carbonChat.serverId(), handlerImpl, 0L, false, packetDir)
.url(messagingSettings.url(), messagingSettings.port())
@@ -223,7 +224,7 @@ public class MessagingManager {
this.messagingService = builder.build();
}
case REDIS -> {
this.carbonChat.logger().info("Initializing Redis Messaging services...");
this.logger.info("Initializing Redis Messaging services...");
final RedisMessagingService.Builder builder = RedisMessagingService.builder(packetService, name, channelName, this.carbonChat.serverId(), handlerImpl, 0L, false, packetDir)
.url(messagingSettings.url(), messagingSettings.port());
@@ -350,11 +350,8 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override
public @Nullable ChatChannel selectedChannel() {
if (!this.selectedChannel.hasValue()) {
return this.channelRegistry.defaultChannel();
}
return this.channelRegistry.channel(this.selectedChannel.get());
final @Nullable Key selected = this.selectedChannelKey();
return selected == null ? null : this.channelRegistry.channel(selected);
}
public ChannelRegistry channelRegistry() {
@@ -23,7 +23,6 @@ import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Consumer;
@@ -31,7 +30,6 @@ import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.CarbonServer;
import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.common.CarbonChatInternal;
import net.draycia.carbon.common.DataDirectory;
import net.draycia.carbon.common.PeriodicTasks;
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
@@ -48,9 +46,6 @@ import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.entrypoint.EntrypointContainer;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.message.IMessageRenderer;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.ChatType;
import net.minecraft.resources.ResourceKey;
@@ -69,7 +64,6 @@ public final class CarbonChatFabric extends CarbonChatInternal<CarbonPlayerFabri
private CarbonChatFabric(
final Injector injector,
final Logger logger,
final @DataDirectory Path dataDirectory,
final @PeriodicTasks ScheduledExecutorService periodicTasks,
final ProfileCache profileCache,
final ProfileResolver profileResolver,
@@ -79,7 +73,6 @@ public final class CarbonChatFabric extends CarbonChatInternal<CarbonPlayerFabri
final CarbonMessages carbonMessages,
final CarbonEventHandler eventHandler,
final CarbonChannelRegistry channelRegistry,
final IMessageRenderer<Audience, String, Component, Component> renderer,
final Provider<MessagingManager> messagingManagerProvider,
@SuppressWarnings("unused") // Make sure it initializes now
final MinecraftServerHolder minecraftServerHolder
@@ -87,7 +80,6 @@ public final class CarbonChatFabric extends CarbonChatInternal<CarbonPlayerFabri
super(
injector,
logger,
dataDirectory,
periodicTasks,
profileCache,
profileResolver,
@@ -97,7 +89,6 @@ public final class CarbonChatFabric extends CarbonChatInternal<CarbonPlayerFabri
carbonMessages,
eventHandler,
channelRegistry,
renderer,
messagingManagerProvider
);
}
@@ -22,7 +22,6 @@ package net.draycia.carbon.fabric;
import cloud.commandframework.CommandManager;
import cloud.commandframework.fabric.FabricServerCommandManager;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Provider;
import com.google.inject.Provides;
import com.google.inject.Singleton;
@@ -31,12 +30,12 @@ import java.nio.file.Path;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.CarbonServer;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.CarbonCommonModule;
import net.draycia.carbon.common.DataDirectory;
import net.draycia.carbon.common.PlatformScheduler;
import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
import net.draycia.carbon.common.messages.CarbonMessageRenderer;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.users.ProfileResolver;
import net.draycia.carbon.common.users.UserManagerInternal;
@@ -46,9 +45,6 @@ import net.draycia.carbon.fabric.command.FabricPlayerCommander;
import net.draycia.carbon.fabric.users.FabricProfileResolver;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.message.IMessageRenderer;
import net.minecraft.server.level.ServerPlayer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -93,20 +89,6 @@ public final class CarbonChatFabricModule extends AbstractModule {
return commandManager;
}
@Provides
@Singleton
@SuppressWarnings("unchecked,unused")
public IMessageRenderer<Audience, String, Component, Component> messageRenderer(final Injector injector) {
return injector.getInstance(FabricMessageRenderer.class);
}
@Provides
@Singleton
@SuppressWarnings("unchecked,unused")
public IMessageRenderer<SourcedAudience, String, Component, Component> sourcedRenderer(final Injector injector) {
return injector.getInstance(FabricMessageRenderer.class);
}
@Override
public void configure() {
this.install(new CarbonCommonModule());
@@ -120,6 +102,7 @@ public final class CarbonChatFabricModule extends AbstractModule {
this.bind(PlatformScheduler.class).to(FabricScheduler.class);
this.bind(new TypeLiteral<UserManager<?>>() {}).to(FabricUserManager.class);
this.bind(new TypeLiteral<UserManagerInternal<?>>() {}).to(FabricUserManager.class);
this.bind(CarbonMessageRenderer.class).to(FabricMessageRenderer.class);
}
}
@@ -20,12 +20,14 @@
package net.draycia.carbon.fabric;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import io.github.miniplaceholders.api.MiniPlaceholders;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.config.ConfigFactory;
import net.draycia.carbon.common.messages.CarbonMessageRenderer;
import net.draycia.carbon.common.messages.SourcedAudience;
import net.draycia.carbon.common.users.ConsoleCarbonPlayer;
import net.draycia.carbon.fabric.users.CarbonPlayerFabric;
import net.fabricmc.loader.api.FabricLoader;
@@ -34,12 +36,12 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.moonshine.message.IMessageRenderer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public class FabricMessageRenderer<T extends Audience> implements IMessageRenderer<T, String, Component, Component> {
@Singleton
public class FabricMessageRenderer implements CarbonMessageRenderer {
private final ConfigFactory configFactory;
@@ -50,7 +52,7 @@ public class FabricMessageRenderer<T extends Audience> implements IMessageRender
@Override
public Component render(
final T receiver,
final Audience receiver,
final String intermediateMessage,
final Map<String, ? extends Component> resolvedPlaceholders,
final Method method,
@@ -20,10 +20,9 @@
package net.draycia.carbon.fabric.listeners;
import com.google.inject.Inject;
import net.draycia.carbon.api.event.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.KeyedRenderer;
import net.draycia.carbon.common.config.ConfigFactory;
import net.draycia.carbon.common.event.events.CarbonChatEventImpl;
import net.draycia.carbon.common.listeners.ChatListenerInternal;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.fabric.CarbonChatFabric;
@@ -62,20 +61,14 @@ public class FabricChatHandler extends ChatListenerInternal implements ServerMes
final @Nullable CarbonPlayer sender = this.carbonChat.userManager().user(serverPlayer.getUUID()).join();
final String content = chatMessage.decoratedContent().getString();
final @Nullable CarbonChatEvent chatEvent = this.prepareAndEmitChatEvent(sender, content, null);
final @Nullable CarbonChatEventImpl chatEvent = this.prepareAndEmitChatEvent(sender, content, null);
if (chatEvent == null || chatEvent.cancelled()) {
return false;
}
for (final var recipient : chatEvent.recipients()) {
Component renderedMessage = chatEvent.message();
for (final KeyedRenderer renderer : chatEvent.renderers()) {
renderedMessage = renderer.render(sender, recipient, renderedMessage, chatEvent.message());
}
final Component finishedMessage = renderedMessage;
final Component finishedMessage = chatEvent.renderFor(recipient);
final net.minecraft.network.chat.Component nativeMessage = FabricAudiences.nonWrappingSerializer().serialize(finishedMessage);
final PlayerChatMessage customChatMessage = new PlayerChatMessage(chatMessage.link(), chatMessage.signature(), chatMessage.signedBody(), nativeMessage, FilterMask.FULLY_FILTERED);
@@ -20,7 +20,6 @@
package net.draycia.carbon.fabric.users;
import com.google.inject.Provider;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
@@ -170,7 +169,7 @@ public class CarbonPlayerFabric extends WrappedCarbonPlayer implements Forwardin
return "default";
}
return this.user().getPrimaryGroup();
return super.primaryGroup();
}
@Override
@@ -179,13 +178,7 @@ public class CarbonPlayerFabric extends WrappedCarbonPlayer implements Forwardin
return List.of("default");
}
final var groups = new ArrayList<String>();
for (final var group : this.user().getInheritedGroups(this.user().getQueryOptions())) {
groups.add(group.getName());
}
return groups;
return super.groups();
}
}
@@ -24,13 +24,11 @@ import com.google.inject.Injector;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import github.scarsz.discordsrv.DiscordSRV;
import java.nio.file.Path;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import net.draycia.carbon.api.CarbonServer;
import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.common.CarbonChatInternal;
import net.draycia.carbon.common.DataDirectory;
import net.draycia.carbon.common.PeriodicTasks;
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
@@ -45,9 +43,6 @@ import net.draycia.carbon.paper.listeners.DiscordMessageListener;
import net.draycia.carbon.paper.listeners.PaperChatListener;
import net.draycia.carbon.paper.listeners.PaperPlayerJoinListener;
import net.draycia.carbon.paper.users.CarbonPlayerPaper;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.message.IMessageRenderer;
import org.apache.logging.log4j.LogManager;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
@@ -78,15 +73,14 @@ public final class CarbonChatPaper extends CarbonChatInternal<CarbonPlayerPaper>
final Provider<MessagingManager> messagingManager,
final CarbonServer carbonServer,
final PaperUserManager userManager,
@DataDirectory final Path dataDirectory,
@PeriodicTasks final ScheduledExecutorService periodicTasks,
final ProfileCache profileCache,
final ProfileResolver profileResolver,
final ExecutionCoordinatorHolder commandExecutor,
final IMessageRenderer<Audience, String, Component, Component> renderer
final ExecutionCoordinatorHolder commandExecutor
) {
super(
injector, LogManager.getLogger("CarbonChat"), dataDirectory,
injector,
LogManager.getLogger("CarbonChat"),
periodicTasks,
profileCache,
profileResolver,
@@ -96,7 +90,6 @@ public final class CarbonChatPaper extends CarbonChatInternal<CarbonPlayerPaper>
carbonMessages,
eventHandler,
channelRegistry,
renderer,
messagingManager
);
this.plugin = plugin;
@@ -23,7 +23,6 @@ import cloud.commandframework.CommandManager;
import cloud.commandframework.brigadier.CloudBrigadierManager;
import cloud.commandframework.paper.PaperCommandManager;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
@@ -32,12 +31,12 @@ import java.nio.file.Path;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.CarbonServer;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.CarbonCommonModule;
import net.draycia.carbon.common.DataDirectory;
import net.draycia.carbon.common.PlatformScheduler;
import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
import net.draycia.carbon.common.messages.CarbonMessageRenderer;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.users.ProfileResolver;
import net.draycia.carbon.common.users.UserManagerInternal;
@@ -47,9 +46,6 @@ import net.draycia.carbon.paper.command.PaperPlayerCommander;
import net.draycia.carbon.paper.messages.PaperMessageRenderer;
import net.draycia.carbon.paper.users.CarbonPlayerPaper;
import net.draycia.carbon.paper.users.PaperProfileResolver;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.message.IMessageRenderer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.Server;
@@ -106,20 +102,6 @@ public final class CarbonChatPaperModule extends AbstractModule {
return commandManager;
}
@Provides
@Singleton
@SuppressWarnings("unchecked,unused")
public IMessageRenderer<Audience, String, Component, Component> messageRenderer(final Injector injector) {
return injector.getInstance(PaperMessageRenderer.class);
}
@Provides
@Singleton
@SuppressWarnings("unchecked,unused")
public IMessageRenderer<SourcedAudience, String, Component, Component> sourcedRenderer(final Injector injector) {
return injector.getInstance(PaperMessageRenderer.class);
}
@Override
public void configure() {
this.install(new CarbonCommonModule());
@@ -134,6 +116,7 @@ public final class CarbonChatPaperModule extends AbstractModule {
this.bind(PlatformScheduler.class).to(PaperScheduler.class);
this.bind(new TypeLiteral<UserManager<?>>() {}).to(PaperUserManager.class);
this.bind(new TypeLiteral<UserManagerInternal<?>>() {}).to(PaperUserManager.class);
this.bind(CarbonMessageRenderer.class).to(PaperMessageRenderer.class);
this.install(new FactoryModuleBuilder().build(CarbonPlayerPaper.Factory.class));
}
@@ -22,9 +22,9 @@ package net.draycia.carbon.paper.listeners;
import com.google.inject.Inject;
import io.papermc.paper.event.player.AsyncChatEvent;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.event.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.config.ConfigFactory;
import net.draycia.carbon.common.event.events.CarbonChatEventImpl;
import net.draycia.carbon.common.listeners.ChatListenerInternal;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.kyori.adventure.audience.Audience;
@@ -63,7 +63,7 @@ public final class PaperChatListener extends ChatListenerInternal implements Lis
}
final String content = PlainTextComponentSerializer.plainText().serialize(event.message());
final @Nullable CarbonChatEvent chatEvent = this.prepareAndEmitChatEvent(sender, content, event.signedMessage());
final @Nullable CarbonChatEventImpl chatEvent = this.prepareAndEmitChatEvent(sender, content, event.signedMessage());
if (chatEvent == null || chatEvent.cancelled()) {
event.setCancelled(true);
@@ -78,7 +78,6 @@ public final class PaperChatListener extends ChatListenerInternal implements Lis
}
event.renderer(($, $$, $$$, recipient) -> {
var renderedMessage = chatEvent.message();
final var recipientUUID = recipient.get(Identity.UUID);
final Audience recipientViewer;
@@ -88,11 +87,7 @@ public final class PaperChatListener extends ChatListenerInternal implements Lis
recipientViewer = recipient;
}
for (final var renderer : chatEvent.renderers()) {
renderedMessage = renderer.render(sender, recipientViewer, renderedMessage, event.originalMessage());
}
return renderedMessage;
return chatEvent.renderFor(recipientViewer);
});
}
@@ -21,14 +21,16 @@ package net.draycia.carbon.paper.messages;
import com.google.common.base.Suppliers;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import io.github.miniplaceholders.api.MiniPlaceholders;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.function.Supplier;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.config.ConfigFactory;
import net.draycia.carbon.common.messages.CarbonMessageRenderer;
import net.draycia.carbon.common.messages.SourcedAudience;
import net.draycia.carbon.common.users.ConsoleCarbonPlayer;
import net.draycia.carbon.paper.CarbonChatPaper;
import net.kyori.adventure.audience.Audience;
@@ -36,7 +38,6 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.moonshine.message.IMessageRenderer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@@ -47,7 +48,8 @@ import org.checkerframework.framework.qual.DefaultQualifier;
import static java.util.Objects.requireNonNull;
@DefaultQualifier(NonNull.class)
public class PaperMessageRenderer<T extends Audience> implements IMessageRenderer<T, String, Component, Component> {
@Singleton
public class PaperMessageRenderer implements CarbonMessageRenderer {
private final Supplier<@MonotonicNonNull PlaceholderAPIMiniMessageParser> placeholderApiProcessor = Suppliers.memoize(() -> {
if (CarbonChatPaper.papiLoaded()) {
@@ -67,7 +69,7 @@ public class PaperMessageRenderer<T extends Audience> implements IMessageRendere
@Override
public Component render(
final T receiver,
final Audience receiver,
final String intermediateMessage,
final Map<String, ? extends Component> resolvedPlaceholders,
final Method method,
@@ -24,9 +24,7 @@ import com.google.inject.Injector;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer;
import java.nio.file.Path;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import net.draycia.carbon.api.CarbonChatProvider;
@@ -44,9 +42,6 @@ import net.draycia.carbon.velocity.listeners.VelocityListener;
import net.draycia.carbon.velocity.listeners.VelocityPlayerJoinListener;
import net.draycia.carbon.velocity.listeners.VelocityPlayerLeaveListener;
import net.draycia.carbon.velocity.users.CarbonPlayerVelocity;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.message.IMessageRenderer;
import org.apache.logging.log4j.LogManager;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -68,12 +63,10 @@ public class CarbonChatVelocity extends CarbonChatInternal<CarbonPlayerVelocity>
final ProxyServer proxyServer,
final Injector injector,
final PluginContainer pluginContainer,
@DataDirectory final Path dataDirectory,
@PeriodicTasks final ScheduledExecutorService periodicTasks,
final ProfileCache profileCache,
final ProfileResolver profileResolver,
final ExecutionCoordinatorHolder commandExecutor,
final IMessageRenderer<Audience, String, Component, Component> renderer,
final VelocityUserManager userManager,
final CarbonServerVelocity carbonServer,
final CarbonMessages carbonMessages,
@@ -82,8 +75,8 @@ public class CarbonChatVelocity extends CarbonChatInternal<CarbonPlayerVelocity>
final Provider<MessagingManager> messagingManager
) {
super(
injector, LogManager.getLogger(pluginContainer.getDescription().getId()),
dataDirectory,
injector,
LogManager.getLogger(pluginContainer.getDescription().getId()),
periodicTasks,
profileCache,
profileResolver,
@@ -93,7 +86,6 @@ public class CarbonChatVelocity extends CarbonChatInternal<CarbonPlayerVelocity>
carbonMessages,
eventHandler,
channelRegistry,
renderer,
messagingManager
);
this.proxyServer = proxyServer;
@@ -22,7 +22,6 @@ package net.draycia.carbon.velocity;
import cloud.commandframework.CommandManager;
import cloud.commandframework.velocity.VelocityCommandManager;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
@@ -33,12 +32,12 @@ import java.nio.file.Path;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.CarbonServer;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.CarbonCommonModule;
import net.draycia.carbon.common.DataDirectory;
import net.draycia.carbon.common.PlatformScheduler;
import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
import net.draycia.carbon.common.messages.CarbonMessageRenderer;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.users.ProfileResolver;
import net.draycia.carbon.common.users.UserManagerInternal;
@@ -46,9 +45,6 @@ import net.draycia.carbon.common.util.CloudUtils;
import net.draycia.carbon.velocity.command.VelocityCommander;
import net.draycia.carbon.velocity.command.VelocityPlayerCommander;
import net.draycia.carbon.velocity.users.VelocityProfileResolver;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.message.IMessageRenderer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -100,20 +96,6 @@ public final class CarbonChatVelocityModule extends AbstractModule {
return commandManager;
}
@Provides
@Singleton
@SuppressWarnings("unchecked,unused")
public IMessageRenderer<Audience, String, Component, Component> messageRenderer(final Injector injector) {
return injector.getInstance(VelocityMessageRenderer.class);
}
@Provides
@Singleton
@SuppressWarnings("unchecked,unused")
public IMessageRenderer<SourcedAudience, String, Component, Component> sourcedRenderer(final Injector injector) {
return injector.getInstance(VelocityMessageRenderer.class);
}
@Override
public void configure() {
this.install(new CarbonCommonModule());
@@ -126,6 +108,7 @@ public final class CarbonChatVelocityModule extends AbstractModule {
this.bind(PlatformScheduler.class).to(PlatformScheduler.RunImmediately.class);
this.bind(new TypeLiteral<UserManager<?>>() {}).to(VelocityUserManager.class);
this.bind(new TypeLiteral<UserManagerInternal<?>>() {}).to(VelocityUserManager.class);
this.bind(CarbonMessageRenderer.class).to(VelocityMessageRenderer.class);
}
}
@@ -20,13 +20,15 @@
package net.draycia.carbon.velocity;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.velocitypowered.api.plugin.PluginManager;
import io.github.miniplaceholders.api.MiniPlaceholders;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.config.ConfigFactory;
import net.draycia.carbon.common.messages.CarbonMessageRenderer;
import net.draycia.carbon.common.messages.SourcedAudience;
import net.draycia.carbon.common.users.ConsoleCarbonPlayer;
import net.draycia.carbon.velocity.users.CarbonPlayerVelocity;
import net.kyori.adventure.audience.Audience;
@@ -34,12 +36,12 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.moonshine.message.IMessageRenderer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public class VelocityMessageRenderer<T extends Audience> implements IMessageRenderer<T, String, Component, Component> {
@Singleton
public class VelocityMessageRenderer implements CarbonMessageRenderer {
private final ConfigFactory configFactory;
private final PluginManager pluginManager;
@@ -52,7 +54,7 @@ public class VelocityMessageRenderer<T extends Audience> implements IMessageRend
@Override
public Component render(
final T receiver,
final Audience receiver,
final String intermediateMessage,
final Map<String, ? extends Component> resolvedPlaceholders,
final Method method,
@@ -31,16 +31,14 @@ import com.velocitypowered.api.proxy.Player;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.event.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.api.util.KeyedRenderer;
import net.draycia.carbon.common.config.ConfigFactory;
import net.draycia.carbon.common.event.events.CarbonChatEventImpl;
import net.draycia.carbon.common.listeners.ChatListenerInternal;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.velocity.CarbonVelocityBootstrap;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -115,20 +113,14 @@ public final class VelocityChatListener extends ChatListenerInternal implements
final CarbonPlayer sender = this.userManager.user(event.getPlayer().getUniqueId()).join();
final String content = event.getResult().getMessage().orElse(event.getMessage());
final @Nullable CarbonChatEvent chatEvent = this.prepareAndEmitChatEvent(sender, content, null);
final @Nullable CarbonChatEventImpl chatEvent = this.prepareAndEmitChatEvent(sender, content, null);
if (chatEvent == null || chatEvent.cancelled()) {
return;
}
for (final Audience recipient : chatEvent.recipients()) {
Component renderedMessage = chatEvent.message();
for (final KeyedRenderer renderer : chatEvent.renderers()) {
renderedMessage = renderer.render(sender, recipient, renderedMessage, chatEvent.message());
}
recipient.sendMessage(renderedMessage);
recipient.sendMessage(chatEvent.renderFor(recipient));
}
}