mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
Can now call an event from anywhere in the tree
This commit is contained in:
parent
f4db18ea6c
commit
4f7192b6c7
@ -1,23 +1,17 @@
|
||||
package net.minestom.server.event;
|
||||
|
||||
import net.minestom.server.event.handler.EventHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.function.Consumer;
|
||||
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 EventListener(Class<T> eventType, @NotNull Consumer<T> combined) {
|
||||
this.eventType = eventType;
|
||||
private EventListener(@NotNull Consumer<T> combined) {
|
||||
this.combined = combined;
|
||||
}
|
||||
|
||||
@ -25,20 +19,8 @@ public class EventListener<T extends Event> implements ListenerAttach {
|
||||
return new EventListener.Builder<>(eventType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attachTo(@NotNull EventHandler handler) {
|
||||
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);
|
||||
}
|
||||
protected Consumer<T> getCombined() {
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static class Builder<T extends Event> {
|
||||
@ -63,7 +45,7 @@ public class EventListener<T extends Event> implements ListenerAttach {
|
||||
}
|
||||
|
||||
public EventListener<T> build() {
|
||||
return new EventListener<>(eventType, event -> {
|
||||
return new EventListener<>(event -> {
|
||||
// Filtering
|
||||
if (!filters.isEmpty()) {
|
||||
if (filters.stream().anyMatch(filter -> !filter.test(event))) {
|
||||
|
@ -12,8 +12,8 @@ import java.util.function.Predicate;
|
||||
public class EventNode<T extends Event> {
|
||||
|
||||
private final String name = "debug";
|
||||
private final List<EventListener<? extends T>> listeners = new CopyOnWriteArrayList<>();
|
||||
private final List<EventNode<? extends T>> children = new CopyOnWriteArrayList<>();
|
||||
private final List<EventListener<T>> listeners = new CopyOnWriteArrayList<>();
|
||||
private final List<EventNode<T>> children = new CopyOnWriteArrayList<>();
|
||||
private final Predicate<T> condition = t -> true;
|
||||
|
||||
private EventNode() {
|
||||
@ -37,12 +37,17 @@ public class EventNode<T extends Event> {
|
||||
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) {
|
||||
this.children.add(child);
|
||||
this.children.add((EventNode<T>) child);
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user