Skip to content

Latest commit

 

History

History
82 lines (71 loc) · 4.39 KB

step1-mod-class.md

File metadata and controls

82 lines (71 loc) · 4.39 KB

Let's start MODDING

  • 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.
  • Create a new class

    • src/main/java/nz.cdr.minecrafttutorial - Right Click and New -> Class.
      • Give it the name TutorialMod.
      • Click Finish.
  • 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 called event, and that argument has a type of FMLPreInitializationEvent.
      • Press [Ctrl-Shift-O], and this will add an import at the top of the class to let the code know what the type FMLPreInitializationEvent is.
      • The code we've added should be as in this - (click on the octocat :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 the import statements at the top of the class.
      • You should end up with the following (:octocat:):

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]```` (:octocat:).

  • Add some information about our Mod:
    • Add a new class called Reference(:octocat:).

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); ```

Run the mod

  • Click the Mods button in Minecraft - you should see one listed with the name we gave ours.

Custom Items