Forge Mod Introduction - FloppaCoding/AtonAddons GitHub Wiki
Introduction to Forge mods
This page contains a basic introduction into the key concepts required for understanding Forge mods.
How you can modify Minecraft.
Forge mods are driven by events. These get posted whenever a certain part of the vanilla game code is executed. In your mod you can listen for these events to perform an action whenever the event you are listening for occurs. In code this looks like the following:
@SubscribeEvent
fun onWorldLoad(event: WorldEvent.Unload) {
// This code will be run when the world gets unloaded
}
Here the annotation @SubscribeEvent
marks that the method is listening for events and the argument type WorldEvent.Unload
specifies for which type of event you are listening.
For this to work you need to register instances of classes in which you listen to these events to the Forge Event Bus.
This can be done with:
MinecraftForge.EVENT_BUS.register(InstanceOfYourClass)
With these event listeners you can realize most of the things you could want.
Something else you will need a lot is to access Objects, data and methods from within Minecraft.
Some examples are the Player Object or other entities in the World. You can access all of that through the Minecraft
instance of the running game, which can be obtained through Minecraft.getMinecraft()
. For convenience, it is
recommended
that you create a central reference to that Minecraft Instance once and just use that.
val mc: Minecraft = Minecraft.getMinecraft()
This should be somewhere in your main class.
While you can use these event listeners to make most things like running additional evaluations on a per tick basis, registering chat messages, rendering things in the World or HUD and way more, there are still limitations. There are a limited amount of events, and you still can't directly modify the minecraft code. To solve that you will need mixins.
Mixins
Mixins allow you to "mixin" your own code into the Minecraft code and directly modify it. This allows you to create custom events and way more.
This should be all you need to know for now, and I will not go into detail on how to use mixins here for now. If you want to know more you can use the following resources to read more about mixins.
The best starting point to learn more about mixins might be this page in the Fabric Wiki. Although it's not forge the mixin stuff is still pretty much the same. This Cheatsheet is very helpful to correctly use mixins once you understand the core functionality. You can also check out the official documentation, but that unfortunately is incomplete especially when it comes to how to use mixins.
A general thing to keep in mind with mixins is that multiple mods might end trying to modify the same part of Minecraft code, which can lead to a lot of unexpected problems. So I recommend that unless required it is generally best to use as little mixins as possible.
Something else that is good practice when working with mixins, is to do as little coding as possible within the mixin itself. Instead, it is better to use forge events or directly reference a method that you have implemented within your code. On top of that I recommend that whenever you just want to access properties from within your mixin that you don't directly reference the value, but instead use a custom getter function that gets the value for you. The purpose of this is to make the code more organized.