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) {
PluginClassLoader cl = new PluginClassLoader(new URL[0]);
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,
new ASMEventExecutorFactory<>(cl),
new VelocityMethodScanner());
@ -104,7 +110,8 @@ public class VelocityEventManager implements EventManager {
return CompletableFuture.completedFuture(event);
}
Runnable runEvent = () -> {
CompletableFuture<E> eventFuture = new CompletableFuture<>();
service.execute(() -> {
PostResult result = bus.post(event);
if (!result.exceptions().isEmpty()) {
logger.error("Some errors occurred whilst posting event {}.", event);
@ -113,24 +120,23 @@ public class VelocityEventManager implements EventManager {
logger.error("#{}: \n", ++i, exception);
}
}
};
CompletableFuture<E> eventFuture = new CompletableFuture<>();
service.execute(() -> {
runEvent.run();
eventFuture.complete(event);
});
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<Object> listeners = registeredListenersByPlugin.removeAll(plugin);
listeners.forEach(methodAdapter::unregister);
Collection<EventHandler<?>> handlers = registeredHandlersByPlugin.removeAll(plugin);
handlers
.forEach(handler -> bus.unregister(new KyoriToVelocityHandler<>(handler, PostOrder.LAST)));
handlers.forEach(this::unregisterHandler);
}
@Override
@ -146,7 +152,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 {
@ -163,12 +169,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
@ -196,26 +197,5 @@ public class VelocityEventManager implements EventManager {
public int 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);
}
}
}