Monday, March 2, 2015

Minecraft Tutorials: The Basic Mod Class

The following will be the first in a series of tutorials aimed to provide instruction for creating a mod that adds a new custom machine to Minecraft utilizing the forge api. The version of Minecraft we will be using is 1.7.10 and the version of forge is 10.13.2.1291.

Disclaimer: I have tested this code and it works for the above versions of forge and Minecraft. I do not promise that it will work for other versions of Minecraft (in fact, I know it won't without at least some modification) or for earlier versions of forge. I do not promise that this is the best possible implementation to achieve the stated goals of the tutorial for any value of the word "best". I do promise that I have tested the code and it works both in the testing environment, and when built and tested in Minecraft with a variety of other mods.

This tutorial assumes that you have properly set up forge with your desired IDE. The IDE I use is eclipse and instruction may assume the use of eclipse, but it isn't as though the code would be different if you use another IDE of your choice. This tutorial also assumes that you have at least a basic understanding of object-oriented programming, preferably in java since that is the language we will be using.

To begin we will create a new package which will be the main package for our mod, if you're using eclipse you do this by right clicking on the java folder in your package explorer and selecting "package" from the "new" drop down list. Then name your package. Convention states that this should begin with your domain name, if you have one.  If you don't have one you can use me.myname.mypackagename.  I will be using me.codasylph.grindermod.

Now we create a new class inside this package.  This will be the main class for your mod.  Generally its a good idea to name it something close to your mod name. My class will be called GrinderMod.

Your new class should look something like this:

package me.codasylph.grindermod;

public class GrinderMod {


}

Outside the class declaration we want to add the @Mod annotation followed by some information.  This lets forge know to load your mod with the listed modid, name, and version.  Note that the modid ought to be in lower case.  It should look something like this:
@Mod(modid = "grindermod", name = "Grinder Mod", version = "1.0")

These strings can also be variables if you wanted to have a separate file for your strings, which is good practice, but not a necessity.

At this point you will likely have an error because @Mod requires the package cpw.mods.fml.common.Mod to be imported.  If you are using eclipse you can just hover over the offending word and tell it to import or press Crtl + Shift + O.

Inside the class declaration we want to add three new methods each with the @Mod.EventHandler annotation, these will dictate what aspects of your mod get loaded when.  The whole mod file will then (assuming you also add the needed imports) look like this:

package me.codasylph.grindermod;

import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;

import cpw.mods.fml.common.event.FMLPreInitializationEvent;

@Mod(modid = "grindermod", name = "Grinder Mod", version = "1.0")

public class GrinderMod

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

}

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

}

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


}
}

What are these methods for you ask? As stated before they dictate when "things" get loaded by forge.  Forge loads in three stages, first the preInit, which is where any Blocks, Items, or worldgen, etc should be registered. Then init, which is where TileEnities, Events, Renderers, etc should be registered. Finally, postInit, which is mostly for addons to other mods, we won't really be using postInit in this tutorial.

Now, if you went ahead and ran this in the testing environment (you can do this in eclipse by clicking "Run Client" which looks like a white play button in a green circle) and then clicked the Mods button, it should show up in the list. You have created your first mod.  It doesn't do a thing, but it is a mod!

In my next tutorial we will discuss adding a brand new block to the mod!

You can visit the table of contents here.

No comments:

Post a Comment