From 43da3d45417c27446a0825b472cf96e5117d7437 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 3 Dec 2018 23:53:06 +0000 Subject: [PATCH 1/4] Allow the same EventHandler to be registered with different PostOrders The #equals implementations in KyoriToVelocityHandler silently prevented this previously. --- .../proxy/plugin/VelocityEventManager.java | 31 +++++-------------- 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java index cb2223e88..a9d9f8ce5 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java @@ -123,14 +123,18 @@ public class VelocityEventManager implements EventManager { return eventFuture; } + private void unregisterHandler(EventHandler handler) { + bus.unregister(s -> s instanceof KyoriToVelocityHandler && + ((KyoriToVelocityHandler) s).handler == handler); + } + @Override public void unregisterListeners(Object plugin) { ensurePlugin(plugin); Collection listeners = registeredListenersByPlugin.removeAll(plugin); listeners.forEach(methodAdapter::unregister); Collection> handlers = registeredHandlersByPlugin.removeAll(plugin); - handlers - .forEach(handler -> bus.unregister(new KyoriToVelocityHandler<>(handler, PostOrder.LAST))); + handlers.forEach(this::unregisterHandler); } @Override @@ -146,7 +150,7 @@ public class VelocityEventManager implements EventManager { ensurePlugin(plugin); Preconditions.checkNotNull(handler, "listener"); registeredHandlersByPlugin.remove(plugin, handler); - bus.unregister(new KyoriToVelocityHandler<>(handler, PostOrder.LAST)); + unregisterHandler(handler); } public boolean shutdown() throws InterruptedException { @@ -196,26 +200,5 @@ public class VelocityEventManager implements EventManager { public int postOrder() { return postOrder; } - - public EventHandler getHandler() { - return handler; - } - - @Override - public boolean equals(@Nullable Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - KyoriToVelocityHandler that = (KyoriToVelocityHandler) o; - return Objects.equals(handler, that.handler); - } - - @Override - public int hashCode() { - return Objects.hash(handler); - } } } From cfae542dd21655210037ef93ed226ecff3d7cd14 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 3 Dec 2018 23:53:50 +0000 Subject: [PATCH 2/4] Slightly optimize SimpleEventBus usage in VelocityEventManager --- .../proxy/plugin/VelocityEventManager.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java index a9d9f8ce5..b0ec5cfdd 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java @@ -47,7 +47,13 @@ public class VelocityEventManager implements EventManager { public VelocityEventManager(PluginManager pluginManager) { PluginClassLoader cl = new PluginClassLoader(new URL[0]); cl.addToClassloaders(); - this.bus = new SimpleEventBus<>(Object.class); + this.bus = new SimpleEventBus(Object.class) { + @Override + protected boolean shouldPost(@NonNull Object event, @NonNull EventSubscriber subscriber) { + // Velocity doesn't use Cancellable or generic events, so we can skip those checks. + return true; + } + }; this.methodAdapter = new SimpleMethodSubscriptionAdapter<>(bus, new ASMEventExecutorFactory<>(cl), new VelocityMethodScanner()); From 3d3d6adf0409713a1d3d9f3bba6016995dbf6144 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 3 Dec 2018 23:55:39 +0000 Subject: [PATCH 3/4] Remove redundant null check in VelocityMethodScanner --- .../velocitypowered/proxy/plugin/VelocityEventManager.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java index b0ec5cfdd..7526a50a2 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java @@ -173,12 +173,7 @@ public class VelocityEventManager implements EventManager { @Override public int postOrder(@NonNull Object listener, @NonNull Method method) { - Subscribe annotation = method.getAnnotation(Subscribe.class); - if (annotation == null) { - throw new IllegalStateException( - "Trying to determine post order for listener without @Subscribe annotation"); - } - return annotation.order().ordinal(); + return method.getAnnotation(Subscribe.class).order().ordinal(); } @Override From 6ff91809879f7f128842f50cc9c73dc0a5982c49 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 4 Dec 2018 00:03:47 +0000 Subject: [PATCH 4/4] Remove redundant extra lambda in EventManager#fire implementation --- .../proxy/plugin/VelocityEventManager.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java index 7526a50a2..4cfa88de0 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java @@ -110,7 +110,8 @@ public class VelocityEventManager implements EventManager { return CompletableFuture.completedFuture(event); } - Runnable runEvent = () -> { + CompletableFuture eventFuture = new CompletableFuture<>(); + service.execute(() -> { PostResult result = bus.post(event); if (!result.exceptions().isEmpty()) { logger.error("Some errors occurred whilst posting event {}.", event); @@ -119,11 +120,6 @@ public class VelocityEventManager implements EventManager { logger.error("#{}: \n", ++i, exception); } } - }; - - CompletableFuture eventFuture = new CompletableFuture<>(); - service.execute(() -> { - runEvent.run(); eventFuture.complete(event); }); return eventFuture;