EventNode methods return self

This commit is contained in:
TheMode 2021-06-03 04:41:08 +02:00
parent d5cce11562
commit 8ec33bb6c8
2 changed files with 27 additions and 22 deletions

View File

@ -108,7 +108,7 @@ public class EventNode<T extends Event> {
this.children.forEach(eventNode -> eventNode.call(event));
}
public void addChild(@NotNull EventNode<? extends T> child) {
public EventNode<T> addChild(@NotNull EventNode<? extends T> child) {
synchronized (GLOBAL_CHILD_LOCK) {
final boolean result = this.children.add((EventNode<T>) child);
if (result) {
@ -122,9 +122,10 @@ public class EventNode<T extends Event> {
}
}
}
return this;
}
public void removeChild(@NotNull EventNode<? extends T> child) {
public EventNode<T> removeChild(@NotNull EventNode<? extends T> child) {
synchronized (GLOBAL_CHILD_LOCK) {
final boolean result = this.children.remove(child);
if (result) {
@ -138,9 +139,10 @@ public class EventNode<T extends Event> {
}
}
}
return this;
}
public void addListener(@NotNull EventListener<? extends T> listener) {
public EventNode<T> addListener(@NotNull EventListener<? extends T> listener) {
synchronized (GLOBAL_CHILD_LOCK) {
final var eventType = listener.getEventType();
this.listenerMap.computeIfAbsent(eventType, aClass -> new CopyOnWriteArrayList<>())
@ -151,14 +153,15 @@ public class EventNode<T extends Event> {
}
}
}
return this;
}
public void removeListener(@NotNull EventListener<? extends T> listener) {
public EventNode<T> removeListener(@NotNull EventListener<? extends T> listener) {
synchronized (GLOBAL_CHILD_LOCK) {
final var eventType = listener.getEventType();
var listeners = listenerMap.get(eventType);
if (listeners == null || listeners.isEmpty())
return;
return this;
final boolean removed = listeners.remove(listener);
if (removed && parent != null) {
synchronized (parent.lock) {
@ -166,25 +169,29 @@ public class EventNode<T extends Event> {
}
}
}
return this;
}
public <E extends T, V2> void map(@NotNull EventFilter<E, V2> filter, @NotNull V2 value, @NotNull EventNode<E> node) {
public <E extends T, V2> EventNode<T> map(@NotNull EventFilter<E, V2> filter, @NotNull V2 value, @NotNull EventNode<E> node) {
RedirectionEntry<E> entry = new RedirectionEntry<>();
entry.filter = filter;
entry.node = node;
this.redirectionMap.put(value, (RedirectionEntry<T>) entry);
return this;
}
public void removeMap(@NotNull Object value) {
public EventNode<T> removeMap(@NotNull Object value) {
this.redirectionMap.remove(value);
return this;
}
public @NotNull String getName() {
return name;
}
public void setName(@NotNull String name) {
public EventNode<T> setName(@NotNull String name) {
this.name = name;
return this;
}
public @NotNull Set<@NotNull EventNode<T>> getChildren() {

View File

@ -137,22 +137,20 @@ public class PlayerInit {
// EVENT REGISTERING
var empty = EventNode.all();
empty.setName("empty");
empty.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> {
}));
var empty = EventNode.all()
.setName("empty")
.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> {
}));
var node = EventNode.type(EventFilter.PLAYER);
node.setName("node");
node.addListener(EventListener.builder(PlayerTickEvent.class)
.handler(playerTickEvent -> System.out.println("Player tick!"))
.expirationCount(50)
.build());
var node = EventNode.type(EventFilter.PLAYER)
.setName("node")
.addListener(EventListener.builder(PlayerTickEvent.class)
.handler(playerTickEvent -> System.out.println("Player tick!"))
.expirationCount(50)
.build());
var conditional = EventNode.predicateValue(EventFilter.PLAYER, Player::isCreative);
conditional.addListener(EventListener.of(PlayerMoveEvent.class, (event) ->
System.out.println("creative player moved")));
var conditional = EventNode.predicateValue(EventFilter.PLAYER, Player::isCreative)
.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> System.out.println("creative player moved")));
GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler();
globalEventHandler.addEventCallback(EntityAttackEvent.class, event -> {