Mod: Chainsaw

This is the thirteenth lesson in a series introducing 10-year-olds to programming through Minecraft. Learn more here.

This is another suggestion from my class :)

Goal

To create a chainsaw item that can cut wood really quickly.

Relevant Classes

Item definitions extend net.minecraft.item.Item. You may recall that instances of items are created using ItemStack. Kinda sounds like classes and objects? :)

Creating A New Item

Add a new class named Chainsaw with a Superclass of net.minecraft.item.ItemAxe. Check "constructors from superclass". Update the constructor to look like this (we'll worry about the last line a bit later)

public class Chainsaw extends ItemAxe {
    public Chainsaw(int id, EnumToolMaterial material) {
        super(id, material);
        setUnlocalizedName("chainsaw");
        func_111206_d("chainsawmod:chainsaw");
    }
}

Let's say we want our chainsaw to be a faster version of an emerald axe. One way to do this is to edit EnumToolMaterial found in net.minecraft.item and add a new value like this:

MODERN(3, 2000, 200.0F, 3.0F, 10) 

... but that would make life more difficult for us later on. Luckily, there is a shortcut we can use by adding these lines to our main mod file.

import net.minecraftforge.common.EnumHelper;

public static EnumToolMaterial ModernMaterial = 
    EnumHelper.addToolMaterial(
         "MODERN", 3, 5000, 200.0F, 3.0F, 10);

You can see more details about each value in the source code; the important one is the third number, efficiencyOnProperMaterial. An emerald axe has a value of 8.0 (a gold axe has a value of 12.0, but it doesn't last as long). Ours has a value of 200! We can chop down trees pretty much instantaneously with our chainsaw...

Assigning an Icon

You'll notice that all the tools in Minecraft have a small image, or icon, that is displayed in your inventory. I've found one we can use for our chainsaw at http://findicons.com/icon/53591/chainsaw. Luckily they already have 16x16 icons with transparent backgrounds, otherwise we'd have to do some image editing in Paint.NET.

In our mod file, there is a line that looks like this

@Mod(modid="ChainsawMod", name="...", version="...")

We need to create a directory with the same name as our modid (with all lowercase letters) in {workspace}forgemcpsrcminecraftassets. Under that directory, we then create texturesitems and place our new PNG file there. For example, I have

D:\eclipse-workspace
   \forge
   \mcp
   \src
   \minecraft
   \assets
   \chainsawmod
   \textures
   \items
   \chainsaw.png

Now, remember that weird-looking third line in the constructor of our new item?

func_111206_d("chainsawmod:chainsaw");

They haven't gotten around to naming that (yet) - it should be called setItemIcon or something similar, but what it does is tell Minecraft that the icon for our item is located under the assets directory in chainsawmod and that the filename is chainsaw.png. It assumes there's a textures directory as well.

Adding the Item to the Game

In our main mod class, we can now create an instance of the Chainsaw

public static Chainsaw chainsaw = 
    new Chainsaw(5000, ModernMaterial);

and then inside load we can register it with the game

GameRegistry.registerItem(chainsaw, "Chainsaw");
LanguageRegistry.addName(chainsaw, "Chainsaw");

If you wanted to create a crafting recipe that outputs this item you could, but I just cheated and added it to the ConnectionHandler we created earlier. Here's what I see :)

chainsaw

Note: if you're using eclipse, you also need to right-click on src, Create > New Folder, Advanced, Link, and choose src/minecraft from the path above otherwise you won't see the chainsaw icon.

comments powered by Disqus