mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-08 01:17:47 +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 org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.BiPredicate;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public interface EventNode<T extends Event> {
|
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,
|
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);
|
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) {
|
static <E extends Event, H extends EventHandler> EventNodeList<E, H> list(@NotNull EventFilter<E, H> filter) {
|
||||||
return new EventNodeList<>(filter);
|
return new EventNodeList<>(filter);
|
||||||
}
|
}
|
||||||
|
@ -3,27 +3,27 @@ package net.minestom.server.event;
|
|||||||
import net.minestom.server.event.handler.EventHandler;
|
import net.minestom.server.event.handler.EventHandler;
|
||||||
import org.jetbrains.annotations.NotNull;
|
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> {
|
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);
|
super(filter);
|
||||||
this.predicate = predicate;
|
this.predicate = predicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean condition(@NotNull T event) {
|
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;
|
return predicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPredicate(@NotNull Predicate<@NotNull T> predicate) {
|
public void setPredicate(@NotNull BiPredicate<T, H> predicate) {
|
||||||
this.predicate = predicate;
|
this.predicate = predicate;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,12 @@ class EventNodeImpl<T extends Event, H extends EventHandler> implements EventNod
|
|||||||
this.filter = filter;
|
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) {
|
protected boolean condition(@NotNull T event) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -147,11 +147,14 @@ public class PlayerInit {
|
|||||||
empty.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> {
|
empty.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
var list = EventNode.list(EventFilter.PLAYER);
|
var conditional = EventNode.conditionalHandler(EventFilter.PLAYER, Player::isCreative);
|
||||||
list.addListener(EventListener.of(PlayerMoveEvent.class, playerMoveEvent -> {
|
conditional.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> {
|
||||||
System.out.println("move");
|
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 globalEventHandler = MinecraftServer.getGlobalEventHandler();
|
||||||
globalEventHandler.addEventCallback(EntityAttackEvent.class, event -> {
|
globalEventHandler.addEventCallback(EntityAttackEvent.class, event -> {
|
||||||
final Entity source = event.getEntity();
|
final Entity source = event.getEntity();
|
||||||
|
Loading…
Reference in New Issue
Block a user