Add shortcut to add listener

This commit is contained in:
TheMode 2021-06-03 20:46:11 +02:00
parent e5031ff37e
commit 81e2072bc5
2 changed files with 13 additions and 5 deletions

View File

@ -10,6 +10,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class EventNode<T extends Event> {
@ -171,6 +172,10 @@ public class EventNode<T extends Event> {
return this;
}
public <E extends T> EventNode<T> addListener(@NotNull Class<E> eventType, @NotNull Consumer<@NotNull E> listener) {
return addListener(EventListener.of(eventType, listener));
}
public <E extends T> EventNode<T> map(@NotNull Object value, @NotNull EventNode<E> node) {
this.redirectionMap.put(value, (EventNode<T>) node);
return this;

View File

@ -139,8 +139,8 @@ public class PlayerInit {
var empty = EventNode.all()
.setName("empty")
.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> {
}));
.addListener(PlayerMoveEvent.class, (event) -> {
});
var node = EventNode.type(EventFilter.PLAYER)
.setName("node")
@ -150,7 +150,10 @@ public class PlayerInit {
.build());
var conditional = EventNode.predicateValue(EventFilter.PLAYER, Player::isCreative)
.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> System.out.println("creative player moved")));
.addListener(PlayerMoveEvent.class, (event) -> System.out.println("creative player moved"));
node.addChild(conditional);
node.call(new PlayerTickEvent(null));
GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler();
globalEventHandler.addEventCallback(EntityAttackEvent.class, event -> {
@ -172,8 +175,8 @@ public class PlayerInit {
((Player) source).sendMessage("You attacked something!");
}
empty.map(source, EventNode.type(EventFilter.ENTITY)
.addListener(EventListener.of(PlayerMoveEvent.class, (e) ->
System.out.println("Test movement"))));
.addListener(PlayerMoveEvent.class, (e) ->
System.out.println("Test movement")));
});
globalEventHandler.addEventCallback(PlayerDeathEvent.class, event -> {