mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-04 23:47:59 +01:00
Style cleanup
This commit is contained in:
parent
61d19243f7
commit
a92ac203f7
@ -273,12 +273,11 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
@Override
|
@Override
|
||||||
public void call(@NotNull E event) {
|
public void call(@NotNull E event) {
|
||||||
final Consumer<E> listener = updatedListener();
|
final Consumer<E> listener = updatedListener();
|
||||||
if (listener != null) {
|
if (listener == null) return;
|
||||||
try {
|
try {
|
||||||
listener.accept(event);
|
listener.accept(event);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
MinecraftServer.getExceptionManager().handleException(e);
|
MinecraftServer.getExceptionManager().handleException(e);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -304,19 +303,19 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
forTargetEvents(eventType, type -> {
|
forTargetEvents(eventType, type -> {
|
||||||
final ListenerEntry<E> entry = node.listenerMap.get(type);
|
final ListenerEntry<E> entry = node.listenerMap.get(type);
|
||||||
if (entry != null) {
|
if (entry != null) {
|
||||||
final Consumer<E> result = appendEntries(entry, node);
|
final Consumer<E> result = listenersConsumer(entry);
|
||||||
if (result != null) listeners.add(result);
|
if (result != null) listeners.add(result);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
final Consumer<E>[] listenersArray = listeners.toArray(Consumer[]::new);
|
final Consumer<E>[] listenersArray = listeners.toArray(Consumer[]::new);
|
||||||
// Mapped
|
// Mapped
|
||||||
final Consumer<E> mappedListener = handleMappedNode(node);
|
final Consumer<E> mappedListener = mappedConsumer();
|
||||||
// Children
|
// Children
|
||||||
final Consumer<E>[] childrenListeners = node.children.stream()
|
final Consumer<E>[] childrenListeners = node.children.stream()
|
||||||
.filter(child -> child.eventType.isAssignableFrom(eventType)) // Invalid event type
|
.filter(child -> child.eventType.isAssignableFrom(eventType)) // Invalid event type
|
||||||
.sorted(Comparator.comparing(EventNode::getPriority))
|
.sorted(Comparator.comparing(EventNode::getPriority))
|
||||||
.map(child -> ((Handle<E>) child.getHandle(eventType)).updatedListener())
|
.map(child -> ((Handle<E>) child.getHandle(eventType)).updatedListener())
|
||||||
.filter(eConsumer -> eConsumer != null)
|
.filter(Objects::nonNull)
|
||||||
.toArray(Consumer[]::new);
|
.toArray(Consumer[]::new);
|
||||||
// Empty check
|
// Empty check
|
||||||
final BiPredicate<E, Object> predicate = node.predicate;
|
final BiPredicate<E, Object> predicate = node.predicate;
|
||||||
@ -353,12 +352,12 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add listeners from {@link EventNode#addListener(EventListener)} and
|
* Create a consumer calling all listeners from {@link EventNode#addListener(EventListener)} and
|
||||||
* {@link EventNode#register(EventBinding)} to the handle list.
|
* {@link EventNode#register(EventBinding)}.
|
||||||
* <p>
|
* <p>
|
||||||
* Most computation should ideally be done outside the consumers as a one-time cost.
|
* Most computation should ideally be done outside the consumers as a one-time cost.
|
||||||
*/
|
*/
|
||||||
private @Nullable Consumer<E> appendEntries(ListenerEntry<E> entry, EventNodeImpl<E> targetNode) {
|
private @Nullable Consumer<E> listenersConsumer(@NotNull ListenerEntry<E> entry) {
|
||||||
final EventListener<E>[] listenersCopy = entry.listeners.toArray(EventListener[]::new);
|
final EventListener<E>[] listenersCopy = entry.listeners.toArray(EventListener[]::new);
|
||||||
final Consumer<E>[] bindingsCopy = entry.bindingConsumers.toArray(Consumer[]::new);
|
final Consumer<E>[] bindingsCopy = entry.bindingConsumers.toArray(Consumer[]::new);
|
||||||
final boolean listenersEmpty = listenersCopy.length == 0;
|
final boolean listenersEmpty = listenersCopy.length == 0;
|
||||||
@ -367,13 +366,13 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
if (bindingsEmpty && listenersCopy.length == 1) {
|
if (bindingsEmpty && listenersCopy.length == 1) {
|
||||||
// Only one normal listener
|
// Only one normal listener
|
||||||
final EventListener<E> listener = listenersCopy[0];
|
final EventListener<E> listener = listenersCopy[0];
|
||||||
return e -> callListener(targetNode, listener, e);
|
return e -> callListener(listener, e);
|
||||||
}
|
}
|
||||||
// Worse case scenario, try to run everything
|
// Worse case scenario, try to run everything
|
||||||
return e -> {
|
return e -> {
|
||||||
if (!listenersEmpty) {
|
if (!listenersEmpty) {
|
||||||
for (EventListener<E> listener : listenersCopy) {
|
for (EventListener<E> listener : listenersCopy) {
|
||||||
callListener(targetNode, listener, e);
|
callListener(listener, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!bindingsEmpty) {
|
if (!bindingsEmpty) {
|
||||||
@ -388,8 +387,8 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
* Create a consumer handling {@link EventNode#map(EventNode, Object)}.
|
* Create a consumer handling {@link EventNode#map(EventNode, Object)}.
|
||||||
* The goal is to limit the amount of map lookup.
|
* The goal is to limit the amount of map lookup.
|
||||||
*/
|
*/
|
||||||
private @Nullable Consumer<E> handleMappedNode(EventNodeImpl<E> targetNode) {
|
private @Nullable Consumer<E> mappedConsumer() {
|
||||||
final var mappedNodeCache = targetNode.mappedNodeCache;
|
final var mappedNodeCache = node.mappedNodeCache;
|
||||||
if (mappedNodeCache.isEmpty()) return null;
|
if (mappedNodeCache.isEmpty()) return null;
|
||||||
Set<EventFilter<E, ?>> filters = new HashSet<>(mappedNodeCache.size());
|
Set<EventFilter<E, ?>> filters = new HashSet<>(mappedNodeCache.size());
|
||||||
Map<Object, Handle<E>> handlers = new HashMap<>(mappedNodeCache.size());
|
Map<Object, Handle<E>> handlers = new HashMap<>(mappedNodeCache.size());
|
||||||
@ -430,10 +429,10 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void callListener(EventNodeImpl<E> targetNode, EventListener<E> listener, E event) {
|
void callListener(@NotNull EventListener<E> listener, E event) {
|
||||||
EventListener.Result result = listener.run(event);
|
EventListener.Result result = listener.run(event);
|
||||||
if (result == EventListener.Result.EXPIRED) {
|
if (result == EventListener.Result.EXPIRED) {
|
||||||
targetNode.removeListener(listener);
|
node.removeListener(listener);
|
||||||
this.updated = false;
|
this.updated = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user