13
0
geforkt von Mirrors/Velocity

Merge pull request #158 from lucko/fix/event-bus

Some event bus related fixes
Dieser Commit ist enthalten in:
Andrew Steinborn 2018-12-05 20:03:07 -05:00 committet von GitHub
Commit 811d7400c3
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23

Datei anzeigen

@ -47,7 +47,13 @@ public class VelocityEventManager implements EventManager {
public VelocityEventManager(PluginManager pluginManager) { public VelocityEventManager(PluginManager pluginManager) {
PluginClassLoader cl = new PluginClassLoader(new URL[0]); PluginClassLoader cl = new PluginClassLoader(new URL[0]);
cl.addToClassloaders(); cl.addToClassloaders();
this.bus = new SimpleEventBus<>(Object.class); this.bus = new SimpleEventBus<Object>(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, this.methodAdapter = new SimpleMethodSubscriptionAdapter<>(bus,
new ASMEventExecutorFactory<>(cl), new ASMEventExecutorFactory<>(cl),
new VelocityMethodScanner()); new VelocityMethodScanner());
@ -104,7 +110,8 @@ public class VelocityEventManager implements EventManager {
return CompletableFuture.completedFuture(event); return CompletableFuture.completedFuture(event);
} }
Runnable runEvent = () -> { CompletableFuture<E> eventFuture = new CompletableFuture<>();
service.execute(() -> {
PostResult result = bus.post(event); PostResult result = bus.post(event);
if (!result.exceptions().isEmpty()) { if (!result.exceptions().isEmpty()) {
logger.error("Some errors occurred whilst posting event {}.", event); logger.error("Some errors occurred whilst posting event {}.", event);
@ -113,24 +120,23 @@ public class VelocityEventManager implements EventManager {
logger.error("#{}: \n", ++i, exception); logger.error("#{}: \n", ++i, exception);
} }
} }
};
CompletableFuture<E> eventFuture = new CompletableFuture<>();
service.execute(() -> {
runEvent.run();
eventFuture.complete(event); eventFuture.complete(event);
}); });
return eventFuture; return eventFuture;
} }
private void unregisterHandler(EventHandler<?> handler) {
bus.unregister(s -> s instanceof KyoriToVelocityHandler &&
((KyoriToVelocityHandler<?>) s).handler == handler);
}
@Override @Override
public void unregisterListeners(Object plugin) { public void unregisterListeners(Object plugin) {
ensurePlugin(plugin); ensurePlugin(plugin);
Collection<Object> listeners = registeredListenersByPlugin.removeAll(plugin); Collection<Object> listeners = registeredListenersByPlugin.removeAll(plugin);
listeners.forEach(methodAdapter::unregister); listeners.forEach(methodAdapter::unregister);
Collection<EventHandler<?>> handlers = registeredHandlersByPlugin.removeAll(plugin); Collection<EventHandler<?>> handlers = registeredHandlersByPlugin.removeAll(plugin);
handlers handlers.forEach(this::unregisterHandler);
.forEach(handler -> bus.unregister(new KyoriToVelocityHandler<>(handler, PostOrder.LAST)));
} }
@Override @Override
@ -146,7 +152,7 @@ public class VelocityEventManager implements EventManager {
ensurePlugin(plugin); ensurePlugin(plugin);
Preconditions.checkNotNull(handler, "listener"); Preconditions.checkNotNull(handler, "listener");
registeredHandlersByPlugin.remove(plugin, handler); registeredHandlersByPlugin.remove(plugin, handler);
bus.unregister(new KyoriToVelocityHandler<>(handler, PostOrder.LAST)); unregisterHandler(handler);
} }
public boolean shutdown() throws InterruptedException { public boolean shutdown() throws InterruptedException {
@ -163,12 +169,7 @@ public class VelocityEventManager implements EventManager {
@Override @Override
public int postOrder(@NonNull Object listener, @NonNull Method method) { public int postOrder(@NonNull Object listener, @NonNull Method method) {
Subscribe annotation = method.getAnnotation(Subscribe.class); return method.getAnnotation(Subscribe.class).order().ordinal();
if (annotation == null) {
throw new IllegalStateException(
"Trying to determine post order for listener without @Subscribe annotation");
}
return annotation.order().ordinal();
} }
@Override @Override
@ -196,26 +197,5 @@ public class VelocityEventManager implements EventManager {
public int postOrder() { public int postOrder() {
return postOrder; return postOrder;
} }
public EventHandler<E> 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);
}
} }
} }