Fix error message when event handlers throw an exception

This commit is contained in:
Luck
2018-12-22 17:03:51 +00:00
Unverified
parent fc69002c86
commit 2ccffc6e6d
3 changed files with 15 additions and 2 deletions
@@ -43,4 +43,12 @@ public interface LuckPermsEvent {
*/ */
@NonNull LuckPermsApi getApi(); @NonNull LuckPermsApi getApi();
/**
* Gets the type of the event.
*
* @return the type of the event
* @since 4.4
*/
@NonNull Class<? extends LuckPermsEvent> getEventType();
} }
@@ -108,7 +108,7 @@ public class LuckPermsEventHandler<T extends LuckPermsEvent> implements EventHan
this.consumer.accept(event); this.consumer.accept(event);
this.callCount.incrementAndGet(); this.callCount.incrementAndGet();
} catch (Throwable t) { } catch (Throwable t) {
this.eventBus.getPlugin().getLogger().warn("Unable to pass event " + event.getClass().getSimpleName() + " to handler " + this.consumer.getClass().getName()); this.eventBus.getPlugin().getLogger().warn("Unable to pass event " + event.getEventType().getSimpleName() + " to handler " + this.consumer.getClass().getName());
t.printStackTrace(); t.printStackTrace();
} }
} }
@@ -52,12 +52,14 @@ public class GeneratedEventSpec {
private static final Method EQUALS_METHOD; private static final Method EQUALS_METHOD;
private static final Method HASHCODE_METHOD; private static final Method HASHCODE_METHOD;
private static final Method GET_API_METHOD; private static final Method GET_API_METHOD;
private static final Method GET_EVENT_TYPE_METHOD;
static { static {
try { try {
TO_STRING_METHOD = Object.class.getMethod("toString"); TO_STRING_METHOD = Object.class.getMethod("toString");
EQUALS_METHOD = Object.class.getMethod("equals", Object.class); EQUALS_METHOD = Object.class.getMethod("equals", Object.class);
HASHCODE_METHOD = Object.class.getMethod("hashCode"); HASHCODE_METHOD = Object.class.getMethod("hashCode");
GET_API_METHOD = LuckPermsEvent.class.getMethod("getApi"); GET_API_METHOD = LuckPermsEvent.class.getMethod("getApi");
GET_EVENT_TYPE_METHOD = LuckPermsEvent.class.getMethod("getEventType");
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
throw new ExceptionInInitializerError(e); throw new ExceptionInInitializerError(e);
} }
@@ -81,7 +83,7 @@ public class GeneratedEventSpec {
if (method.isDefault()) { if (method.isDefault()) {
continue; continue;
} }
if (GET_API_METHOD.equals(method)) { if (GET_API_METHOD.equals(method) || GET_EVENT_TYPE_METHOD.equals(method)) {
continue; continue;
} }
@@ -141,6 +143,9 @@ public class GeneratedEventSpec {
if (GET_API_METHOD.equals(method)) { if (GET_API_METHOD.equals(method)) {
return this.api; return this.api;
} }
if (GET_EVENT_TYPE_METHOD.equals(method)) {
return GeneratedEventSpec.this.eventClass;
}
if (method.getDeclaringClass() == Object.class || method.isDefault()) { if (method.getDeclaringClass() == Object.class || method.isDefault()) {
return MethodHandles.lookup() return MethodHandles.lookup()