Wuppy’s Minecraft Forge Modding Tutorials for 1.7: Basic File Part 4: @EventHandler and more

In the last tutorial I explained the class and String lines. In this tutorial I will explain the @EventHandler and lines below from the ExampleMod file. The code from the example mod.

Once Forge knows which mods exist, it tries to run the mod. There are 3 things it does. First PreInit for every mod, after that it runs Init and finally PostInit for all of the mods found. In the ExampleMod there is only a Init, but when you are building some bigger or more complicated mods you will need more of them. There are also certain conventions of what you do in which phase, but I will explain that once we get to that part of the code in another tutorial.

The way Forge checks which methods to run and in which order it uses the @EventHandler. In the ExampleMod there is an EventHandler annotation above the init method. In the method is a parameter which is required for Forge to run properly. In the case of this method it doesn’t matter what the name of the method is. You just need to make sure that the first and only parameter in there is FMLInitializationEvent.

One thing you need to know about a method is that it is, like a class, a group of code. If you call a method all of the code in the method will be read and executed.

Now during the initialization phase of forge the code in the method will be ran. In the method are 2 lines. The first line starts with //. // in Java means comment. This will not be ran as code and simply skipped.

The second line in the method is a bit longer and more complicated. The first part of this line is System.out.println. This code enables you to write whatever comes next to the console. When you launch Minecraft in Eclipse you can read the console at the bottom. The second part of this line, which is between brackets () is what gets printed out to the console. In here are 2 more parts to be seen. The first part: “DIRT BLOCK >> ” is another String. This will simply print out to the screen except for the quotation marks. After this bit outside of the closing quotation you get a + which means also add what comes next. What comes next in this code is Blocks.dirt.func_149732_F(). This code goes into the Blocks file, where all the Block objects are saved and selects the dirt block. It then uses this object to call the method func_149732_F(). This method gets the name of the Block and returns it in a String. When you run this code in Minecraft you will see DIRT BLOCK >> followed by the name of the block.

Of course this is just test code. If you followed the advanced setup tutorial the next thing you should do is follow the custom mod project tutorial. If you went with the standard setup, follow the standard custom mod tutorial.


For a more detailed explanation and much more, check out my book Sams Teach Yourself Mod Development for Minecraft in 24 Hours.

STY Minecraft Mod Development Book

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">