mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-23 16:41:35 +01:00
Added ignoreCancelled option for EventListener
This commit is contained in:
parent
12a215349d
commit
0bd4a68290
@ -1,5 +1,6 @@
|
|||||||
package net.minestom.server.event;
|
package net.minestom.server.event;
|
||||||
|
|
||||||
|
import net.minestom.server.event.trait.CancellableEvent;
|
||||||
import org.jetbrains.annotations.Contract;
|
import org.jetbrains.annotations.Contract;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@ -55,6 +56,7 @@ public interface EventListener<T extends Event> {
|
|||||||
class Builder<T extends Event> {
|
class Builder<T extends Event> {
|
||||||
private final Class<T> eventType;
|
private final Class<T> eventType;
|
||||||
private final List<Predicate<T>> filters = new ArrayList<>();
|
private final List<Predicate<T>> filters = new ArrayList<>();
|
||||||
|
private boolean ignoreCancelled;
|
||||||
private int expireCount;
|
private int expireCount;
|
||||||
private Predicate<T> expireWhen;
|
private Predicate<T> expireWhen;
|
||||||
private Consumer<T> handler;
|
private Consumer<T> handler;
|
||||||
@ -73,6 +75,19 @@ public interface EventListener<T extends Event> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specifies if the handler should still be called if {@link CancellableEvent#isCancelled()} returns {@code true}.
|
||||||
|
* <p>
|
||||||
|
* Default is set to {@code false}.
|
||||||
|
*
|
||||||
|
* @param ignoreCancelled True to still process the event when cancelled
|
||||||
|
*/
|
||||||
|
@Contract(value = "_ -> this")
|
||||||
|
public @NotNull EventListener.Builder<T> ignoreCancelled(boolean ignoreCancelled) {
|
||||||
|
this.ignoreCancelled = ignoreCancelled;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes this listener after it has been executed the given number of times.
|
* Removes this listener after it has been executed the given number of times.
|
||||||
*
|
*
|
||||||
@ -108,6 +123,7 @@ public interface EventListener<T extends Event> {
|
|||||||
|
|
||||||
@Contract(value = "-> new", pure = true)
|
@Contract(value = "-> new", pure = true)
|
||||||
public @NotNull EventListener<T> build() {
|
public @NotNull EventListener<T> build() {
|
||||||
|
final boolean ignoreCancelled = this.ignoreCancelled;
|
||||||
AtomicInteger expirationCount = new AtomicInteger(this.expireCount);
|
AtomicInteger expirationCount = new AtomicInteger(this.expireCount);
|
||||||
final boolean hasExpirationCount = expirationCount.get() > 0;
|
final boolean hasExpirationCount = expirationCount.get() > 0;
|
||||||
|
|
||||||
@ -123,6 +139,11 @@ public interface EventListener<T extends Event> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Result run(@NotNull T event) {
|
public @NotNull Result run(@NotNull T event) {
|
||||||
|
// Event cancellation
|
||||||
|
if (!ignoreCancelled && event instanceof CancellableEvent &&
|
||||||
|
((CancellableEvent) event).isCancelled()) {
|
||||||
|
return Result.INVALID;
|
||||||
|
}
|
||||||
// Expiration predicate
|
// Expiration predicate
|
||||||
if (expireWhen != null && expireWhen.test(event)) {
|
if (expireWhen != null && expireWhen.test(event)) {
|
||||||
return Result.EXPIRED;
|
return Result.EXPIRED;
|
||||||
|
Loading…
Reference in New Issue
Block a user