Main Mod Class

In this tutorial you will learn how to code the very basics of Minecraft Forge mods, its backbone class and the necessities to build an empty mod.

For Forge to “see” your mod, you need a main class which will initialize your mod content.

The code is as follows:

package com.emx.tutorial;

@Mod(modid = TutorialMod.MODID, name = TutorialMod.NAME, version = TutorialMod.VERSION)

public class TutorialMod
{
    public static final String NAME = "Tutorial Mod";
    public static final String MODID = "tutorial";
    public static final String VERSION = "1.7.10-R1";

    @Mod.Instance("tutorial")
    public static TutorialMod instance;

    @Mod.EventHandler
    public void preInit(FMLPreInitializationEvent event) {}

    @Mod.EventHandler
    public void init(FMLInitializationEvent event) {}

    @Mod.EventHandler
    public void postInit(FMLPostInitializationEvent event) {}
}

There’s your basic mod class.

As you can see in the code, I put this class in the package “com.emx.tutorial”. There is a Java convention of package names that should use in your code. It is made from your inverted website URL and your mod name, so for instance my website is emx.com, my package name is com.emx.tutorial.

@Mod(modid = TutorialMod.MODID, name = TutorialMod.NAME, version = TutorialMod.VERSION)

For this line, you might wonder why I didn’t put the strings directly, but instead I made a variable. This is recommended because over time you will need your mod ID everywhere in your mod. You can just rewrite the mod ID everywhere, but the problem comes if you want to change it (you shouldn’t). By using this method you can change it on the variable declaration and it will change everywhere else.

As for the versioning, “1.7.10-R1” is just my favourite way to write version numbers. If you don’t like this, feel free to use any other ways. In my case, I put the Minecraft version followed by a hyphen (-), and then the mod version. R1 stands for Release 1, A1 for Alpha 1, B1 for Beta 1.

Now for the mod instance.

@Mod.Instance("tutorial")
public static TutorialMod instance;

You don’t actually need it if you are making really simple mods, but it’s a good practice to do, because when making more complex mods you will need your mod’s instance.

public void preInit(FMLPreInitializationEvent event) {}
public void init(FMLInitializationEvent event) {}
public void postInit(FMLPostInitializationEvent event) {}

These are the three basic stages where you can initialize your mod contents. The preInit method will get called first, followed by init, then postInit. So if your mod has anything to do that needs Minecraft to be fully loaded, you do that in the postInit method. Also, all of these have to have the @Mod.EventHandler (or @EventHandler, same thing) annotation.

The next tutorials will cover how to add items, blocks and other fun stuff, so hang on!

8 thoughts on “Main Mod Class

  1. Tstomix July 9, 2015 / 8:46 PM

    I would of liked to known more in detail. For example:

    @Mod.Instance(“tutorial”)
    public static TutorialMod instance;

    Just a more in depth explanation; but I don’t know much java so that’s probably why I don’t quite get it.

    Like

    • Emx2000 July 9, 2015 / 11:26 PM

      That makes an instance of your mod, to be called by forge, or any other mods if needed. You don’t need that when making simple mods, but when you start doing integration and more advanced stuff, like GUIs, you’ll need that, so that forge can call your mod’s instance. Also, the parameter in that annotation must be the exact same as your modid, case sensitive.

      Like

    • Emx2000 February 1, 2016 / 4:51 PM

      Main mod class is in src/main/java.

      Like

      • Will March 19, 2016 / 10:38 AM

        Under the Forge package, or the Tutorial Mod package? Cause I have two packages in the left pane and going by what your previous guide had us make I don’t have an src/main/java in the Tutorial Mod package. These guides are not being detailed enough. I directly copied your code and have 16 errors… Mostly “XXX cannot resolve to a type” errors…

        Liked by 1 person

      • Emx2000 March 20, 2016 / 10:45 AM

        So there’s a forge folder, and a workspace folder. You must set your eclipse work folder to this workspace folder. And then you can import the forge project into the workspace (via eclipse). To create a mod you create a new project, and that’s where you make a src/main/java source folder.

        Like

      • staticextasy May 25, 2016 / 1:54 PM

        I’m with Will on this, we import Forge with the MDK we downloaded. it adds “forge” folder under our workspace and you told us to create “src/java” under our main mod folder not src/main/java which is indeed inside the “forge” folder on the explorer window. I challenge you to follow your exact instructions to a T and you’ll begin to understand somewhere along the lines you need to correct your steps and add some correct procedures.

        Like

      • Emx2000 June 5, 2016 / 2:25 PM

        You are correct. I have updated the tutorial regarding this, thanks!

        Like

Leave a comment