mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Refactor event cancellation (#143)
This commit is contained in:
6 files changed
+108
-22
No files matched your search
@@ -1,12 +0,0 @@
|
||||
package net.draycia.carbon.api.events;
|
||||
|
||||
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 {
|
||||
|
||||
}
|
||||
@@ -9,26 +9,25 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static net.kyori.adventure.text.Component.empty;
|
||||
|
||||
/**
|
||||
* {@link CancellableCarbonEvent} that's called when chat components are rendered for online players.
|
||||
* {@link ResultedCarbonEvent} that's called when chat components are rendered for online players.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class CarbonChatEvent extends CancellableCarbonEvent {
|
||||
public class CarbonChatEvent implements ResultedCarbonEvent<CarbonChatEvent.Result> {
|
||||
|
||||
private final List<KeyedRenderer> renderers;
|
||||
|
||||
private final CarbonPlayer sender;
|
||||
|
||||
private final Component originalMessage;
|
||||
|
||||
private Component message;
|
||||
|
||||
private final List<? extends Audience> recipients;
|
||||
private Result result = Result.ALLOWED;
|
||||
|
||||
/**
|
||||
* {@link CancellableCarbonEvent} that's called when players send messages in chat.
|
||||
* {@link ResultedCarbonEvent} that's called when players send messages in chat.
|
||||
*
|
||||
* @param sender the sender of the message
|
||||
* @param originalMessage the original message that was sent
|
||||
@@ -111,4 +110,28 @@ public class CarbonChatEvent extends CancellableCarbonEvent {
|
||||
return this.recipients;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result result() {
|
||||
return this.result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void result(final Result result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public record Result(boolean cancelled, Component reason) implements ResultedCarbonEvent.Result {
|
||||
|
||||
private static final Result ALLOWED = new Result(true, empty());
|
||||
|
||||
public static Result allowed() {
|
||||
return ALLOWED;
|
||||
}
|
||||
|
||||
public static Result denied(final Component reason) {
|
||||
return new Result(false, reason);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,7 +16,16 @@ import java.util.function.Consumer;
|
||||
@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) {
|
||||
@Override
|
||||
protected boolean eventCancelled(final @NonNull CarbonEvent event) {
|
||||
if (event instanceof ResultedCarbonEvent<?> resultedCarbonEvent) {
|
||||
return !resultedCarbonEvent.result().cancelled();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Registers a subscriber for the given event class.
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package net.draycia.carbon.api.events;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
/**
|
||||
* Represents a {@link CarbonEvent} that has a result.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public interface ResultedCarbonEvent<R extends ResultedCarbonEvent.Result> extends CarbonEvent {
|
||||
|
||||
/**
|
||||
* The result of the event.
|
||||
*
|
||||
* @return the event's result
|
||||
* @since 2.0.0
|
||||
*/
|
||||
R result();
|
||||
|
||||
/**
|
||||
* Sets the result of the event.
|
||||
*
|
||||
* @param result the result
|
||||
* @since 2.0.0
|
||||
*/
|
||||
void result(final R result);
|
||||
|
||||
/**
|
||||
* Represents the result of the event.
|
||||
* @since 2.0.0
|
||||
*/
|
||||
interface Result {
|
||||
|
||||
/**
|
||||
* If the event is cancelled to continue execution.<br>
|
||||
*
|
||||
* @return if listeners will continue executing
|
||||
* @since 2.0.0
|
||||
*/
|
||||
boolean cancelled();
|
||||
|
||||
/**
|
||||
* The reason of the result.<br>
|
||||
* Typically only present if allowed is false.
|
||||
*
|
||||
* @return the reason of the result
|
||||
* @since 2.0.0
|
||||
*/
|
||||
Component reason();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user