Эх сурвалжийг харах

Add EventFactory.attachToForge

shedaniel 4 жил өмнө
parent
commit
7513980699

+ 6 - 0
common/src/main/java/me/shedaniel/architectury/event/EventFactory.java

@@ -17,6 +17,7 @@
 package me.shedaniel.architectury.event;
 package me.shedaniel.architectury.event;
 
 
 import com.google.common.reflect.AbstractInvocationHandler;
 import com.google.common.reflect.AbstractInvocationHandler;
+import me.shedaniel.architectury.ExpectPlatform;
 import net.jodah.typetools.TypeResolver;
 import net.jodah.typetools.TypeResolver;
 import net.minecraft.world.InteractionResult;
 import net.minecraft.world.InteractionResult;
 import net.minecraft.world.InteractionResultHolder;
 import net.minecraft.world.InteractionResultHolder;
@@ -112,6 +113,11 @@ public final class EventFactory {
         }));
         }));
     }
     }
     
     
+    @ExpectPlatform
+    public static <T> Event<Consumer<T>> attachToForge(Event<Consumer<T>> event) {
+        throw new AssertionError();
+    }
+    
     private static class EventImpl<T> implements Event<T> {
     private static class EventImpl<T> implements Event<T> {
         private final Function<T[], T> function;
         private final Function<T[], T> function;
         private T invoker = null;
         private T invoker = null;

+ 11 - 0
fabric/src/main/java/me/shedaniel/architectury/event/fabric/EventFactoryImpl.java

@@ -0,0 +1,11 @@
+package me.shedaniel.architectury.event.fabric;
+
+import me.shedaniel.architectury.event.Event;
+
+import java.util.function.Consumer;
+
+public class EventFactoryImpl {
+    public static <T> Event<Consumer<T>> attachToForge(Event<Consumer<T>> event) {
+        return event;
+    }
+}

+ 18 - 0
forge/src/main/java/me/shedaniel/architectury/event/forge/EventFactoryImpl.java

@@ -0,0 +1,18 @@
+package me.shedaniel.architectury.event.forge;
+
+import me.shedaniel.architectury.event.Event;
+import net.minecraftforge.common.MinecraftForge;
+
+import java.util.function.Consumer;
+
+public class EventFactoryImpl {
+    public static <T> Event<Consumer<T>> attachToForge(Event<Consumer<T>> event) {
+        event.register(eventObj -> {
+            if (!(eventObj instanceof net.minecraftforge.eventbus.api.Event)) {
+                throw new ClassCastException(eventObj.getClass() + " is not an instance of forge Event!");
+            }
+            MinecraftForge.EVENT_BUS.post((net.minecraftforge.eventbus.api.Event) eventObj);
+        });
+        return event;
+    }
+}