From e23ca38572e8d87700b799413b6777f836d523d3 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sun, 15 Nov 2020 23:01:18 +0800 Subject: [PATCH] automatically attach to forge event if the class is annotated with @ForgeEvent. --- .../me/shedaniel/architectury/event/EventFactory.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/me/shedaniel/architectury/event/EventFactory.java b/common/src/main/java/me/shedaniel/architectury/event/EventFactory.java index 131ac541..772f5b10 100644 --- a/common/src/main/java/me/shedaniel/architectury/event/EventFactory.java +++ b/common/src/main/java/me/shedaniel/architectury/event/EventFactory.java @@ -18,6 +18,7 @@ package me.shedaniel.architectury.event; import com.google.common.reflect.AbstractInvocationHandler; import me.shedaniel.architectury.ExpectPlatform; +import me.shedaniel.architectury.ForgeEvent; import net.jodah.typetools.TypeResolver; import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResultHolder; @@ -86,7 +87,7 @@ public final class EventFactory { @SuppressWarnings("UnstableApiUsage") public static Event> createConsumerLoop(Class clazz) { - return create(listeners -> (Consumer) Proxy.newProxyInstance(EventFactory.class.getClassLoader(), new Class[]{Consumer.class}, new AbstractInvocationHandler() { + Event> event = create(listeners -> (Consumer) Proxy.newProxyInstance(EventFactory.class.getClassLoader(), new Class[]{Consumer.class}, new AbstractInvocationHandler() { @Override protected Object handleInvocation(@NotNull Object proxy, @NotNull Method method, Object @NotNull [] args) throws Throwable { for (Consumer listener : listeners) { @@ -95,6 +96,14 @@ public final class EventFactory { return null; } })); + Class superClass = clazz; + do { + if (superClass.isAnnotationPresent(ForgeEvent.class)) { + return attachToForge(event); + } + superClass = superClass.getSuperclass(); + } while (superClass != null); + return event; } @SuppressWarnings("UnstableApiUsage")