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.

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.

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):

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):

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:

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:

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:

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!
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:

Now, hit save, alt+tab (or cmd+tab) back to Minecraft, and ask for a cookie.
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:

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!

Adam Koblentz is the Product Marketing Manager for JRebel. He has worked for several years in the financial industry writing backend frameworks and software for highly available and concurrent systems. Now he spends his days bringing awesome and liberating software to developers around the world. Adam gets satisfaction from increasing the quality of life of the every day developer.