Setting Up The Mod Development Environment


School Computers

These all should have been setup already.

Personal Computers

Java 7
Windows

Download the program JSelect

Mac

Open a Terminal window and type java -version, you should get output similar to the following:

java version "1.7.0_75"
Java(TM) SE Runtime Environment (build 1.7.0_75-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.75-b04, mixed mode)

If your major version is not 1.7, go install the latest Java 7 JDK (not JRE!!) from Oracle.

Next, you need a way to switch between JDK versions. In your terminal, type open ~/.profile. TextEdit will open, so add the following to the end of the file:

function setjdk() {
    if [ $# -ne 0 ]; then
        removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
        if [ -n "${JAVA_HOME+x}" ]; then
            removeFromPath $JAVA_HOME
        fi
        export JAVA_HOME=`/usr/libexec/java_home -v $@`
        export PATH=$JAVA_HOME/bin:$PATH
    fi
}

function removeFromPath() {
    export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}

Save and close the file. Now in the Terminal, type source ~/.profile, which reloads the file you modified. Now, you'll be able to change JDKs by typing the following into the Terminal:

setjdk 1.7 # Change the JDK to Java 7, for Minecraft
setjdk 1.8 # Change the JDK to Java 8, for JavaFX
Linux

Ensure via your package manager that the Java 7 JDK has been installed, and active.

Eclipse

Eclipse can be installed from the Eclipse website for the OS's used in our class.

Forge

Download the latest 'src' (source) version of Forge. When Eclipse installed, it installed itself in a folder in your main applications directory (C:\Program Files\, /Applications/, or /opt/). You need to save the unzipped Forge directory as forge in the Eclipse installation directory.

The next few steps will take place in the Command Prompt, or Terminal in the UNIX world. Forge uses a language called Gradle as a tool to automate many common tasks, and it is a command line application. Once your terminal is up, you need to change your working directory to the Forge directory, using the cd command. Type the following into your terminal:

# Leave a space afterwards
cd 

Open the Eclipse installation folder using Windows Explorer or the Finder, and drag and drop the Forge folder into the terminal window. It will autofill the path to the Forge folder, like this:

cd /Applications/eclipse/forge
# or
cd "C:\Program Files\Eclipse\forge"

This will put you in the Forge directory. Next, type the following:

# On Windows
gradlew setupDecompWorkspace

# On everything else
./gradlew setupDecompWorkspace

This may take many moons, but once it's done we need to run one more command to setup Eclipse.

# On Windows
gradlew eclipse

# On everything else
./gradlew eclipse

Launch Eclipse normally. You will be prompted to select a workspace. Eclipse organizes itself using workspaces, which is simply one parent folder that everything you write in Eclipse is saved. You will need to select the eclipse folder WITHIN the Forge folder (meta, eh?). You will need to always use this folder for mod development.


Understanding a Minecraft Mod

Within the src/main/java folder in the Package Explorer to the left you will find the com.example.examplemod package containing the ExampleMod.java file

package com.example.examplemod;

import net.minecraft.init.Blocks;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;

@Mod(modid = ExampleMod.MODID, version = ExampleMod.VERSION)
public class ExampleMod
{
    public static final String MODID = "examplemod";
    public static final String VERSION = "1.0";
    
    @EventHandler
    public void init(FMLInitializationEvent event)
    {
        // some example code
        System.out.println("DIRT BLOCK >> "+Blocks.dirt.getUnlocalizedName());
    }
}

Really, the only new thing syntactically are the annotation parameters on the @Mod annotation. These are used by Forge to set the Mod ID and the version number. The Mod ID need to be all lowercase letters (though underscores are fine), and the convention is name_modname

Forge mods use three main event handlers to run, preInit, init, and postInit. Mostly, we will be using preInit and init. For now, stick everything within init. preInit registers the basics of your mod (like custom items and such), init contains mod-specific code, while postInit is where code that interacts with other mods lives. You will use the template above to create all of your main files for your mods.

Many of the API we will utilize is not automatically imported. Just like NetBeans, Eclipse will give you an error and allow you to import the required class.