Jump to content

Q: Best Event Bus Practice


Recommended Posts

This is just a quick question for anybody out there who has a more intimate understanding of the event registration system and Forge as a whole.


I'm creating what is planned to be a decently large mod, one that dabbles in the usual bits of world gen, tile entities that access/manipulate semi-large pools of data, potentially a few new biomes... etc. As a result of this, and knowing that such mods (if they're done well enough) will find their way into modpacks, I'm looking to fully optimize my mod performance-wise while I build it.


So far, I've created some items, blocks and tile entities, and am looking in to registering event handlers. Reading the Docs, I've noticed that there are two methods with which events can be registered. One is through the use of Forge's main event bus, accessed through MinecraftForge.EVENT_BUS and the other is the 'mod event bus', accessed through FMLJavaModLoadingContext.get().getModEventBus(). The Doc mentions that the mod event bus should only be used 'in specific cases', but fails to mention why and what those cases actually are. Thus, my main question. Which should I prefer to use and for what, and why? Oh, and does it really matter? Will it affect loading time, in-game performance, or what? And assume that I am asking for a large-scale project, as I would guess that if one was registering a single listener and three blocks that it wouldn't really matter.


Quick note: While I'm new to Forge modding, I'm experienced with Java runtime concepts as well as programming Minecraft in general (I've been writing Bukkit/Spigot Plugins for about 5 years, so I understand pretty much every basic and advanced technical concept in Minecraft). Knowing this, I'm hoping that anybody who chooses to answer will provide a more in-depth explanation of their answer, rather than 'X is more efficient than Y, trust me'. I'm always looking to gain a more in-depth understanding of Forge, especially considering that the official documentation on it is extremely rudimentary and light (as far as large APIs typically are concerned).


Quick note 2: Any other advice for performance optimization is definitely welcome!


Thanks in advance for any and all responses!

Edit: I should mention that I'm writing for 1.15, with the intention of porting to 1.16 before release, as well as potentially back-porting as distantly as 1.12, depending on various factors.

Edited by Sorcerous
Link to comment
Share on other sites

12 hours ago, diesieben07 said:

Some events are fired on the mod event bus (specifically those that need mod context attached to the event handlers). You can detect these events by seeing that they (potentially indirectly) implement IModBusEvent (I don't know if this was backported to 1.15). All other events are fired on the main Forge event bus.


Blanket performance tips are usually not very helpful, especially if we don't know the context. Additionally, premature optimization is the root of all evil, so unless you have performance problems you should write readable code, not fancy "oh so very fast" code.

Thanks for the reply, that all makes sense!


I think that I understand your sentiment about 'premature optimization', although I'd argue that good coding practice hardly qualifies under optimization. I'm not trying to prioritize milking any last bits of performance in favor of creating features, I was just wondering if there's anything that I should look out for. It seems though, by what you're saying, that code simply working is paramount.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

By using this site, you agree to our Terms of Use.