From d5cdb36e477152ee5c6974d7a82d6ba219eccc2d Mon Sep 17 00:00:00 2001 From: TheMode Date: Tue, 17 Aug 2021 01:35:16 +0200 Subject: [PATCH] Remove unnecessary list check --- .../minestom/server/event/EventInterface.java | 1 - .../minestom/server/event/EventNodeImpl.java | 79 ++++++++----------- 2 files changed, 35 insertions(+), 45 deletions(-) diff --git a/src/main/java/net/minestom/server/event/EventInterface.java b/src/main/java/net/minestom/server/event/EventInterface.java index 6c3f1854d..529537157 100644 --- a/src/main/java/net/minestom/server/event/EventInterface.java +++ b/src/main/java/net/minestom/server/event/EventInterface.java @@ -48,7 +48,6 @@ public interface EventInterface { final T handler = filter.getHandler(event); if (!predicate.test(handler)) return; final var consumer = copy.get(event.getClass()); - if (consumer == null) return; consumer.accept(handler, event); } }; diff --git a/src/main/java/net/minestom/server/event/EventNodeImpl.java b/src/main/java/net/minestom/server/event/EventNodeImpl.java index b4a48d139..e45a50cc3 100644 --- a/src/main/java/net/minestom/server/event/EventNodeImpl.java +++ b/src/main/java/net/minestom/server/event/EventNodeImpl.java @@ -57,46 +57,7 @@ class EventNodeImpl implements EventNode { // Process listeners list final var entry = listenerMap.get(eventClass); if (entry == null) return; // No listener nor children - { - // Event interfaces - final var interfaces = entry.interfaces; - if (!interfaces.isEmpty()) { - for (EventInterface inter : interfaces) { - if (!inter.eventTypes().contains(eventClass)) continue; - inter.call(event); - } - } - // Mapped listeners - final var mapped = entry.mappedNode; - if (!mapped.isEmpty()) { - synchronized (mappedNodeCache) { - if (!mapped.isEmpty()) { - // Check mapped listeners for each individual event handler - for (var filter : entry.filters) { - final var handler = filter.castHandler(event); - final var map = mapped.get(handler); - if (map != null) map.call(event); - } - } - } - } - // Basic listeners - final var listeners = entry.listeners; - if (!listeners.isEmpty()) { - for (EventListener listener : listeners) { - EventListener.Result result; - try { - result = listener.run(event); - } catch (Exception e) { - result = EventListener.Result.EXCEPTION; - MinecraftServer.getExceptionManager().handleException(e); - } - if (result == EventListener.Result.EXPIRED) { - listeners.remove(listener); - } - } - } - } + entry.call(event); // Process children if (entry.childCount > 0) { this.children.stream() @@ -241,7 +202,8 @@ class EventNodeImpl implements EventNode { }); Check.stateCondition(!correct, "The node filter {0} is not compatible with type {1}", nodeType, valueType); synchronized (mappedNodeCache) { - entry.mappedNode.put(value, (EventNode) node); + System.out.println("add " + value + " " + nodeImpl + " " + type); + entry.mappedNode.put(value, (EventNode) nodeImpl); mappedNodeCache.put(value, entry); } }); @@ -297,7 +259,7 @@ class EventNodeImpl implements EventNode { private void propagateChildCountChange(Class eventClass, int count) { var entry = getEntry(eventClass); - final int result = ListenerEntry.addAndGet(entry, count); + final int result = ListenerEntry.CHILD_UPDATER.addAndGet(entry, count); if (result == 0 && entry.listeners.isEmpty()) { this.listenerMap.remove(eventClass); } else if (result < 0) { @@ -349,8 +311,37 @@ class EventNodeImpl implements EventNode { this.filters = FILTERS.stream().filter(eventFilter -> eventFilter.eventType().isAssignableFrom(eventType)).collect(Collectors.toList()); } - private static int addAndGet(ListenerEntry entry, int add) { - return CHILD_UPDATER.addAndGet(entry, add); + void call(T event) { + // Event interfaces + if (!interfaces.isEmpty()) { + for (EventInterface inter : interfaces) { + inter.call(event); + } + } + // Mapped listeners + if (!mappedNode.isEmpty()) { + // Check mapped listeners for each individual event handler + for (var filter : filters) { + final var handler = filter.castHandler(event); + final var map = mappedNode.get(handler); + if (map != null) map.call(event); + } + } + // Basic listeners + if (!listeners.isEmpty()) { + for (EventListener listener : listeners) { + EventListener.Result result; + try { + result = listener.run(event); + } catch (Exception e) { + result = EventListener.Result.EXCEPTION; + MinecraftServer.getExceptionManager().handleException(e); + } + if (result == EventListener.Result.EXPIRED) { + listeners.remove(listener); + } + } + } } } }