mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-04 14:38:49 +00:00
1 parent
0eae5b9d45
commit
6518cecce2
5 files changed
+37
-52
No files matched your search
@@ -1,9 +1,10 @@
|
|||||||
package net.draycia.carbon.api.events;
|
package net.draycia.carbon.api.events;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
import net.kyori.event.EventBus;
|
||||||
import net.kyori.event.EventSubscriber;
|
import net.kyori.event.EventSubscriber;
|
||||||
|
import net.kyori.event.EventSubscription;
|
||||||
import net.kyori.event.PostResult;
|
import net.kyori.event.PostResult;
|
||||||
import net.kyori.event.SimpleEventBus;
|
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||||
|
|
||||||
@@ -15,16 +16,12 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
|||||||
@DefaultQualifier(NonNull.class)
|
@DefaultQualifier(NonNull.class)
|
||||||
public final class CarbonEventHandler {
|
public final class CarbonEventHandler {
|
||||||
|
|
||||||
private final SimpleEventBus<CarbonEvent> eventBus = new SimpleEventBus<>(CarbonEvent.class) {
|
private final EventBus<CarbonEvent> eventBus = EventBus.create(CarbonEvent.class, (type, event, subscriber) -> {
|
||||||
@Override
|
if (event instanceof ResultedCarbonEvent<?> rce) {
|
||||||
protected boolean eventCancelled(final @NonNull CarbonEvent event) {
|
return !rce.result().cancelled();
|
||||||
if (event instanceof ResultedCarbonEvent<?> resultedCarbonEvent) {
|
|
||||||
return !resultedCarbonEvent.result().cancelled();
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
return false;
|
});
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a subscriber for the given event class.
|
* Registers a subscriber for the given event class.
|
||||||
@@ -32,10 +29,14 @@ public final class CarbonEventHandler {
|
|||||||
* @param eventClass the class to listen for
|
* @param eventClass the class to listen for
|
||||||
* @param subscriber the subscriber that's executed when the event is emitted
|
* @param subscriber the subscriber that's executed when the event is emitted
|
||||||
* @param <T> the class to listen for
|
* @param <T> the class to listen for
|
||||||
* @since 1.0.0
|
* @return the subscription, so that it may be unregistered
|
||||||
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
public <T extends CarbonEvent> void register(final Class<T> eventClass, final EventSubscriber<T> subscriber) {
|
public <T extends CarbonEvent> EventSubscription subscribe(
|
||||||
this.eventBus.register(eventClass, subscriber);
|
final Class<T> eventClass,
|
||||||
|
final EventSubscriber<T> subscriber
|
||||||
|
) {
|
||||||
|
return this.eventBus.subscribe(eventClass, subscriber);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -44,47 +45,19 @@ public final class CarbonEventHandler {
|
|||||||
*
|
*
|
||||||
* @param eventClass the class to listen for
|
* @param eventClass the class to listen for
|
||||||
* @param priority the priority of the consumer
|
* @param priority the priority of the consumer
|
||||||
* @param consumeCancelled if the consumer should be executed if the event is cancelled early
|
* @param acceptsCancelled 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 consumer the consumer that's executed when the event is emitted
|
||||||
* @param <T> the class to listen for
|
* @param <T> the class to listen for
|
||||||
* @return the subscriber, so that it may be unregistered
|
* @return the subscription, so that it may be unregistered
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
public <T extends CarbonEvent> EventSubscriber<T> register(
|
public <T extends CarbonEvent> EventSubscription subscribe(
|
||||||
final Class<T> eventClass, final int priority,
|
final Class<T> eventClass,
|
||||||
final boolean consumeCancelled,
|
final int priority,
|
||||||
final Consumer<T> consumer) {
|
final boolean acceptsCancelled,
|
||||||
|
final Consumer<T> consumer
|
||||||
final var subscriber = new EventSubscriber<T>() {
|
) {
|
||||||
@Override
|
return this.eventBus.subscribe(eventClass, new EventSubscriberImpl<>(consumer, priority, acceptsCancelled));
|
||||||
public int postOrder() {
|
|
||||||
return priority;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean consumeCancelledEvents() {
|
|
||||||
return consumeCancelled;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void invoke(final 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 EventSubscriber<? super CarbonEvent> subscriber) {
|
|
||||||
this.eventBus.unregister(subscriber);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package net.draycia.carbon.api.events;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import net.kyori.event.EventSubscriber;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
|
record EventSubscriberImpl<T extends CarbonEvent>(Consumer<T> consumer, int postOrder, boolean acceptsCancelled) implements EventSubscriber<T> {
|
||||||
|
@Override
|
||||||
|
public void on(final @NonNull T event) {
|
||||||
|
this.consumer.accept(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -53,4 +53,5 @@ public class BasicChatChannel implements ChatChannel {
|
|||||||
public @NonNull Key key() {
|
public @NonNull Key key() {
|
||||||
return this.key;
|
return this.key;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -26,7 +26,7 @@ versions:
|
|||||||
bstats: 2.2.1
|
bstats: 2.2.1
|
||||||
paperApi: 1.16.5-R0.1-SNAPSHOT
|
paperApi: 1.16.5-R0.1-SNAPSHOT
|
||||||
paperLib: 1.0.6
|
paperLib: 1.0.6
|
||||||
kyoriEventApi: 3.0.0
|
kyoriEventApi: 5.0.0-SNAPSHOT
|
||||||
kyoriRegistry: 1.0.0-SNAPSHOT
|
kyoriRegistry: 1.0.0-SNAPSHOT
|
||||||
log4j: 2.11.2
|
log4j: 2.11.2
|
||||||
moonshine: 0.2.4-SNAPSHOT
|
moonshine: 0.2.4-SNAPSHOT
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package net.draycia.carbon.sponge.listeners;
|
|||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import net.draycia.carbon.api.channels.ChatChannel;
|
|
||||||
import net.draycia.carbon.api.events.CarbonChatEvent;
|
import net.draycia.carbon.api.events.CarbonChatEvent;
|
||||||
import net.draycia.carbon.api.util.KeyedRenderer;
|
import net.draycia.carbon.api.util.KeyedRenderer;
|
||||||
import net.draycia.carbon.common.channels.BasicChatChannel;
|
import net.draycia.carbon.common.channels.BasicChatChannel;
|
||||||
|
|||||||
Reference in new issue
Block a user