Monday, March 2, 2015

Minecraft Tutorials: A Basic Tool

In this installation of my Minecraft modding tutorial series I will discuss creating custom pickaxes, swords, shovels, etc.  This tutorial assumes you have an understanding of how to create a basic mod file, and a basic item.  Oh, and a basic understanding of java also important, I keep forgetting to mention that one.  The code I'll show is built off of all previous tutorials so if you did not follow all the tutorials your code may be slightly different, that's ok.

The example I'll use is a Copper Pickaxe.
Most of the stats of a Minecraft tool (this includes swords) actually come from the ToolMaterial. This provides the tool's harvest level, durability, mining speed, damage, and enchant-ability.  If you wish these stats to be the same as a preexisting material like wood, or diamond, then you don't need to create a new material (although you might ask yourself why you're creating a new tool at all).

ToolMaterial is an enum and not a class so we can't create a new class that extends it.  Instead we will use forge's EnumHelper class to add our new material to the list.

To do this I will edit my ModItems.class to add the line
public static ToolMaterial COPPER = EnumHelper.addToolMaterial("COPPER", 2, 160, 8.0F, 1.0F, 10);

If you're not following along exactly with the tutorial series you can initialize your new ToolMaterial in your main mod file, or in a new class all its own.

addToolMaterials takes 6 parameters:
String Name is just the String name of the material.

int harvestLevel indicates the harvest level of tools made of this material. For reference vanilla harvest levels are as follows:
  • 0 Wood or Gold
  • 1 Stone
  • 2 Iron
  • 3 Diamond
copper tools will be able to harvest the same things as iron ones.  If we wanted we could give our new material a harvest level of 4, which would allow it to harvest anything diamond can, plus some things it can't! (which is nothing in vanilla minecraft)

int maxUses indicates number of time the tool can be used before it breaks, also known as the durability. For reference vanilla durabilities are as follows
  • Gold 32
  • Wood 59
  • Stone 131
  • Iron 250
  • Diamond 1561
So our tools made from our material are less durable than iron, but moreso than stone.

float efficiency indicates the speed at which the tool mines, digs, swings, or whatever it does.
Vanilla efficiency levels are:
  • Wood 2.0
  • Stone 4.0
  • Iron 6.0
  • Diamond 8.0
  • Gold 12.0
Making copper as speedy as diamond!
float damage indicates how much damage is done to entities that are attacked with a tool made from this material, although it should be noted that certain tools, such as swords have a higher base damage.
Vanilla damage levels are:
  • Wood or Gold 0.0
  • Stone 1.0
  • Iron 2.0
  • Diamond 3.0
So copper tools are as effective as stone for dealing damage.

int enchantability indicates the natural enchantability factor for the material.
Vanilla enchantability levels are:
  • Wood 15
  • Stone 5
  • Iron 14
  • Diamond 10
  • Gold 22
This means our copper tools will take enchantments about as well as diamond tools.  All in all this makes copper a little better than iron for tools (except maybe swords) but significantly less durable, which I think balances it out some.

Now that we have a ToolMaterial, we need to use it on something!  I will start with a new pickaxe.  I do this by creating a new class in me.codasylph.grindermod.items.  I will name it CopperPickaxe and have it extend ItemPickaxe. My class looks like this:
package me.codasylph.grindermod.items;

import net.minecraft.item.ItemPickaxe;

public class CopperPickaxe extends ItemPickaxe
{

}

Right now I have an error because I need to write an explicit constructor, so I'll write one that calls the super constructor and passes my newly created material ModItems.COPPER, then I'll flush it out with the same sorts of things that I put in my CopperIngot item, an unlocalized name, a texture, and a creative tab to find it in.  Now my class looks like this:
package me.codasylph.grindermod.items;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemPickaxe;

public class CopperPickaxe extends ItemPickaxe
{
       private final String unlocalizedName = "copperPickaxe";

       public CopperPickaxe()
       {
              super(ModItems.COPPER);
              this.setUnlocalizedName(unlocalizedName);
              this.setTextureName("grindermod:"+unlocalizedName);
              this.setCreativeTab(CreativeTabs.tabTools);
       }

}
This is the texture I used: 

If anything in the above code doesn't make sense to you, you should probably refer to the A Basic Item tutorial.

Now, the item needs to be registered, this is done exactly the same way one would for any other item, with a call to GameRegistry.registerItem().  I will place this in my pre-existing init method of my ModItems class, so that class now looks like this:
package me.codasylph.grindermod.items;

import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.item.Item;
import net.minecraft.item.Item.ToolMaterial;
import net.minecraftforge.common.util.EnumHelper;

public class ModItems
{
       public static ToolMaterial COPPER = EnumHelper.addToolMaterial("COPPER", 2, 160, 8.0F, 1.0F, 10);
      
       public static Item copperIngot = new CopperIngot();
       public static Item copperPickaxe = new CopperPickaxe();
      
       public static void init()
       {
              GameRegistry.registerItem(copperIngot, copperIngot.getUnlocalizedName());
              GameRegistry.registerItem(copperPickaxe, copperPickaxe.getUnlocalizedName());
       }
}

You can use this procedure will all the different tool types, the classes to extend are as follows:
  • Pickaxe = ItemPickaxe
  • Axe = ItemAxe
  • Shovel = ItemSpade
  • Hoe = ItemHoe
  • Sword = ItemSword
And here are some unimpressive textures to play with:

Don't forget to update your language file!

To return to the table of contents click here.

No comments:

Post a Comment