update checkstyle and bump carbon to 2.0.0-SNAPSHOT

This commit is contained in:
Draycia committed 2021-05-12 20:47:48 -07:00
1 parent 7abfbe2090
commit f3a734601e
15 files changed
+462 -154

No files matched your search

@@ -1,16 +1,45 @@
package net.draycia.carbon.api;
import net.draycia.carbon.api.events.CarbonEventHandler;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.UserManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
* The main plugin interface.<br>
* Instances may be obtained through {@link CarbonChatProvider#carbonChat()}.
*
* @since 1.0.0
*/
public interface CarbonChat {
/**
* The plugin's logger.<br>
* All messages will be logged through this.
*
* @return the plugin's logger
*
* @since 2.0.0
*/
@NonNull Logger logger();
/**
* The event handler, used for listening to and emitting events.
*
* @return the event handler
*
* @since 2.0.0
*/
@NonNull CarbonEventHandler eventHandler();
/**
* The user manager, used for loading and obtaining {@link CarbonPlayer CarbonPlayers}.
*
* @return the user manager
*
* @since 2.0.0
*/
@NonNull UserManager userManager();
}
@@ -3,6 +3,11 @@ package net.draycia.carbon.api;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Static accessor for the {@link CarbonChat} class.
*
* @since 1.0.0
*/
public final class CarbonChatProvider {
private CarbonChatProvider() {
@@ -11,10 +16,24 @@ public final class CarbonChatProvider {
private static @Nullable CarbonChat instance;
/**
* Registers the {@link CarbonChat} implementation.
*
* @param carbonChat the carbon implementation
*
* @since 1.0.0
*/
public static void register(final @NonNull CarbonChat carbonChat) {
CarbonChatProvider.instance = carbonChat;
}
/**
* Gets the currently registered {@link CarbonChat} implementation.
*
* @return the registered carbon implementation
*
* @since 1.0.0
*/
public static @NonNull CarbonChat carbonChat() {
if (CarbonChatProvider.instance == null) {
throw new IllegalStateException("CarbonChat not initialized!"); // LuckPerms design go brrrr
@@ -4,6 +4,8 @@ import net.kyori.event.Cancellable;
/**
* Represents a {@link CarbonEvent} that's cancellable.
*
* @since 2.0.0
*/
public abstract class CancellableCarbonEvent extends Cancellable.Impl implements CarbonEvent {
@@ -11,6 +11,8 @@ import static net.kyori.adventure.text.Component.translatable;
/**
* {@link CancellableCarbonEvent} that's called when chat components are rendered for online players.
*
* @since 2.0.0
*/
public class CarbonChatEvent extends CancellableCarbonEvent {
@@ -25,6 +27,15 @@ public class CarbonChatEvent extends CancellableCarbonEvent {
private final @NonNull List<@NonNull CarbonPlayer> recipients;
/**
* {@link CancellableCarbonEvent} that's called when chat components are rendered for online players.
*
* @param sender the sender of the message
* @param originalMessage the original message that was sent
* @param recipients the recipients of the message
*
* @since 2.0.0
*/
public CarbonChatEvent(
final @NonNull CarbonPlayer sender,
final @NonNull Component originalMessage,
@@ -39,7 +50,9 @@ public class CarbonChatEvent extends CancellableCarbonEvent {
/**
* Set the {@link PlayerComponentRenderer} for the event.
*
* @param renderer new {@link PlayerComponentRenderer}
* @param renderer the renderer
*
* @since 2.0.0
*/
public void renderer(final @NonNull PlayerComponentRenderer renderer) {
this.renderer = renderer;
@@ -49,6 +62,8 @@ public class CarbonChatEvent extends CancellableCarbonEvent {
* Get the renderer used to construct components for each player in recipients.
*
* @return The per-player component renderer.
*
* @since 2.0.0
*/
public @NonNull PlayerComponentRenderer renderer() {
return this.renderer;
@@ -58,6 +73,8 @@ public class CarbonChatEvent extends CancellableCarbonEvent {
* Get the sender of the message.
*
* @return The message sender.
*
* @since 2.0.0
*/
public CarbonPlayer sender() {
return this.sender;
@@ -67,6 +84,8 @@ public class CarbonChatEvent extends CancellableCarbonEvent {
* Get the original message that was sent.
*
* @return The original message.
*
* @since 2.0.0
*/
public Component originalMessage() {
return this.originalMessage;
@@ -76,6 +95,8 @@ public class CarbonChatEvent extends CancellableCarbonEvent {
* Get the chat message that will be sent.
*
* @return The chat message.
*
* @since 2.0.0
*/
public Component message() {
return this.message;
@@ -85,6 +106,8 @@ public class CarbonChatEvent extends CancellableCarbonEvent {
* Set the chat message that will be sent.
*
* @param message new message
*
* @since 2.0.0
*/
public void message(final @NonNull Component message) {
this.message = message;
@@ -95,6 +118,8 @@ public class CarbonChatEvent extends CancellableCarbonEvent {
* List is mutable and players may be added/removed.
*
* @return The players that will receive this message.
*
* @since 2.0.0
*/
public @NonNull List<@NonNull CarbonPlayer> recipients() {
return this.recipients;
@@ -1,5 +1,10 @@
package net.draycia.carbon.api.events;
/**
* Interface to represent that an event belongs to carbon.
*
* @since 1.0.0
*/
public interface CarbonEvent {
}
@@ -7,16 +7,44 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.function.Consumer;
/**
* Event handler for listening to and emitting carbon events.
*
* @since 1.0.0
*/
public final class CarbonEventHandler {
private final SimpleEventBus<CarbonEvent> eventBus = new SimpleEventBus<>(CarbonEvent.class);
public <T extends CarbonEvent> void register(final @NonNull Class<T> clazz, final @NonNull EventSubscriber<T> subscriber) {
this.eventBus.register(clazz, subscriber);
/**
* Registers a subscriber for the given event class.
*
* @param eventClass the class to listen for
* @param subscriber the subscriber that's executed when the event is emitted
* @param <T> the class to listen for
*
* @since 1.0.0
*/
public <T extends CarbonEvent> void register(final @NonNull Class<T> eventClass, final @NonNull EventSubscriber<T> subscriber) {
this.eventBus.register(eventClass, subscriber);
}
public <T extends CarbonEvent> void register(final @NonNull Class<T> clazz, final int priority, final boolean consumeCancelled, final @NonNull Consumer<T> consumer) {
this.eventBus.register(clazz, new EventSubscriber<>() {
/**
* Registers a subscriber for the given event class.<br>
* Includes extra values to control when the consumer is executed.
*
* @param eventClass the class to listen for
* @param priority the priority of the consumer
* @param consumeCancelled if the consumer should be executed if the event is cancelled early
* @param consumer the consumer that's executed when the event is emitted
* @param <T> the class to listen for
*
* @return the subscriber, so that it may be unregistered
*
* @since 2.0.0
*/
public <T extends CarbonEvent> EventSubscriber<T> register(final @NonNull Class<T> eventClass, final int priority, final boolean consumeCancelled, final @NonNull Consumer<T> consumer) {
final var subscriber = new EventSubscriber<T>() {
@Override
public int postOrder() {
return priority;
@@ -31,14 +59,36 @@ public final class CarbonEventHandler {
public void invoke(final @NonNull T event) {
consumer.accept(event);
}
});
};
this.eventBus.register(eventClass, subscriber);
return subscriber;
}
/**
* Unregisters the subscriber and prevents it from being executed.
*
* @param subscriber the subscriber
*
* @since 1.0.0
*/
public void unregister(final @NonNull EventSubscriber<? super CarbonEvent> subscriber) {
this.eventBus.unregister(subscriber);
}
public PostResult post(final @NonNull CarbonEvent event) {
/**
* Emits the supplied event and returns its result.<br>
* Events are modified in place, so care should be taken to keep a reference to
* the event while it's being emitted.
*
* @param event the event to be emitted
*
* @return the result
*
* @since 2.0.0
*/
public PostResult emit(final @NonNull CarbonEvent event) {
return this.eventBus.post(event);
}
@@ -8,31 +8,57 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.UUID;
/**
* Generic abstraction for players.
*
* @since 2.0.0
*/
public interface CarbonPlayer extends Audience, Identified {
/**
* Gets the player's username.
*
* @return The player's username.
* @return the player's username
*
* @since 2.0.0
*/
@NonNull String username();
/**
* Gets the player's display name, shown in places like chat and tab menu.
*
* @return The player's display name.
* @return the player's display name
*
* @since 2.0.0
*/
@NonNull Component displayName();
/**
* Sets the player's display name.<br>
* Setting null is equivalent to setting the display name to the username.
*
* @param displayName the new display name
*
* @since 2.0.0
*/
void displayName(final @Nullable Component displayName);
/**
* Gets the player's UUID.
* The player's UUID, often used for identification purposes.
*
* @return The player's UUID.
* @return the player's UUID
*
* @since 2.0.0
*/
@NonNull UUID uuid();
/**
* Creates a {@link Component} with a content and item hover given the player's actively held item.
*
* @return the player's held item as an item hover component
*
* @since 2.0.0
*/
@NonNull Component createItemHoverComponent();
}
@@ -5,6 +5,11 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.UUID;
/**
* Manager used to load and obtain {@link CarbonPlayer CarbonPlayers}.
*
* @since 2.0.0
*/
public interface UserManager {
/**
@@ -13,6 +18,8 @@ public interface UserManager {
* @param uuid The player's UUID.
*
* @return The {@link CarbonPlayer}, or null if the player doesn't exist.
*
* @since 2.0.0
*/
@Nullable CarbonPlayer carbonPlayer(final @NonNull UUID uuid);
@@ -22,6 +29,8 @@ public interface UserManager {
* @param username The player's username.
*
* @return The {@link CarbonPlayer}, or null if the player doesn't exist.
*
* @since 2.0.0
*/
@Nullable CarbonPlayer carbonPlayer(final @NonNull String username);
@@ -10,12 +10,27 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.regex.Pattern;
/**
* Basic color related utilities.
*
* @since 1.0.0
*/
public final class ColorUtils {
private ColorUtils() {
}
/**
* Parses the input into a color.<br>
* Supports named colors, legacy, and hex inputs.
*
* @param input the color input
*
* @return the color
*
* @since 1.0.0
*/
public static @Nullable TextColor parseColor(@NonNull String input) {
if (input.isEmpty()) {
return NamedTextColor.WHITE;
@@ -44,7 +59,16 @@ public final class ColorUtils {
private static final @NonNull String hexReplacement = "<#$1$2$3$4$5$6>";
public static @NonNull String translateAlternateColors(final @NonNull String input) {
/**
* Converts the input legacy, legacy rgb, and alternate color formats to MiniMessage color tags.
*
* @param input the message to convert
*
* @return the converted message
*
* @since 1.0.0
*/
public static @NonNull String legacyToMiniMessage(final @NonNull String input) {
String output = input;
// Legacy RGB
@@ -4,10 +4,12 @@ import net.kyori.registry.DefaultedRegistryGetter;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
* An extension of the DefaultedRegistryGetter that declares a defaultValue method
* An extension of the DefaultedRegistryGetter that declares a defaultValue method.
*
* @param <K> the key type
* @param <V> the value type
*
* @since 1.0.0
*/
public interface DefaultedKeyValueRegistry<K, V> extends DefaultedRegistryGetter<K, V> {
@@ -15,6 +17,8 @@ public interface DefaultedKeyValueRegistry<K, V> extends DefaultedRegistryGetter
* Gets the default value.
*
* @return the default value
*
* @since 1.0.0
*/
@NonNull V defaultValue();
@@ -5,6 +5,11 @@ import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Renderer used to construct chat components on a per-player basis.
*
* @since 2.0.0
*/
@FunctionalInterface
public interface PlayerComponentRenderer {
@@ -16,7 +21,9 @@ public interface PlayerComponentRenderer {
* @param message The message being sent.
*
* @return The component to be shown to the recipient,
* or null if the recipient should not receive the message.
* or null if the recipient should not receive the message.
*
* @since 2.0.0
*/
@Nullable
Component render(final @NonNull CarbonPlayer sender,