Subscribing to Forge events?
Solved
ScriptAPI
-
The way I'm trying to do:
//I was just curious to extend (implement) SubscribeEvent. forgeListener = new (Java.extend(SubscribeEvent, java.util.function.Consumer, { accept: function(event) { print(event.class.name) mc.fontRenderer.drawString("test", 0, 0, -16746560) } })); function registerForgeEvent(eventClass, listener) { activeModContainer = Loader.instance().activeModContainer(); !activeModContainer && (activeModContainer = Loader.instance().getMinecraftModContainer()); adaptedBus = new Reflector(MinecraftForge.EVENT_BUS); //Core issue: when a class has two the same methods but with different parameters Core calls the first one found (even with the wrong parameter count). registerMethod = MinecraftForge.EVENT_BUS.class.getDeclaredMethod("register", java.lang.Class.class, java.lang.Object.class, java.lang.reflect.Method.class, ModContainer.class); registerMethod.setAccessible(true); adaptedBus.listenerOwners.put(listener, activeModContainer); //adaptedBus.register(eventClass, listener, getMethod(listener, "accept"), activeModContainer); registerMethod.invoke(MinecraftForge.EVENT_BUS, eventClass, listener, getMethod(listener, "accept"), activeModContainer); } function unregisterForgeEvent(listener) { adaptedBus = new Reflector(MinecraftForge.EVENT_BUS); adaptedBus.unregister(listener); }
But I get this error:
[11:49:50] [Client thread/ERROR] [FML]: Error registering event handler: minecraft class net.minecraftforge.client.event.RenderGameOverlayEvent$Text public void net.minecraftforge.fml.common.eventhandler.SubscribeEvent$$NashornJavaAdapter$$Consumer.accept(java.lang.Object) java.lang.NullPointerException: null at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.getPriority(ASMEventHandler.java:100) ~[ASMEventHandler.class:?] at net.minecraftforge.fml.common.eventhandler.EventBus.register(EventBus.java:150) [EventBus.class:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_202] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_202] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_202] at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_202] at java.lang.invoke.MethodHandleImpl$BindCaller$T/545451212.invoke_V(MethodHandleImpl.java:1258) [?:1.8.0_202] at jdk.nashorn.internal.scripts.Script$Recompilation$1240$3684AA$\^eval\_.registerForgeEvent(<eval>:88) [?:?] at jdk.nashorn.internal.scripts.Script$Recompilation$1196$332$\^eval\_.onEnable(<eval>:14) [?:?] at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:657) [ScriptFunctionData.class:?] at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494) [ScriptFunction.class:?] at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393) [ScriptRuntime.class:?] at jdk.nashorn.api.scripting.ScriptObjectMirror.call(ScriptObjectMirror.java:117) [ScriptObjectMirror.class:?] at net.ccbluex.liquidbounce.script.api.ScriptModule.callEvent(ScriptModule.kt:129) [ScriptModule.class:?] at net.ccbluex.liquidbounce.script.api.ScriptModule.callEvent$default(ScriptModule.kt:127) [ScriptModule.class:?] at net.ccbluex.liquidbounce.script.api.ScriptModule.onEnable(ScriptModule.kt:70) [ScriptModule.class:?] at net.ccbluex.liquidbounce.features.module.Module.setState(Module.kt:76) [Module.class:?] at net.ccbluex.liquidbounce.features.module.Module.toggle(Module.kt:114) [Module.class:?] at net.ccbluex.liquidbounce.ui.client.clickgui.elements.ModuleElement.mouseClicked(ModuleElement.java:43) [ModuleElement.class:?] at net.ccbluex.liquidbounce.ui.client.clickgui.Panel.mouseClicked(Panel.java:112) [Panel.class:?] at net.ccbluex.liquidbounce.ui.client.clickgui.ClickGui.mouseClicked(ClickGui.java:261) [ClickGui.class:?] at net.ccbluex.liquidbounce.injection.backend.utils.GuiScreenWrapper.func_73864_a(GuiScreenWrapper.kt:17) [GuiScreenWrapper.class:?] at net.minecraft.client.gui.GuiScreen.func_146274_d(GuiScreen.java:533) [blk.class:?] at net.ccbluex.liquidbounce.injection.backend.utils.GuiScreenWrapper.superHandleMouseInput(GuiScreenWrapper.kt:36) [GuiScreenWrapper.class:?] at net.ccbluex.liquidbounce.injection.backend.GuiScreenImpl.superHandleMouseInput(GuiScreenImpl.kt:42) [GuiScreenImpl.class:?] at net.ccbluex.liquidbounce.api.util.WrappedGuiScreen.handleMouseInput(WrappedGuiScreen.kt:33) [WrappedGuiScreen.class:?] at net.ccbluex.liquidbounce.injection.backend.utils.GuiScreenWrapper.func_146274_d(GuiScreenWrapper.kt:20) [GuiScreenWrapper.class:?] at net.minecraft.client.gui.GuiScreen.func_146269_k(GuiScreen.java:501) [blk.class:?] at net.minecraft.client.Minecraft.func_71407_l(Minecraft.java:1759) [bib.class:?] at net.minecraft.client.Minecraft.func_71411_J(Minecraft.java:1098) [bib.class:?] at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:4942) [bib.class:?] at net.minecraft.client.main.Main.main(SourceFile:123) [Main.class:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_202] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_202] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_202] at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_202] at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?] at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_202] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_202] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_202] at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_202] at misc.tweaker.Tweaker.a(Unknown Source) [tweaker-1.2.jar:?] at misc.tweaker.Tweaker.stubMain(Unknown Source) [tweaker-1.2.jar:?] at misc.tweaker.StubMain.main(Unknown Source) [tweaker-net.minecraft.launchwrapper.Launch.jar:?]
-
I copied the way of registering methods from https://github.com/MinecraftForge/MinecraftForge/blob/d3f01843f7e7a4f613b5e8113d381fd8747b4343/src/main/java/net/minecraftforge/fml/common/eventhandler/EventBus.java#L65
-