Hello everyone and welcome to part 2 of Minecraft 1.12 modding with forge, today we are going to setup the main mod class and make minecraft load our mod. So start eclipse and start modding!
First we are going to setup some folders to structure the mod.
- Right-mouse click on the root folder in eclipse, press new and select source folder. Then as folder name use: “src/main/java”.
- Do step 1 again but instead use “src/main/resources” as folder name.
- Create a package in the “src/main/java” source folder. I will call it “com.suppergerrie2.tutorial”. But you should think of your own name, I will be referring to this package as the mod package. This package will contain all of the classes we are going to create.
- Create a class in the mod package and I will call it TutorialMod I will be referring to this class as the Main class. This class will be the entry point of the mod. Everything will start here.
- One last class before we can start coding, I will call it Reference. This class is going to contain all of the references to constant values. For example the mod version, modname, etc.
The basic files are now setup so lets start coding!
In the reference class add 4 static final String variables:
- MODID, this will be the id of your mod, it can be whatever you want but don’t make it too fancy, it also has to be all lowercase.
- MODNAME, this will be the name of your mod, this will be shown to the users of your mod. This can be as fancy as you want.
- VERSION, the version of your mod. You could think of your own versioning system or use a standard one, I am just gonna use 1.0
- ACCEPTED_MINECRAFT_VERSIONS, the versions of minecraft your mod is supposed to work with. This is in maven syntax I will use [1.12].
Your reference class should now look a little like this:
In the Main class add an @Mod annotation above the class declaration. This will tell forge that this is a mod file. In eclipse you can press ctrl-shift-o to import missing imports, press it to import net.minecraftforge.fml.common.Mod. @Mod requires the modid as an attribute, so lets add it. I will also add the name, version and accepted minecraft. It should now look like this:
Now lets add some functions, these 3 functions will be called by forge in specific loading stages. We have preInit, init and postInit. They all need an @EventHandler annotation. They all need a specific argument. preInit has FMLPreInitializationEvent, init has FMLInitializationEvent, and postInit has FMLPostInitializationEvent. I will also add some println functions for debugging.
This is what it should look like:
This is enough for forge to load the mod, to run the game press the green run button. If this doesn’t work go to Run Configurations click on the client and run.
If everything is good the game will start and load the mod. If you added the println function calls you can look in the console and they should be in there. You can also click on Mods and see if your mod is in there. If you click on your mod it will show a red warning telling you to add a file, don’t worry. We will add that now!
Going back to eclipse, go to the src/main/resources source folder and create an empty text file. Add this to the file and replace the words in capslock:
After relaunching the game the issue should be fixed.
One last thing before I will finish this part of the tutorial,
In the Main class add these 2 lines:
Forge will assign the instance of your Main class it is using to this variable.
Next time we will add an custom item, I hope to see you next time!
I also made a github repository for this tutorial, by clicking here you will be send to the code of this part, it is my first time really using github so sorry for all of the errors I probably made!
And as always, if you have any question don’t hesitate to ask in the comments! Also if you saw any errors in the tutorial please let me know so I can fix it!
Till next time!