-
You're ready to start creating a mod - we're going to follow MrCrayfish's Minecraft 1.8 Modding tutorial - Episode 2 - Mod Class.
-
We should have already set up our development environment.
-
Delete the example mod
src/main/java/
-com.example.examplemod
- Right Click and delete.
-
Create a new package
src/main/java/
- Right Click and New -> Package.- In Java, packages often are often given a name of an organisation's website, but in reverse. So http://cdr.nz would become
nz.cdr
. - Think of each dot being a new directory.
- Why do you think this might be?
- We'll use the name
nz.cdr.minecrafttutorial
for our package, or you can use your website, or minecraft username. - Click Finish.
- In Java, packages often are often given a name of an organisation's website, but in reverse. So http://cdr.nz would become
-
Create a new class
src/main/java/nz.cdr.minecrafttutorial
- Right Click and New -> Class.- Give it the name
TutorialMod
. - Click Finish.
- Give it the name
-
Add initialisation functions
- There are three initialisation events we need to handle:
- For the first, type the following inside the class:
public void preInit(FMLPreInitializationEvent event) { }
public
means that this function can be accessed by other parts of the program.void
means that it doesn't return anything when it has finished. Functions can return a value after they've executed, or return no value.- Note the American spelling of initialize - this is common in a lot of programming (even program is an American spelling!).
- The
(FMLPreInitializationEvent event)
part is the argument list, so the function is called with an argument calledevent
, and that argument has a type ofFMLPreInitializationEvent
. - Press
[Ctrl-Shift-O]
, and this will add animport
at the top of the class to let the code know what the typeFMLPreInitializationEvent
is. - The code we've added should be as in this - (click on the octocat ).
- For the second and third, copy the previous code block you added, but change the function name and argument type as follows:
init
,FMLInitializationEvent
postInit
,FMLPostInitializationEvent
- Press
[Ctrl-Shift-O]
again to add theimport
statements at the top of the class. - You should end up with the following ():
- There are three initialisation events we need to handle:
package nz.cdr.minecrafttutorial;
import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
public class TutorialMod { public void preInit(FMLPreInitializationEvent event) {
}
public void init(FMLInitializationEvent event)
{
}
public void postInit(FMLPostInitializationEvent event)
{
}
}
```
- Add @EventHandler
annotation above each of the initialisation functions, and again ````[Ctrl-Shift-O]```` ().
package nz.cdr.minecrafttutorial;
public class Reference {
public static final String MOD_ID = "tm";
public static final String MOD_NAME = "Tutorial Mod";
public static final String VERSION = "1.0";
}
- Add a line in our Tutorial class with an
@Modannotation ([:octocat:](https://github.com/CoderDojo-Porirua/minecraft-forge-1.8/commit/53d0e593e335c5d2d57da1a736f24200550769ed)).:
java
@Mod(modid = Reference.MOD_ID, name = Reference.MOD_NAME, version = Reference.VERSION);
```
- Click the Mods button in Minecraft - you should see one listed with the name we gave ours.
- Now lets create some custom items.