mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
Separate conditional event from conditional handler
This commit is contained in:
parent
c95c8430b6
commit
4f127b2e0e
@ -4,6 +4,7 @@ import net.minestom.server.event.handler.EventHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public interface EventNode<T extends Event> {
|
||||
@ -17,10 +18,20 @@ public interface EventNode<T extends Event> {
|
||||
}
|
||||
|
||||
static <E extends Event, H extends EventHandler> EventNodeConditional<E, H> conditional(@NotNull EventFilter<E, H> filter,
|
||||
@NotNull Predicate<E> predicate) {
|
||||
@NotNull BiPredicate<E, H> predicate) {
|
||||
return new EventNodeConditional<>(filter, predicate);
|
||||
}
|
||||
|
||||
static <E extends Event, H extends EventHandler> EventNodeConditional<E, H> conditionalEvent(@NotNull EventFilter<E, H> filter,
|
||||
@NotNull Predicate<E> predicate) {
|
||||
return conditional(filter, (e, h) -> predicate.test(e));
|
||||
}
|
||||
|
||||
static <E extends Event, H extends EventHandler> EventNodeConditional<E, H> conditionalHandler(@NotNull EventFilter<E, H> filter,
|
||||
@NotNull Predicate<H> predicate) {
|
||||
return conditional(filter, (e, h) -> predicate.test(h));
|
||||
}
|
||||
|
||||
static <E extends Event, H extends EventHandler> EventNodeList<E, H> list(@NotNull EventFilter<E, H> filter) {
|
||||
return new EventNodeList<>(filter);
|
||||
}
|
||||
|
@ -3,27 +3,27 @@ package net.minestom.server.event;
|
||||
import net.minestom.server.event.handler.EventHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.BiPredicate;
|
||||
|
||||
public class EventNodeConditional<T extends Event, H extends EventHandler> extends EventNodeImpl<T, H> {
|
||||
|
||||
private volatile Predicate<T> predicate;
|
||||
private volatile BiPredicate<T, H> predicate;
|
||||
|
||||
protected EventNodeConditional(EventFilter<T, H> filter, Predicate<T> predicate) {
|
||||
protected EventNodeConditional(EventFilter<T, H> filter, BiPredicate<T, H> predicate) {
|
||||
super(filter);
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean condition(@NotNull T event) {
|
||||
return predicate.test(event);
|
||||
return predicate.test(event, filter.getHandler(event));
|
||||
}
|
||||
|
||||
public @NotNull Predicate<@NotNull T> getPredicate() {
|
||||
public @NotNull BiPredicate<T, H> getPredicate() {
|
||||
return predicate;
|
||||
}
|
||||
|
||||
public void setPredicate(@NotNull Predicate<@NotNull T> predicate) {
|
||||
public void setPredicate(@NotNull BiPredicate<T, H> predicate) {
|
||||
this.predicate = predicate;
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,12 @@ class EventNodeImpl<T extends Event, H extends EventHandler> implements EventNod
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Condition to enter the node.
|
||||
*
|
||||
* @param event the called event
|
||||
* @return true to enter the node, false otherwise
|
||||
*/
|
||||
protected boolean condition(@NotNull T event) {
|
||||
return true;
|
||||
}
|
||||
|
@ -147,11 +147,14 @@ public class PlayerInit {
|
||||
empty.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> {
|
||||
}));
|
||||
|
||||
var list = EventNode.list(EventFilter.PLAYER);
|
||||
list.addListener(EventListener.of(PlayerMoveEvent.class, playerMoveEvent -> {
|
||||
System.out.println("move");
|
||||
var conditional = EventNode.conditionalHandler(EventFilter.PLAYER, Player::isCreative);
|
||||
conditional.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> {
|
||||
System.out.println("creative player moved");
|
||||
}));
|
||||
|
||||
var list = EventNode.list(EventFilter.PLAYER);
|
||||
list.addListener(EventListener.of(PlayerMoveEvent.class, playerMoveEvent -> System.out.println("move")));
|
||||
|
||||
GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler();
|
||||
globalEventHandler.addEventCallback(EntityAttackEvent.class, event -> {
|
||||
final Entity source = event.getEntity();
|
||||
|
Loading…
Reference in New Issue
Block a user