[CORE] Subscribing to Forge events.
-
function register(eventClass, listener) { adaptedBus = new Reflector(MinecraftForge.EVENT_BUS); activeModContainer = Loader.instance().activeModContainer(); !activeModContainer && (activeModContainer = Loader.instance().getMinecraftModContainer()); adaptedBus.listenerOwners.put(listener, activeModContainer); ctr = eventClass.getConstructor(); ctr.setAccessible(true); EVENT = ctr.newInstance(); asm = new ASMEventHandler(listener, getMethod(listener, "invoke"), activeModContainer, IGenericEvent.class.isAssignableFrom(eventClass)); ieventlistener = asm; if (IContextSetter.class.isAssignableFrom(eventClass)) { ieventlistener = new (Java.extend(IEventListener, { invoke: function(e) { old = Loader.instance().activeModContainer(); Loader.instance().setActiveModContainer(owner); Java.to(e, IContextSetter).setModContainer(owner); asm.invoke(e); Loader.instance().setActiveModContainer(old); } })); } EVENT.getListenerList().register(adaptedBus.busID, EventPriority.NORMAL, listener); //useless broken shit //others = adaptedBus.listeners.computeIfAbsent(listener, function(k) new ArrayList()) //others.add(ieventlistener) } RenderGameOverlayEvent = Java.type('net.minecraftforge.client.event.RenderGameOverlayEvent'), MinecraftForge = Java.type('net.minecraftforge.common.MinecraftForge'), SubscribeEvent = Java.type('net.minecraftforge.fml.common.eventhandler.SubscribeEvent'), Event = Java.type('net.minecraftforge.fml.common.eventhandler.Event'), IGenericEvent = Java.type('net.minecraftforge.fml.common.eventhandler.IGenericEvent'), ModContainer = Java.type('net.minecraftforge.fml.common.ModContainer'), IEventListener = Java.type('net.minecraftforge.fml.common.eventhandler.IEventListener'), ASMEventHandler = Java.type('net.minecraftforge.fml.common.eventhandler.ASMEventHandler'), EventPriority = Java.type('net.minecraftforge.fml.common.eventhandler.EventPriority'), IContextSetter = Java.type('net.minecraftforge.fml.common.eventhandler.IContextSetter')
onEnable: function() { register(RenderGameOverlayEvent.Text.class, forgeListener) }
forgeListener = new (Java.extend(IEventListener, java.util.function.Consumer, { invoke: function(event) { mc.fontRenderer.drawString("test", 0, 0, -16746560) //debug T_T this.accept(event) }, accept: function(event) { //print(event.class.name) } }));
-
-
-
@idk-my-name Time to make a complete client in a client I guess
However, is this for 1.12 forge since IGenericEvent isn't in forge 1.8.9 classloader? I don't know anything about forge modding.
-
@czechhek 1.8.9 Forge's EventBus is more simple, take a look: https://github.com/MinecraftForge/MinecraftForge/blob/d06e0ad71b8471923cc809dde58251de8299a143/src/main/java/net/minecraftforge/fml/common/eventhandler/EventBus.java#L97
-
1.8.9 version (?):
function register(eventClass, listener) { //we are making reflected event bus to access private fields & methods adaptedBus = new Reflector(MinecraftForge.EVENT_BUS); //getting an actual mod container (it seems that activeModContainer() returns null in LiquidBounce so we are checking for null & redefining to minecraft mod container) activeModContainer = Loader.instance().activeModContainer(); !activeModContainer && (activeModContainer = Loader.instance().getMinecraftModContainer()); //https://github.com/MinecraftForge/MinecraftForge/blob/d06e0ad71b8471923cc809dde58251de8299a143/src/main/java/net/minecraftforge/fml/common/eventhandler/EventBus.java#L58 adaptedBus.listenerOwners.put(listener, activeModContainer); ctr = eventClass.getConstructor(); ctr.setAccessible(true); EVENT = ctr.newInstance(); //don't remove this line because event won't call without it asm = new ASMEventHandler(listener, getMethod(listener, "invoke"), activeModContainer); //registering a new listener EVENT.getListenerList().register(adaptedBus.busID, EventPriority.NORMAL, listener); others = adaptedBus.listeners.get(listener); if (!others) { others = new ArrayList(); adaptedBus.listeners.put(listener, others); } others.add(asm) }