mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
Remove second generic from EventNode
This commit is contained in:
parent
5fa750fde3
commit
d5cce11562
@ -14,46 +14,46 @@ import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class EventNode<T extends Event, V> {
|
||||
public class EventNode<T extends Event> {
|
||||
|
||||
public static <E extends Event, V> EventNode<E, V> type(@NotNull EventFilter<E, V> filter) {
|
||||
public static <E extends Event, V> EventNode<E> type(@NotNull EventFilter<E, V> filter) {
|
||||
return new EventNode<>(filter, (e, v) -> true);
|
||||
}
|
||||
|
||||
public static EventNode<Event, ?> all() {
|
||||
public static EventNode<Event> all() {
|
||||
return type(EventFilter.ALL);
|
||||
}
|
||||
|
||||
public static <E extends Event, V> EventNode<E, V> predicate(@NotNull EventFilter<E, V> filter,
|
||||
@NotNull BiPredicate<E, V> predicate) {
|
||||
return new EventNode<>(filter, predicate);
|
||||
public static <E extends Event, V> EventNode<E> predicate(@NotNull EventFilter<E, V> filter,
|
||||
@NotNull BiPredicate<E, V> predicate) {
|
||||
return new EventNode<>(filter, (e, o) -> predicate.test(e, (V) o));
|
||||
}
|
||||
|
||||
public static <E extends Event, V> EventNode<E, V> predicateEvent(@NotNull EventFilter<E, V> filter,
|
||||
@NotNull Predicate<E> predicate) {
|
||||
public static <E extends Event, V> EventNode<E> predicateEvent(@NotNull EventFilter<E, V> filter,
|
||||
@NotNull Predicate<E> predicate) {
|
||||
return predicate(filter, (e, h) -> predicate.test(e));
|
||||
}
|
||||
|
||||
public static <E extends Event, V> EventNode<E, V> predicateValue(@NotNull EventFilter<E, V> filter,
|
||||
@NotNull Predicate<V> predicate) {
|
||||
public static <E extends Event, V> EventNode<E> predicateValue(@NotNull EventFilter<E, V> filter,
|
||||
@NotNull Predicate<V> predicate) {
|
||||
return predicate(filter, (e, h) -> predicate.test(h));
|
||||
}
|
||||
|
||||
private final Map<Class<? extends T>, List<EventListener<T>>> listenerMap = new ConcurrentHashMap<>();
|
||||
private final Map<Object, RedirectionEntry<T>> redirectionMap = new ConcurrentHashMap<>();
|
||||
private final Set<EventNode<T, ?>> children = new CopyOnWriteArraySet<>();
|
||||
private final Set<EventNode<T>> children = new CopyOnWriteArraySet<>();
|
||||
|
||||
protected final EventFilter<T, V> filter;
|
||||
protected volatile BiPredicate<T, V> predicate;
|
||||
protected final EventFilter<T, ?> filter;
|
||||
protected final BiPredicate<T, Object> predicate;
|
||||
private volatile String name = "unknown";
|
||||
|
||||
// Tree data
|
||||
private static final Object GLOBAL_CHILD_LOCK = new Object();
|
||||
private volatile EventNode<? super T, ?> parent;
|
||||
private volatile EventNode<? super T> parent;
|
||||
private final Object lock = new Object();
|
||||
private final Object2IntMap<Class<? extends T>> childEventMap = new Object2IntOpenHashMap<>();
|
||||
|
||||
protected EventNode(EventFilter<T, V> filter, BiPredicate<T, V> predicate) {
|
||||
protected EventNode(EventFilter<T, ?> filter, BiPredicate<T, Object> predicate) {
|
||||
this.filter = filter;
|
||||
this.predicate = predicate;
|
||||
}
|
||||
@ -65,7 +65,8 @@ public class EventNode<T extends Event, V> {
|
||||
* @return true to enter the node, false otherwise
|
||||
*/
|
||||
protected boolean condition(@NotNull T event) {
|
||||
return predicate.test(event, filter.getHandler(event));
|
||||
final var value = filter.getHandler(event);
|
||||
return predicate.test(event, value);
|
||||
}
|
||||
|
||||
public void call(@NotNull T event) {
|
||||
@ -107,9 +108,9 @@ public class EventNode<T extends Event, V> {
|
||||
this.children.forEach(eventNode -> eventNode.call(event));
|
||||
}
|
||||
|
||||
public void addChild(@NotNull EventNode<? extends T, ?> child) {
|
||||
public void addChild(@NotNull EventNode<? extends T> child) {
|
||||
synchronized (GLOBAL_CHILD_LOCK) {
|
||||
final boolean result = this.children.add((EventNode<T, ?>) child);
|
||||
final boolean result = this.children.add((EventNode<T>) child);
|
||||
if (result) {
|
||||
child.parent = this;
|
||||
// Increase listener count
|
||||
@ -123,7 +124,7 @@ public class EventNode<T extends Event, V> {
|
||||
}
|
||||
}
|
||||
|
||||
public void removeChild(@NotNull EventNode<? extends T, ?> child) {
|
||||
public void removeChild(@NotNull EventNode<? extends T> child) {
|
||||
synchronized (GLOBAL_CHILD_LOCK) {
|
||||
final boolean result = this.children.remove(child);
|
||||
if (result) {
|
||||
@ -167,15 +168,7 @@ public class EventNode<T extends Event, V> {
|
||||
}
|
||||
}
|
||||
|
||||
public @NotNull BiPredicate<T, V> getPredicate() {
|
||||
return predicate;
|
||||
}
|
||||
|
||||
public void setPredicate(@NotNull BiPredicate<T, V> predicate) {
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
public <E extends T, V2> void map(@NotNull EventFilter<E, V2> filter, @NotNull V2 value, @NotNull EventNode<E, V2> node) {
|
||||
public <E extends T, V2> void map(@NotNull EventFilter<E, V2> filter, @NotNull V2 value, @NotNull EventNode<E> node) {
|
||||
RedirectionEntry<E> entry = new RedirectionEntry<>();
|
||||
entry.filter = filter;
|
||||
entry.node = node;
|
||||
@ -194,7 +187,7 @@ public class EventNode<T extends Event, V> {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public @NotNull Set<@NotNull EventNode<T, ?>> getChildren() {
|
||||
public @NotNull Set<@NotNull EventNode<T>> getChildren() {
|
||||
return Collections.unmodifiableSet(children);
|
||||
}
|
||||
|
||||
@ -218,6 +211,6 @@ public class EventNode<T extends Event, V> {
|
||||
|
||||
private static class RedirectionEntry<E extends Event> {
|
||||
EventFilter<E, ?> filter;
|
||||
EventNode<E, ?> node;
|
||||
EventNode<E> node;
|
||||
}
|
||||
}
|
||||
|
@ -149,13 +149,6 @@ public class PlayerInit {
|
||||
.expirationCount(50)
|
||||
.build());
|
||||
|
||||
empty.addChild(node);
|
||||
node.call(new PlayerTickEvent(null));
|
||||
empty.call(new PlayerTickEvent(null));
|
||||
empty.removeChild(node);
|
||||
node.call(new PlayerTickEvent(null));
|
||||
empty.call(new PlayerTickEvent(null));
|
||||
|
||||
|
||||
var conditional = EventNode.predicateValue(EventFilter.PLAYER, Player::isCreative);
|
||||
conditional.addListener(EventListener.of(PlayerMoveEvent.class, (event) ->
|
||||
|
Loading…
Reference in New Issue
Block a user