Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.
See all our reports

Up Your Minecraft Mod Game with JRebel

WEBINAR ALERT! If you’ve ever been curious about our awesome tool JRebel, then join us on Sept. 28th for a live ZT Master Class: Intro to JRebel for a quick overview of the basics plus a live Q&A!

LEARN MORE


meme

If you had to guess what programming language Minecraft, one of the most popular games of all time, was written in, what would you guess? The answer is Java! We’re taking a break from our normal enterprise Java subject matter and have a little fun with making minecraft mods. Minecraft is an open-world sandbox game, where the player gets to design and build everything independently or collaboratively in a multiplayer environment. Minecraft affords a wonderful creative opportunity to anyone with a computer, or even some mobile devices like iPhone and Android smartphones (Minecraft Pocket Edition).

One of the coolest things about Minecraft is the nexus of gaming and coding that Minecraft mods represent. Not only do we get to exercise our professional programming in a more fun and personal setting, but we can also use Minecraft mods as a way to introduce kids into coding. There are some great projects right now focusing on using Minecraft mods to introduce kids to coding and problem solving, one we want to mention is Devoxx4Kids, here’s their Minecraft modding workshop and they use GitHub so you can get involved too!

Why do I need JRebel to Make Minecraft Mods?

You can certainly make a Minecraft mod without JRebel, it just won’t be as fun. You’re going to lose a lot of time to building your mod and restarting the client, that’s valuable time that you could spend having fun and playing with your mod. Playing with a mod is a lot more fun than waiting for a build. So, let’s just kill the build process with JRebel. Run through the build once, then experiment with your Minecraft mod and stay in the client. Follow along with this blog post, we’ll setup a Minecraft Forge environment, create a basic mod, and change it with JRebel.

FOLLOW ALONG WITH JREBEL NOW

Let’s get setup!

Let’s set up an environment to make a Minecraft mod. First we’ll need an IDE, I’m going to use Eclipse, but IntelliJ is also a well-supported, popular option. Next we need to download Minecraft Forge, get the “src” build from http://files.minecraftforge.net/ for Minecraft 1.8. You want to follow the normal gradle instructions for your operating system from this site: http://www.minecraftforge.net/wiki/Installation/Source. Once you have your new Eclipse workspace, you should see the minecraft project. Now, let’s install JRebel through the Eclipse marketplace.

eclipse_marketplace

Next, JRebel needs a custom plugin for Minecraft, one of our awesome devs, Henri, made one here: https://github.com/henri5/jr-minecraft-plugin, just run the maven command to build the jar file: mvn clean package and move the jar file somewhere easy to remember.

One thing of note, you may have problems enabling JRebel normally through the IDE configuration, the eclipse project that comes with forge doesn’t seem to have the appropriate metadata to be turned into a faceted project. This isn’t a big deal however, we can add the necessary jvm arguments and rebel.xml file ourselves. In the build.gradle file, add the following lines (modified for your own paths):

runClient {
    jvmArgs '-javaagent:/path/to/jrebel.jar','-Xmx1g',
            '-Drebel.plugins=/path/to/jr-minecraft-plugin-6.2.2.jar', 
            '-Drebel.minecraft.skip_reload_handlers=net.minecraft.client.audio.SoundHandler'
}

Next we need to create a rebel.xml file, this file gets read by JRebel when the JVM starts and tells JRebel where to look for new code on your disk. Usually this would be created for us, but once again, the forge project is a little wonky. Create a file named rebel.xml in the src/main/resources directory in the project, and add these lines (again, modified for your own paths):

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zeroturnaround.com" xsi:schemaLocation="http://www.zeroturnaround.com http://www.zeroturnaround.com/alderaan/rebel-2_0.xsd">
    <classpath>
        <dir name="/path/to/forge-1.8/src/main/resources">
        </dir>
        <dir name="/path/to/forge-1.8/bin/">
        </dir>
    </classpath>
