mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 14:38:49 +00:00
Event stuff
This commit is contained in:
1 parent
3506fc2fb8
commit
0c7b65d978
16 files changed
+181
-2
No files matched your search
@@ -4,6 +4,7 @@ dependencies {
|
||||
api(platform(libs.adventureBom))
|
||||
compileOnlyApi(libs.adventureApi)
|
||||
compileOnlyApi(libs.adventureTextSerializerPlain)
|
||||
compileOnlyApi(libs.adventureTextSerializerLegacy)
|
||||
compileOnlyApi(libs.adventureTextSerializerGson) {
|
||||
exclude("com.google.code.gson")
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package net.draycia.carbon.bukkit.util;
|
||||
package net.draycia.carbon.api.util;
|
||||
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
+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);
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package net.draycia.carbon.bukkit;
|
||||
|
||||
import io.papermc.lib.PaperLib;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.events.CarbonEventHandler;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@@ -24,6 +25,7 @@ import static net.kyori.adventure.text.format.NamedTextColor.WHITE;
|
||||
|
||||
public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
||||
private final Logger logger = LogManager.getLogger("CarbonChat");
|
||||
private final CarbonEventHandler eventHandler = new CarbonEventHandler();
|
||||
|
||||
private static final int BSTATS_PLUGIN_ID = 8720;
|
||||
|
||||
@@ -51,6 +53,12 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull CarbonEventHandler eventHandler() {
|
||||
// TODO: move to common
|
||||
return this.eventHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Component createComponent(final @NonNull UUID uuid) {
|
||||
final Player player = Bukkit.getPlayer(uuid); // This is temporary (it's not)
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package net.draycia.carbon.bukkit.listeners;
|
||||
|
||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
public class BukkitChatListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerchat(final @NonNull AsyncChatEvent event) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -45,6 +45,9 @@ dependencies:
|
||||
adventureTextSerializerPlain:
|
||||
group: net.kyori
|
||||
name: adventure-text-serializer-plain
|
||||
adventureTextSerializerLegacy:
|
||||
group: net.kyori
|
||||
name: adventure-text-serializer-legacy
|
||||
adventureSerializerConfigurate4:
|
||||
group: net.kyori
|
||||
name: adventure-serializer-configurate4
|
||||
|
||||
@@ -9,6 +9,7 @@ plugins {
|
||||
|
||||
dependencies {
|
||||
implementation(projects.carbonchatApi)
|
||||
implementation(libs.adventureTextSerializerLegacy)
|
||||
implementation(libs.cloudSponge)
|
||||
//implementation(libs.bstatsSponge) // not updated for api 8 yet
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package net.draycia.carbon.sponge;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.events.CarbonEventHandler;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -24,6 +25,7 @@ import static net.kyori.adventure.text.format.NamedTextColor.WHITE;
|
||||
public class CarbonChatSponge implements CarbonChat {
|
||||
private final PluginContainer pluginContainer;
|
||||
private final Logger logger;
|
||||
private final CarbonEventHandler eventHandler = new CarbonEventHandler();
|
||||
|
||||
private static final int BSTATS_PLUGIN_ID = 11279;
|
||||
|
||||
@@ -44,6 +46,12 @@ public class CarbonChatSponge implements CarbonChat {
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull CarbonEventHandler eventHandler() {
|
||||
// TODO: move to common
|
||||
return this.eventHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Component createComponent(final @NonNull UUID uuid) {
|
||||
final ServerPlayer player = Sponge.server().player(uuid).orElse(null);
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package net.draycia.carbon.sponge.listeners;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.spongepowered.api.event.Listener;
|
||||
import org.spongepowered.api.event.message.PlayerChatEvent;
|
||||
|
||||
public class SpongeChatListener {
|
||||
|
||||
@Listener
|
||||
public void onPlayerChat(final @NonNull PlayerChatEvent event) {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user