Can now call an event from anywhere in the tree

This commit is contained in:
TheMode 2021-06-02 10:34:01 +02:00
parent f4db18ea6c
commit 4f7192b6c7
3 changed files with 14 additions and 37 deletions

View File

@ -1,23 +1,17 @@
package net.minestom.server.event; package net.minestom.server.event;
import net.minestom.server.event.handler.EventHandler;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
public class EventListener<T extends Event> implements ListenerAttach { public class EventListener<T extends Event> {
private final Class<T> eventType;
private final Set<EventHandler> attach = new CopyOnWriteArraySet<>();
private final Consumer<T> combined; private final Consumer<T> combined;
private EventListener(Class<T> eventType, @NotNull Consumer<T> combined) { private EventListener(@NotNull Consumer<T> combined) {
this.eventType = eventType;
this.combined = combined; this.combined = combined;
} }
@ -25,20 +19,8 @@ public class EventListener<T extends Event> implements ListenerAttach {
return new EventListener.Builder<>(eventType); return new EventListener.Builder<>(eventType);
} }
@Override protected Consumer<T> getCombined() {
public void attachTo(@NotNull EventHandler handler) { return combined;
final boolean success = this.attach.add(handler);
if (success) {
handler.addEventCallback(eventType, combined::accept);
}
}
@Override
public void detachFrom(@NotNull EventHandler handler) {
final boolean success = this.attach.remove(handler);
if (success) {
handler.removeEventCallback(eventType, combined::accept);
}
} }
public static class Builder<T extends Event> { public static class Builder<T extends Event> {
@ -63,7 +45,7 @@ public class EventListener<T extends Event> implements ListenerAttach {
} }
public EventListener<T> build() { public EventListener<T> build() {
return new EventListener<>(eventType, event -> { return new EventListener<>(event -> {
// Filtering // Filtering
if (!filters.isEmpty()) { if (!filters.isEmpty()) {
if (filters.stream().anyMatch(filter -> !filter.test(event))) { if (filters.stream().anyMatch(filter -> !filter.test(event))) {

View File

@ -12,8 +12,8 @@ import java.util.function.Predicate;
public class EventNode<T extends Event> { public class EventNode<T extends Event> {
private final String name = "debug"; private final String name = "debug";
private final List<EventListener<? extends T>> listeners = new CopyOnWriteArrayList<>(); private final List<EventListener<T>> listeners = new CopyOnWriteArrayList<>();
private final List<EventNode<? extends T>> children = new CopyOnWriteArrayList<>(); private final List<EventNode<T>> children = new CopyOnWriteArrayList<>();
private final Predicate<T> condition = t -> true; private final Predicate<T> condition = t -> true;
private EventNode() { private EventNode() {
@ -37,12 +37,17 @@ public class EventNode<T extends Event> {
return new EventNode<>(); return new EventNode<>();
} }
public void call(@NotNull T event) {
this.listeners.forEach(eventListener -> eventListener.getCombined().accept(event));
this.children.forEach(eventNode -> eventNode.call(event));
}
public void addChild(@NotNull EventNode<? extends T> child) { public void addChild(@NotNull EventNode<? extends T> child) {
this.children.add(child); this.children.add((EventNode<T>) child);
} }
public void addListener(@NotNull EventListener<? extends T> listener) { public void addListener(@NotNull EventListener<? extends T> listener) {
this.listeners.add(listener); this.listeners.add((EventListener<T>) listener);
} }
public <E extends T> void addListener(@NotNull Class<E> eventClass, @NotNull Consumer<@NotNull E> listener) { public <E extends T> void addListener(@NotNull Class<E> eventClass, @NotNull Consumer<@NotNull E> listener) {

View File

@ -1,10 +0,0 @@
package net.minestom.server.event;
import net.minestom.server.event.handler.EventHandler;
import org.jetbrains.annotations.NotNull;
public interface ListenerAttach {
void attachTo(@NotNull EventHandler handler);
void detachFrom(@NotNull EventHandler handler);
}