mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-04 06:28:53 +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 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
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
@DefaultQualifier(NonNull.class)
|
@DefaultQualifier(NonNull.class)
|
||||||
public class CarbonChatEvent extends CancellableCarbonEvent {
|
public class CarbonChatEvent implements ResultedCarbonEvent<CarbonChatEvent.Result> {
|
||||||
|
|
||||||
private final List<KeyedRenderer> renderers;
|
private final List<KeyedRenderer> renderers;
|
||||||
|
|
||||||
private final CarbonPlayer sender;
|
private final CarbonPlayer sender;
|
||||||
|
|
||||||
private final Component originalMessage;
|
private final Component originalMessage;
|
||||||
|
|
||||||
private Component message;
|
private Component message;
|
||||||
|
|
||||||
private final List<? extends Audience> recipients;
|
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 sender the sender of the message
|
||||||
* @param originalMessage the original message that was sent
|
* @param originalMessage the original message that was sent
|
||||||
@@ -111,4 +110,28 @@ public class CarbonChatEvent extends CancellableCarbonEvent {
|
|||||||
return this.recipients;
|
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)
|
@DefaultQualifier(NonNull.class)
|
||||||
public final class CarbonEventHandler {
|
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.
|
* 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();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
|
import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
|
||||||
import static net.kyori.adventure.key.Key.key;
|
import static net.kyori.adventure.key.Key.key;
|
||||||
|
import static net.kyori.adventure.text.Component.empty;
|
||||||
|
|
||||||
public final class BukkitChatListener implements Listener {
|
public final class BukkitChatListener implements Listener {
|
||||||
|
|
||||||
@@ -51,8 +52,6 @@ public final class BukkitChatListener implements Listener {
|
|||||||
final var result = this.carbonChat.eventHandler().emit(chatEvent);
|
final var result = this.carbonChat.eventHandler().emit(chatEvent);
|
||||||
|
|
||||||
if (result.wasSuccessful()) {
|
if (result.wasSuccessful()) {
|
||||||
// TODO: send to channels
|
|
||||||
|
|
||||||
for (final var recipient : chatEvent.recipients()) {
|
for (final var recipient : chatEvent.recipients()) {
|
||||||
var component = chatEvent.message();
|
var component = chatEvent.message();
|
||||||
|
|
||||||
@@ -65,6 +64,13 @@ public final class BukkitChatListener implements Listener {
|
|||||||
recipient.sendMessage(component);
|
recipient.sendMessage(component);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (chatEvent.result().cancelled()) {
|
||||||
|
|
||||||
|
final var message = chatEvent.result().reason();
|
||||||
|
|
||||||
|
if (!message.equals(empty())) {
|
||||||
|
sender.sendMessage(message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// There's no guarantee that recipients is mutable.
|
// There's no guarantee that recipients is mutable.
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
|
import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
|
||||||
import static net.kyori.adventure.key.Key.key;
|
import static net.kyori.adventure.key.Key.key;
|
||||||
|
import static net.kyori.adventure.text.Component.empty;
|
||||||
|
|
||||||
public final class SpongeChatListener {
|
public final class SpongeChatListener {
|
||||||
|
|
||||||
@@ -74,6 +75,12 @@ public final class SpongeChatListener {
|
|||||||
recipient.sendMessage(component);
|
recipient.sendMessage(component);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (chatEvent.result().cancelled()) {
|
||||||
|
final var message = chatEvent.result().reason();
|
||||||
|
|
||||||
|
if (!message.equals(empty())) {
|
||||||
|
sender.sendMessage(message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user