mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
Change indentation and improve event
This commit is contained in:
1 parent
73c7f3f78f
commit
591a6606cc
31 files changed
+1072
-974
No files matched your search
@@ -5,6 +5,7 @@ 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;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
/**
|
||||
* The main plugin interface.<br>
|
||||
@@ -12,34 +13,32 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@DefaultQualifier(NonNull.class)
|
||||
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 plugin's logger.<br>
|
||||
* All messages will be logged through this.
|
||||
*
|
||||
* @return the plugin's logger
|
||||
* @since 2.0.0
|
||||
*/
|
||||
Logger logger();
|
||||
|
||||
/**
|
||||
* The event handler, used for listening to and emitting events.
|
||||
*
|
||||
* @return the event handler
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@NonNull CarbonEventHandler eventHandler();
|
||||
/**
|
||||
* The event handler, used for listening to and emitting events.
|
||||
*
|
||||
* @return the event handler
|
||||
* @since 2.0.0
|
||||
*/
|
||||
CarbonEventHandler eventHandler();
|
||||
|
||||
/**
|
||||
* The user manager, used for loading and obtaining {@link CarbonPlayer CarbonPlayers}.
|
||||
*
|
||||
* @return the user manager
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@NonNull UserManager userManager();
|
||||
/**
|
||||
* The user manager, used for loading and obtaining {@link CarbonPlayer CarbonPlayers}.
|
||||
*
|
||||
* @return the user manager
|
||||
* @since 2.0.0
|
||||
*/
|
||||
UserManager userManager();
|
||||
|
||||
}
|
||||
@@ -2,44 +2,44 @@ 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;
|
||||
|
||||
/**
|
||||
* Static accessor for the {@link CarbonChat} class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonChatProvider {
|
||||
|
||||
private CarbonChatProvider() {
|
||||
private 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
|
||||
}
|
||||
|
||||
return CarbonChatProvider.instance;
|
||||
}
|
||||
private static @Nullable CarbonChat instance;
|
||||
|
||||
/**
|
||||
* Registers the {@link CarbonChat} implementation.
|
||||
*
|
||||
* @param carbonChat the carbon implementation
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static void register(final CarbonChat carbonChat) {
|
||||
CarbonChatProvider.instance = carbonChat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the currently registered {@link CarbonChat} implementation.
|
||||
*
|
||||
* @return the registered carbon implementation
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static CarbonChat carbonChat() {
|
||||
if (CarbonChatProvider.instance == null) {
|
||||
throw new IllegalStateException("CarbonChat not initialized!"); // LuckPerms design go brrrr
|
||||
}
|
||||
|
||||
return CarbonChatProvider.instance;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package net.draycia.carbon.api.channels;
|
||||
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.util.ChatComponentRenderer;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
/**
|
||||
* ChatChannel interface, supplies a formatter and filters recipients.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public interface ChatChannel {
|
||||
|
||||
/**
|
||||
* Checks if the player may receive messages from this channel.
|
||||
*
|
||||
* @param player the player
|
||||
* @return if the player may receive this channel's messages
|
||||
* @since 2.0.0
|
||||
*/
|
||||
boolean mayReceiveMessages(final CarbonPlayer player);
|
||||
|
||||
/**
|
||||
* Checks if the player may send messages in this channel.
|
||||
*
|
||||
* @param player the player
|
||||
* @return if the player may send messages in this channel
|
||||
* @since 2.0.0
|
||||
*/
|
||||
boolean maySendMessages(final CarbonPlayer player);
|
||||
|
||||
/**
|
||||
* This channel's renderer.
|
||||
*
|
||||
* @return the renderer
|
||||
* @since 2.0.0
|
||||
*/
|
||||
ChatComponentRenderer renderer();
|
||||
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import net.kyori.adventure.audience.Audience;
|
||||
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;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -15,107 +16,101 @@ import java.util.Map;
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class CarbonChatEvent extends CancellableCarbonEvent {
|
||||
|
||||
private final @NonNull Map<Key, ChatComponentRenderer> renderers;
|
||||
private final Map<Key, ChatComponentRenderer> renderers;
|
||||
|
||||
private final @NonNull CarbonPlayer sender;
|
||||
private final CarbonPlayer sender;
|
||||
|
||||
private final @NonNull Component originalMessage;
|
||||
private final Component originalMessage;
|
||||
|
||||
private @NonNull Component message;
|
||||
private Component message;
|
||||
|
||||
private final @NonNull List<@NonNull ? extends Audience> recipients;
|
||||
private final List<? extends Audience> recipients;
|
||||
|
||||
/**
|
||||
* {@link CancellableCarbonEvent} that's called when players send messages in chat.
|
||||
*
|
||||
* @param sender the sender of the message
|
||||
* @param originalMessage the original message that was sent
|
||||
* @param recipients the recipients of the message
|
||||
* @param renderers the renderers of the message
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public CarbonChatEvent(
|
||||
final @NonNull CarbonPlayer sender,
|
||||
final @NonNull Component originalMessage,
|
||||
final @NonNull List<@NonNull ? extends Audience> recipients,
|
||||
final @NonNull Map<Key, ChatComponentRenderer> renderers
|
||||
) {
|
||||
this.sender = sender;
|
||||
this.originalMessage = originalMessage;
|
||||
this.message = originalMessage;
|
||||
this.recipients = recipients;
|
||||
this.renderers = renderers;
|
||||
}
|
||||
/**
|
||||
* {@link CancellableCarbonEvent} that's called when players send messages in chat.
|
||||
*
|
||||
* @param sender the sender of the message
|
||||
* @param originalMessage the original message that was sent
|
||||
* @param recipients the recipients of the message
|
||||
* @param renderers the renderers of the message
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public CarbonChatEvent(
|
||||
final CarbonPlayer sender,
|
||||
final Component originalMessage,
|
||||
final List<? extends Audience> recipients,
|
||||
final Map<Key, ChatComponentRenderer> renderers
|
||||
) {
|
||||
this.sender = sender;
|
||||
this.originalMessage = originalMessage;
|
||||
this.message = originalMessage;
|
||||
this.recipients = recipients;
|
||||
this.renderers = renderers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the renderers used to construct components for each of the recipients.
|
||||
*
|
||||
* @return The per-recipient component renderers.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public @NonNull Map<Key, ChatComponentRenderer> renderers() {
|
||||
return this.renderers;
|
||||
}
|
||||
/**
|
||||
* Get the renderers used to construct components for each of the recipients.
|
||||
*
|
||||
* @return The per-recipient component renderers.
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public Map<Key, ChatComponentRenderer> renderers() {
|
||||
return this.renderers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sender of the message.
|
||||
*
|
||||
* @return The message sender.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public CarbonPlayer sender() {
|
||||
return this.sender;
|
||||
}
|
||||
/**
|
||||
* Get the sender of the message.
|
||||
*
|
||||
* @return The message sender.
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public CarbonPlayer sender() {
|
||||
return this.sender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the original message that was sent.
|
||||
*
|
||||
* @return The original message.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public Component originalMessage() {
|
||||
return this.originalMessage;
|
||||
}
|
||||
/**
|
||||
* Get the original message that was sent.
|
||||
*
|
||||
* @return The original message.
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public Component originalMessage() {
|
||||
return this.originalMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the chat message that will be sent.
|
||||
*
|
||||
* @return The chat message.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public Component message() {
|
||||
return this.message;
|
||||
}
|
||||
/**
|
||||
* Get the chat message that will be sent.
|
||||
*
|
||||
* @return The chat message.
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public Component message() {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
/**
|
||||
* Set the chat message that will be sent.
|
||||
*
|
||||
* @param message new message
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public void message(final Component message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
/**
|
||||
* The recipients of the message.
|
||||
* List is mutable and elements may be added/removed.
|
||||
*
|
||||
* @return the recipients of the message.
|
||||
* entries may be players, console, or other audience implementations
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public @NonNull List<@NonNull ? extends Audience> recipients() {
|
||||
return this.recipients;
|
||||
}
|
||||
/**
|
||||
* The recipients of the message.
|
||||
* List is mutable and elements may be added/removed.
|
||||
*
|
||||
* @return the recipients of the message.
|
||||
* entries may be players, console, or other audience implementations
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public List<? extends Audience> recipients() {
|
||||
return this.recipients;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import net.kyori.event.EventSubscriber;
|
||||
import net.kyori.event.PostResult;
|
||||
import net.kyori.event.SimpleEventBus;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@@ -12,84 +13,83 @@ import java.util.function.Consumer;
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonEventHandler {
|
||||
|
||||
private final SimpleEventBus<CarbonEvent> eventBus = new SimpleEventBus<>(CarbonEvent.class);
|
||||
private final SimpleEventBus<CarbonEvent> eventBus = new SimpleEventBus<>(CarbonEvent.class);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
/**
|
||||
* 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 Class<T> eventClass, final EventSubscriber<T> subscriber) {
|
||||
this.eventBus.register(eventClass, subscriber);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
/**
|
||||
* 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 Class<T> eventClass, final int priority,
|
||||
final boolean consumeCancelled,
|
||||
final Consumer<T> consumer) {
|
||||
|
||||
@Override
|
||||
public boolean consumeCancelledEvents() {
|
||||
return consumeCancelled;
|
||||
}
|
||||
final var subscriber = new EventSubscriber<T>() {
|
||||
@Override
|
||||
public int postOrder() {
|
||||
return priority;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(final @NonNull T event) {
|
||||
consumer.accept(event);
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public boolean consumeCancelledEvents() {
|
||||
return consumeCancelled;
|
||||
}
|
||||
|
||||
this.eventBus.register(eventClass, subscriber);
|
||||
@Override
|
||||
public void invoke(final T event) {
|
||||
consumer.accept(event);
|
||||
}
|
||||
};
|
||||
|
||||
return subscriber;
|
||||
}
|
||||
this.eventBus.register(eventClass, 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);
|
||||
}
|
||||
return subscriber;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
/**
|
||||
* Unregisters the subscriber and prevents it from being executed.
|
||||
*
|
||||
* @param subscriber the subscriber
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public void unregister(final EventSubscriber<? super CarbonEvent> subscriber) {
|
||||
this.eventBus.unregister(subscriber);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 CarbonEvent event) {
|
||||
return this.eventBus.post(event);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
package net.draycia.carbon.api.users;
|
||||
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.identity.Identified;
|
||||
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 java.util.UUID;
|
||||
|
||||
@@ -13,52 +15,64 @@ import java.util.UUID;
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public interface CarbonPlayer extends Audience, Identified {
|
||||
|
||||
/**
|
||||
* Gets the player's username.
|
||||
*
|
||||
* @return the player's username
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@NonNull String username();
|
||||
/**
|
||||
* Gets the player's username.
|
||||
*
|
||||
* @return the player's username
|
||||
* @since 2.0.0
|
||||
*/
|
||||
String username();
|
||||
|
||||
/**
|
||||
* Gets the player's display name, shown in places like chat and tab menu.
|
||||
*
|
||||
* @return the player's display name
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@NonNull Component displayName();
|
||||
/**
|
||||
* Gets the player's display name, shown in places like chat and tab menu.
|
||||
*
|
||||
* @return the player's display name
|
||||
* @since 2.0.0
|
||||
*/
|
||||
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);
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* The player's UUID, often used for identification purposes.
|
||||
*
|
||||
* @return the player's UUID
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@NonNull UUID uuid();
|
||||
/**
|
||||
* The player's UUID, often used for identification purposes.
|
||||
*
|
||||
* @return the player's UUID
|
||||
* @since 2.0.0
|
||||
*/
|
||||
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();
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
Component createItemHoverComponent();
|
||||
|
||||
/**
|
||||
* The player's selected channel. If none has been set, returns the server default.
|
||||
*
|
||||
* @return the player's selected channel
|
||||
* @since 2.0.0
|
||||
*/
|
||||
ChatChannel selectedChannel();
|
||||
|
||||
/**
|
||||
* Sets the player's selected channel.
|
||||
*
|
||||
* @param chatChannel the channel
|
||||
* @since 2.0.0
|
||||
*/
|
||||
void selectedChannel(final ChatChannel chatChannel);
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package net.draycia.carbon.api.users;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -10,28 +11,25 @@ import java.util.UUID;
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public interface UserManager {
|
||||
|
||||
/**
|
||||
* Loads and returns a {@link CarbonPlayer} with the given {@link UUID}.
|
||||
*
|
||||
* @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);
|
||||
/**
|
||||
* Loads and returns a {@link CarbonPlayer} with the given {@link UUID}.
|
||||
*
|
||||
* @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 UUID uuid);
|
||||
|
||||
/**
|
||||
* Loads and returns a {@link CarbonPlayer} with the given username.
|
||||
*
|
||||
* @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);
|
||||
/**
|
||||
* Loads and returns a {@link CarbonPlayer} with the given username.
|
||||
*
|
||||
* @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 String username);
|
||||
|
||||
}
|
||||
@@ -5,6 +5,7 @@ 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;
|
||||
|
||||
/**
|
||||
* Renderer used to construct chat components on a per-player basis.
|
||||
@@ -12,26 +13,25 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@FunctionalInterface
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public interface ChatComponentRenderer {
|
||||
|
||||
/**
|
||||
* Renders a Component for the specified recipient.
|
||||
*
|
||||
* @param sender the player that sent the message
|
||||
* @param recipient a recipient of the message.
|
||||
* may be a player, console, or other Audience implementations
|
||||
* @param message the message being sent
|
||||
* @param originalMessage the original message that was sent
|
||||
*
|
||||
* @return the component to be shown to the recipient,
|
||||
* or null if the recipient should not receive the message
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@Nullable
|
||||
Component render(final @NonNull CarbonPlayer sender,
|
||||
final @NonNull Audience recipient,
|
||||
final @NonNull Component message,
|
||||
final @NonNull Component originalMessage);
|
||||
/**
|
||||
* Renders a Component for the specified recipient.
|
||||
*
|
||||
* @param sender the player that sent the message
|
||||
* @param recipient a recipient of the message.
|
||||
* may be a player, console, or other Audience implementations
|
||||
* @param message the message being sent
|
||||
* @param originalMessage the original message that was sent
|
||||
* @return the component to be shown to the recipient,
|
||||
* or null if the recipient should not receive the message
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@Nullable
|
||||
Component render(final CarbonPlayer sender,
|
||||
final Audience recipient,
|
||||
final Component message,
|
||||
final Component originalMessage);
|
||||
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyFormat;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -15,95 +16,92 @@ import java.util.regex.Pattern;
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class ColorUtils {
|
||||
|
||||
private 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;
|
||||
}
|
||||
|
||||
for (final NamedTextColor namedColor : NamedTextColor.NAMES.values()) {
|
||||
if (namedColor.toString().equalsIgnoreCase(input)) {
|
||||
return namedColor;
|
||||
}
|
||||
}
|
||||
|
||||
if (input.contains("&") || input.contains("§")) {
|
||||
input = input.replace("&", "§");
|
||||
|
||||
return LegacyComponentSerializer.legacySection().deserialize(input).color();
|
||||
}
|
||||
|
||||
return TextColor.fromCSSHexString(input);
|
||||
}
|
||||
|
||||
private static final @NonNull Pattern spigotLegacyRGB =
|
||||
Pattern.compile("[§&]x[§&]([0-9a-fA-F])[§&]([0-9a-fA-F])[§&]([0-9a-fA-F])[§&]([0-9a-fA-F])[§&]([0-9a-fA-F])[§&]([0-9a-fA-F])");
|
||||
|
||||
private static final @NonNull Pattern pluginRGB =
|
||||
Pattern.compile("[§&]#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])");
|
||||
|
||||
private static final @NonNull String hexReplacement = "<#$1$2$3$4$5$6>";
|
||||
|
||||
/**
|
||||
* 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
|
||||
output = spigotLegacyRGB.matcher(output).replaceAll(hexReplacement);
|
||||
|
||||
// Alternate RGB, TAB (neznamy) && KiteBoard
|
||||
output = pluginRGB.matcher(output).replaceAll(hexReplacement);
|
||||
|
||||
// Legacy Colors
|
||||
for (final char c : "0123456789abcdefABCDEF".toCharArray()) {
|
||||
final LegacyFormat format = LegacyComponentSerializer.parseChar(Character.toLowerCase(c));
|
||||
|
||||
if (format != null) {
|
||||
final TextColor color = format.color();
|
||||
|
||||
if (color != null) {
|
||||
output = output.replaceAll("[§&]" + c, "<" + color.asHexString() + ">");
|
||||
/**
|
||||
* 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(String input) {
|
||||
if (input.isEmpty()) {
|
||||
return NamedTextColor.WHITE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Legacy Formatting
|
||||
for (final char c : "klmnoKLMNO".toCharArray()) {
|
||||
final LegacyFormat format = LegacyComponentSerializer.parseChar(Character.toLowerCase(c));
|
||||
|
||||
if (format != null) {
|
||||
final TextDecoration decoration = format.decoration();
|
||||
|
||||
if (decoration != null) {
|
||||
output = output.replaceAll("[§&]" + c, "<" + decoration.name() + ">");
|
||||
for (final NamedTextColor namedColor : NamedTextColor.NAMES.values()) {
|
||||
if (namedColor.toString().equalsIgnoreCase(input)) {
|
||||
return namedColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (input.contains("&") || input.contains("§")) {
|
||||
input = input.replace("&", "§");
|
||||
|
||||
return LegacyComponentSerializer.legacySection().deserialize(input).color();
|
||||
}
|
||||
|
||||
return TextColor.fromCSSHexString(input);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
private static final Pattern spigotLegacyRGB =
|
||||
Pattern.compile("[§&]x[§&]([0-9a-fA-F])[§&]([0-9a-fA-F])[§&]([0-9a-fA-F])[§&]([0-9a-fA-F])[§&]([0-9a-fA-F])[§&]([0-9a-fA-F])");
|
||||
|
||||
private static final Pattern pluginRGB =
|
||||
Pattern.compile("[§&]#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])");
|
||||
|
||||
private static final String hexReplacement = "<#$1$2$3$4$5$6>";
|
||||
|
||||
/**
|
||||
* 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 String legacyToMiniMessage(final String input) {
|
||||
String output = input;
|
||||
|
||||
// Legacy RGB
|
||||
output = spigotLegacyRGB.matcher(output).replaceAll(hexReplacement);
|
||||
|
||||
// Alternate RGB, TAB (neznamy) && KiteBoard
|
||||
output = pluginRGB.matcher(output).replaceAll(hexReplacement);
|
||||
|
||||
// Legacy Colors
|
||||
for (final char c : "0123456789abcdefABCDEF".toCharArray()) {
|
||||
final @Nullable LegacyFormat format = LegacyComponentSerializer.parseChar(Character.toLowerCase(c));
|
||||
|
||||
if (format != null) {
|
||||
final @Nullable TextColor color = format.color();
|
||||
|
||||
if (color != null) {
|
||||
output = output.replaceAll("[§&]" + c, "<" + color.asHexString() + ">");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Legacy Formatting
|
||||
for (final char c : "klmnoKLMNO".toCharArray()) {
|
||||
final @Nullable LegacyFormat format = LegacyComponentSerializer.parseChar(Character.toLowerCase(c));
|
||||
|
||||
if (format != null) {
|
||||
final @Nullable TextDecoration decoration = format.decoration();
|
||||
|
||||
if (decoration != null) {
|
||||
output = output.replaceAll("[§&]" + c, "<" + decoration.name() + ">");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,18 +8,16 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
*
|
||||
* @param <K> the key type
|
||||
* @param <V> the value type
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface DefaultedKeyValueRegistry<K, V> extends DefaultedRegistryGetter<K, V> {
|
||||
|
||||
/**
|
||||
* Gets the default value.
|
||||
*
|
||||
* @return the default value
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@NonNull V defaultValue();
|
||||
/**
|
||||
* Gets the default value.
|
||||
*
|
||||
* @return the default value
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@NonNull V defaultValue();
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user