Event listeners erroneously called more times than intended on Forge 1.21.6+
PaintNinja opened this issue ยท 2 comments
Versioning
- Dynamic Lights 1.21.10.1
- Finder Compass 1.21.6.1
- Infernal Mobs 1.21.10.1
- Ruins 1.21.10.1
- Forge 60.0.3 for MC 1.21.10
What did you do
Dynamic Lights
MinecraftForge.EVENT_BUS.register(this) is a wrapper around BusGroup.DEFAULT.register(MethodHandles.lookup(), this). Calling register twice will cause all event listeners in this class to be called twice - once from when you registered on the default bus group (the recommended way) and a second time for when you registered the same class with the legacy migration helper (MinecraftForge.EVENT_BUS).
To solve this, delete the Minecraft.EVENT_BUS.register(this) line.
Finder Compass
Similar to above, but combining EventBusSubscriber and MinecraftForge.EVENT_BUS.register(this) in the same class. To solve this, delete the Minecraft.EVENT_BUS.register(this) line.
Infernal Mobs
Same issue as Finder Compass - delete the MinecraftForge.EVENT_BUS.register(this); line as the listeners are already registered by the EventBusSubscriber annotation and registering twice now causes listeners to be called twice.
Ruins Mod
Same issue as Finder Compass and Infernal Mobs.
Expected behavior
For the listeners to only be registered once, and therefore only called once. In 1.21.6+, bulk registering this also registers static listener methods. Do not combine EventBusSubscriber, BusGroup#register and/or MinecraftForge.EVENT_BUS#register for the same class or instance. If you need a hand, I've made a migration guide here and am happy to answer any questions you have regarding the new event system.
EventBus 6 did a limited amount of deduplication, but detecting duplicates slowed down registration, increased memory usage, prevented some classes from being unloaded (causing memory leaks) and didn't always work.
In EventBus 7 I stripped that out for those reasons and instead treat it as either you explicitly want the listeners to be called multiple times, or your code is broken. While it might've not called listeners multiple times on older versions, I recommend avoiding duplicate registration there as well as it still slows down mod load times a bit, and just in case it didn't de-dupe in some cases.
Good to know that the impact of the listeners not being called twice is low though. Thanks for the quick fixes! :)
I'm afraid I'm not a rendering guy so can't help you much regarding overlay vertices, but maybe this helps point you in the right direction? https://discord.com/channels/1129059589325852724/1249189448327889007/1423362754206634096
Thanks for pointing these out. The code is very old and has lived through many versions and migrations. Impact thankfully seems small, as far i can tell.
Would it make sense to have a check for duplicate registration on the Forge side? I cant think of a usecase where you would want it.
36b3169
Finder Compass: Duplicate handshake packet upon server connect
dd68c13
Dynamic Lights: Clears cache twice, world ticks run twice but those check for xyz difference before doing expensive logic
f623d2a
Infernal Mobs: Duplicate commands registration(?), otherwise existing code prevents double execution
69e1480
Ruins: Double triggers looping chunk scan but chunks are only investigated once. Stick debug double execution.
If you got hot tips how i can get my shitty overlay vertices to render again, i would be grateful:
https://github.com/AtomicStryker/atomicstrykers-minecraft-mods/blob/1.21.10NF/FinderCompass/src/main/java/atomicstryker/findercompass/client/CompassRenderHook.java
Or maybe you are aware of a current open source mod that draws simple shapes on the UI