</application>

Our First Minecraft Mod!

Now, we’ve got all the pieces in place in our environment, it’s time to create a Minecraft mod! Let’s create a new package in the src/main/java folder, name it com.zt.forge.mods and create a new class named Main, copy and paste this code into Main.java:

package com.zt.forge.mods;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;

@Mod(modid = Main.MODID, version = Main.VERSION)
public class Main {
  public static final String MODID = "ChatMod";
  public static final String VERSION = "1.0";

  @EventHandler
  public void init(FMLInitializationEvent event) {
    MinecraftForge.EVENT_BUS.register(new ChatItems());
  }
}

This class sets up the metadata for our mod, it’s name is ChatMod and it is version 1.0. In the init phase of our Minecraft mod, we initialize an event handler, and call our mod’s code, inside ChatItems. Let’s create ChatItems now as well. Create another class named ChatItems, and copy in this code:

package com.zt.forge.mods;

import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.event.ServerChatEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class ChatItems {
  @SubscribeEvent
  public void giveItems(ServerChatEvent event){
    if (event.message.contains("cookie")) {
      event.player.inventory.addItemStackToInventory(new ItemStack(Items.cookie, 64));
    }
  }
}

Now, in your terminal, make sure you’re in the forge directory that contains your eclipse directory. Run either gradlew runClient or ./gradlew runClient based on whether you’re using Windows or Mac/Linux. Make sure you see the JRebel banner in the console output, something like this:

2015-07-23 10:28:29 JRebel:  
2015-07-23 10:28:29 JRebel:  #############################################################
2015-07-23 10:28:29 JRebel:  
2015-07-23 10:28:29 JRebel:  JRebel Agent 6.2.2 (201506091435)
2015-07-23 10:28:29 JRebel:  (c) Copyright ZeroTurnaround AS, Estonia, Tartu.
2015-07-23 10:28:29 JRebel:  

You should see the Minecraft client open on your computer, and you should be able to load a new world and walk around. Now, let’s see if our mod works! Type t and then type the word cookie or anything with a cookie, and see what happens!
cookie
Wow, that’s cool! Our mod works and I’m sure Steve is enjoying his cookies. Let’s get Steve on a more balanced diet… Instead of cookies, we’ll give Steve a stack of apples. Replace Items.cookie with Items.apple:

event.player.inventory.addItemStackToInventory(new ItemStack(Items.apple, 64));

Now, hit save, alt+tab (or cmd+tab) back to Minecraft, and ask for a cookie.
apple
Awesome, Steve’s on his way to a healthier diet, we’ve replaced his cookies with apples, and we didn’t have to leave the world or rebuild our mod. And we can even verify JRebel reloaded the code by checking for messages in the console output like this one:

[15:53:30] [rebel-change-detector-thread/INFO] [STDOUT]: [com.zeroturnaround.javarebel.dL:logEcho:446]: 
2015-07-23 15:53:30 JRebel: Reloading class 'com.zt.forge.mods.ChatItems'.

Make Your Own Mods Too!

With JRebel you can also change textures, and other assets like sounds and JSON files. As of right now, we don’t have a good solution for adding new blocks, there isn’t really a standard way to define them. We’ll keep working on our plugin though, so keep an eye out for more info. Developing Minecraft mods may be more fun than writing enterprise Java software, but the downtime because of tooling is just as annoying. With enterprise Java apps, you have to redeploy and deal with the application server, with Minecraft mods you have to quit the Minecraft client, rebuild everything, reopen the client, then reenter the world. Luckily we have JRebel to save us time and stress, whether we’re having fun or working. Now go and make your own mods, leave a comment below and share it with us!

Start your free 14 day JRebel trial today and reload code changes instantly. Whether you’re coding for fun or profit (why not both!).


SET UP JREBEL IN THE NEXT 10 MIN!

 

If you have any questions you can find me on twitter at @akoblentz or leave a comment below!