mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Event stuff
This commit is contained in:
1 parent
3506fc2fb8
commit
0c7b65d978
16 files changed
+181
-2
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package net.draycia.carbon.api;
|
||||
|
||||
import net.draycia.carbon.api.events.CarbonEventHandler;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -10,6 +11,8 @@ public interface CarbonChat {
|
||||
|
||||
@NonNull Logger logger();
|
||||
|
||||
@NonNull CarbonEventHandler eventHandler();
|
||||
|
||||
@NonNull Component createComponent(final @NonNull UUID uuid);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package net.draycia.carbon.api.events;
|
||||
|
||||
import net.kyori.event.Cancellable;
|
||||
|
||||
public abstract class CancellableCarbonEvent extends Cancellable.Impl implements CarbonEvent {
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package net.draycia.carbon.api.events;
|
||||
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.util.PlayerComponentRenderer;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static net.kyori.adventure.text.Component.translatable;
|
||||
|
||||
public class CarbonChatEvent extends CancellableCarbonEvent {
|
||||
private @NonNull PlayerComponentRenderer renderer = (sender, recipient, message) -> {
|
||||
return translatable("chat.type.text", sender.displayName(), message);
|
||||
};
|
||||
|
||||
private final @NonNull List<@NonNull CarbonPlayer> recipients;
|
||||
|
||||
public CarbonChatEvent(final @NonNull List<@NonNull CarbonPlayer> recipients) {
|
||||
this.recipients = recipients;
|
||||
}
|
||||
|
||||
public void renderer(final @NonNull PlayerComponentRenderer renderer) {
|
||||
this.renderer = renderer;
|
||||
}
|
||||
|
||||
/**
|
||||
* The per-player component renderer.
|
||||
*/
|
||||
public @NonNull PlayerComponentRenderer renderer() {
|
||||
return this.renderer;
|
||||
}
|
||||
|
||||
/**
|
||||
* The players that will receive the message.
|
||||
* List is mutable and players may be added/removed.
|
||||
* @return The players that will receive this message.
|
||||
*/
|
||||
public @NonNull List<@NonNull CarbonPlayer> recipients() {
|
||||
return this.recipients;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package net.draycia.carbon.api.events;
|
||||
|
||||
public interface CarbonEvent {
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package net.draycia.carbon.api.events;
|
||||
|
||||
import net.kyori.event.EventSubscriber;
|
||||
import net.kyori.event.PostResult;
|
||||
import net.kyori.event.SimpleEventBus;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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<T>() {
|
||||
@Override
|
||||
public int postOrder() {
|
||||
return priority;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean consumeCancelledEvents() {
|
||||
return consumeCancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(final @NonNull T event) {
|
||||
consumer.accept(event);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void unregister(final @NonNull EventSubscriber<? super CarbonEvent> subscriber) {
|
||||
this.eventBus.unregister(subscriber);
|
||||
}
|
||||
|
||||
public PostResult post(final @NonNull CarbonEvent event) {
|
||||
return this.eventBus.post(event);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package net.draycia.carbon.api.users;
|
||||
|
||||
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;
|
||||
|
||||
public abstract class CarbonPlayer implements Audience, Identified {
|
||||
public abstract @NonNull Component displayName();
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package net.draycia.carbon.api.util;
|
||||
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
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 java.util.regex.Pattern;
|
||||
|
||||
public final class ColorUtils {
|
||||
|
||||
private ColorUtils() {
|
||||
|
||||
}
|
||||
|
||||
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>";
|
||||
|
||||
public static @NonNull String translateAlternateColors(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() + ">");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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() + ">");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package net.draycia.carbon.api;
|
||||
package net.draycia.carbon.api.util;
|
||||
|
||||
import net.kyori.registry.DefaultedRegistryGetter;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -0,0 +1,22 @@
|
||||
package net.draycia.carbon.api.util;
|
||||
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PlayerComponentRenderer {
|
||||
/**
|
||||
* Renders a Component for the specified recipient.
|
||||
* @param sender The player that sent the message.
|
||||
* @param recipient The player receiving the message.
|
||||
* @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.
|
||||
*/
|
||||
@Nullable
|
||||
Component render(final @NonNull CarbonPlayer sender,
|
||||
final @NonNull CarbonPlayer recipient,
|
||||
final @NonNull Component message);
|
||||
}
|
||||
Reference in new issue
Block a user