mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
More cleanup
This commit is contained in:
parent
390c383e82
commit
523c9b512a
@ -311,7 +311,9 @@ public interface EventNode<T extends Event> {
|
|||||||
@NotNull EventNode<T> addListener(@NotNull EventListener<? extends T> listener);
|
@NotNull EventNode<T> addListener(@NotNull EventListener<? extends T> listener);
|
||||||
|
|
||||||
@Contract(value = "_, _ -> this")
|
@Contract(value = "_, _ -> this")
|
||||||
<E extends T> @NotNull EventNode<T> addListener(@NotNull Class<E> eventType, @NotNull Consumer<@NotNull E> listener);
|
default <E extends T> @NotNull EventNode<T> addListener(@NotNull Class<E> eventType, @NotNull Consumer<@NotNull E> listener) {
|
||||||
|
return addListener(EventListener.of(eventType, listener));
|
||||||
|
}
|
||||||
|
|
||||||
@Contract(value = "_ -> this")
|
@Contract(value = "_ -> this")
|
||||||
@NotNull EventNode<T> removeListener(@NotNull EventListener<? extends T> listener);
|
@NotNull EventNode<T> removeListener(@NotNull EventListener<? extends T> listener);
|
||||||
|
@ -88,8 +88,8 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <E extends T> void replaceChildren(@NotNull String name, @NotNull Class<E> eventType, @NotNull EventNode<E> eventNode) {
|
public <E extends T> void replaceChildren(@NotNull String name, @NotNull Class<E> eventType, @NotNull EventNode<E> eventNode) {
|
||||||
if (children.isEmpty()) return;
|
|
||||||
synchronized (GLOBAL_CHILD_LOCK) {
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
|
if (children.isEmpty()) return;
|
||||||
for (EventNode<T> child : children) {
|
for (EventNode<T> child : children) {
|
||||||
if (equals(child, name, eventType)) {
|
if (equals(child, name, eventType)) {
|
||||||
removeChild(child);
|
removeChild(child);
|
||||||
@ -103,8 +103,8 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeChildren(@NotNull String name, @NotNull Class<? extends T> eventType) {
|
public void removeChildren(@NotNull String name, @NotNull Class<? extends T> eventType) {
|
||||||
if (children.isEmpty()) return;
|
|
||||||
synchronized (GLOBAL_CHILD_LOCK) {
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
|
if (children.isEmpty()) return;
|
||||||
for (EventNode<T> child : children) {
|
for (EventNode<T> child : children) {
|
||||||
if (equals(child, name, eventType)) {
|
if (equals(child, name, eventType)) {
|
||||||
removeChild(child);
|
removeChild(child);
|
||||||
@ -166,11 +166,6 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public <E extends T> @NotNull EventNode<T> addListener(@NotNull Class<E> eventType, @NotNull Consumer<@NotNull E> listener) {
|
|
||||||
return addListener(EventListener.of(eventType, listener));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull EventNode<T> removeListener(@NotNull EventListener<? extends T> listener) {
|
public @NotNull EventNode<T> removeListener(@NotNull EventListener<? extends T> listener) {
|
||||||
synchronized (GLOBAL_CHILD_LOCK) {
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
@ -191,7 +186,6 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
@Override
|
@Override
|
||||||
public void map(@NotNull EventNode<? extends T> node, @NotNull Object value) {
|
public void map(@NotNull EventNode<? extends T> node, @NotNull Object value) {
|
||||||
final var nodeImpl = (EventNodeImpl<? extends T>) node;
|
final var nodeImpl = (EventNodeImpl<? extends T>) node;
|
||||||
final var nodeType = nodeImpl.eventType;
|
|
||||||
final var valueType = value.getClass();
|
final var valueType = value.getClass();
|
||||||
synchronized (GLOBAL_CHILD_LOCK) {
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
nodeImpl.listenerMap.forEach((type, listenerEntry) -> {
|
nodeImpl.listenerMap.forEach((type, listenerEntry) -> {
|
||||||
@ -200,7 +194,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
final var handlerType = eventFilter.handlerType();
|
final var handlerType = eventFilter.handlerType();
|
||||||
return handlerType != null && handlerType.isAssignableFrom(valueType);
|
return handlerType != null && handlerType.isAssignableFrom(valueType);
|
||||||
});
|
});
|
||||||
Check.stateCondition(!correct, "The node filter {0} is not compatible with type {1}", nodeType, valueType);
|
Check.stateCondition(!correct, "The node filter {0} is not compatible with type {1}", nodeImpl.eventType, valueType);
|
||||||
synchronized (mappedNodeCache) {
|
synchronized (mappedNodeCache) {
|
||||||
entry.mappedNode.put(value, (EventNode<T>) nodeImpl);
|
entry.mappedNode.put(value, (EventNode<T>) nodeImpl);
|
||||||
mappedNodeCache.put(value, entry);
|
mappedNodeCache.put(value, entry);
|
||||||
@ -225,8 +219,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
synchronized (GLOBAL_CHILD_LOCK) {
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
for (var eventType : binding.eventTypes()) {
|
for (var eventType : binding.eventTypes()) {
|
||||||
var entry = getEntry((Class<? extends T>) eventType);
|
var entry = getEntry((Class<? extends T>) eventType);
|
||||||
final var consumer = binding.consumer(eventType);
|
entry.bindingConsumers.add((Consumer<T>) binding.consumer(eventType));
|
||||||
entry.bindingConsumers.add((Consumer<T>) consumer);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -294,7 +287,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ListenerEntry<T> getEntry(Class<? extends T> type) {
|
private ListenerEntry<T> getEntry(Class<? extends T> type) {
|
||||||
return listenerMap.computeIfAbsent(type, aClass -> new ListenerEntry<>((Class<T>) aClass));
|
return listenerMap.computeIfAbsent(type, aClass -> new ListenerEntry<>(this, (Class<T>) aClass));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean equals(EventNode<?> node, String name, Class<?> eventType) {
|
private static boolean equals(EventNode<?> node, String name, Class<?> eventType) {
|
||||||
@ -312,13 +305,15 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
private static final AtomicIntegerFieldUpdater<ListenerEntry> CHILD_UPDATER =
|
private static final AtomicIntegerFieldUpdater<ListenerEntry> CHILD_UPDATER =
|
||||||
AtomicIntegerFieldUpdater.newUpdater(ListenerEntry.class, "childCount");
|
AtomicIntegerFieldUpdater.newUpdater(ListenerEntry.class, "childCount");
|
||||||
|
|
||||||
|
final EventNodeImpl<T> node;
|
||||||
final List<EventFilter<?, ?>> filters;
|
final List<EventFilter<?, ?>> filters;
|
||||||
final List<EventListener<T>> listeners = new CopyOnWriteArrayList<>();
|
final List<EventListener<T>> listeners = new CopyOnWriteArrayList<>();
|
||||||
final Set<Consumer<T>> bindingConsumers = new CopyOnWriteArraySet<>();
|
final Set<Consumer<T>> bindingConsumers = new CopyOnWriteArraySet<>();
|
||||||
final Map<Object, EventNode<T>> mappedNode = new WeakHashMap<>();
|
final Map<Object, EventNode<T>> mappedNode = new WeakHashMap<>();
|
||||||
volatile int childCount;
|
volatile int childCount;
|
||||||
|
|
||||||
ListenerEntry(Class<T> eventType) {
|
ListenerEntry(EventNodeImpl<T> node, Class<T> eventType) {
|
||||||
|
this.node = node;
|
||||||
this.filters = FILTERS.stream().filter(eventFilter -> eventFilter.eventType().isAssignableFrom(eventType)).collect(Collectors.toList());
|
this.filters = FILTERS.stream().filter(eventFilter -> eventFilter.eventType().isAssignableFrom(eventType)).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -330,6 +325,8 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Mapped listeners
|
// Mapped listeners
|
||||||
|
if (!mappedNode.isEmpty()) {
|
||||||
|
synchronized (node.mappedNodeCache) {
|
||||||
if (!mappedNode.isEmpty()) {
|
if (!mappedNode.isEmpty()) {
|
||||||
// Check mapped listeners for each individual event handler
|
// Check mapped listeners for each individual event handler
|
||||||
for (var filter : filters) {
|
for (var filter : filters) {
|
||||||
@ -338,6 +335,8 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
if (map != null) map.call(event);
|
if (map != null) map.call(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// Basic listeners
|
// Basic listeners
|
||||||
if (!listeners.isEmpty()) {
|
if (!listeners.isEmpty()) {
|
||||||
for (EventListener<T> listener : listeners) {
|
for (EventListener<T> listener : listeners) {
|
||||||
|
Loading…
Reference in New Issue
Block a user