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

@@ -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.
@@ -1,32 +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.event.events;
// TODO: emit event on server shutdown
import net.draycia.carbon.api.event.CarbonEvent;
/**
* An event that's called when the server shuts down.
*
* @since 2.1.0
*/
public class CarbonShutdownEvent implements CarbonEvent {
}
@@ -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);
}
}