v6/site/tutorials/forge-modding-1112/tools.md

12 KiB

title = "Tools"
date = "2016-08-14 15:04:00 -0400"

Let's make some copper tools!

First we'll need to create a tool material for our new tools to use. We'll use Forge's EnumHelper class to add a value to the Minecraft Item.ToolMaterial enum.

// ...
public class TutorialMod {
	// ...
	public static final Item.ToolMaterial copperToolMaterial = EnumHelper.addToolMaterial("COPPER", 2, 500, 6, 2, 14);
	// ...
}

Each tool is going to be quite similar, so feel free to skip ahead to the one you want.

Sword

First we'll create an ItemSword class in the item.tool package inside our mod package. This class will extend the vanilla ItemSword class and implement our ItemModelProvider interface.

In the constructor, we'll:

  • Call the super constructor with the tool material
  • Set the unlocalized and registry names
  • Store the name for use in item model registration

We'll also override registerItemModel and use the stored name to register our item model.

package net.shadowfacts.tutorial.item.tool;

import net.minecraft.item.Item;
import net.shadowfacts.tutorial.TutorialMod;
import net.shadowfacts.tutorial.item.ItemModelProvider;

public class ItemSword extends net.minecraft.item.ItemSword implements ItemModelProvider {

	private String name;

	public ItemSword(ToolMaterial material, String name) {
		super(material);
		setRegistryName(name);
		setUnlocalizedName(name);
		this.name = name;
	}
	
	@Override
	public void registerItemModel(Item item) {
		TutorialMod.proxy.registerItemRenderer(this, 0, name);
	}

}

Next, we'll add our copper sword to our ModItems class simply by adding a field and initializing it using our register method.

// ...
public class ModItems {
	// ...
	public static ItemSword copperSword;
	
	public static void init() {
		// ...
		copperSword = register(new ItemSword(TutorialMod.copperToolMaterial, "copper_sword"));
	}
	// ...
}

We'll also create our JSON item model at assets/tutorial/models/item/copper_sword.json. Unlike our other item models, the parent for the model will be item/handheld instead of item/generated. item/handheld provides the transformations used by handheld items, such as tools.

{
	"parent": "item/handheld",
	"textures": {
		"layer0": "tutorial:items/copper_sword"
	}
}

We'll also need the texture, which you can download here.

And lastly, we'll add a localization entry for the sword.

# Items
# ...
item.copper_sword.name=Copper Sword

Copper Sword

Pickaxe

Let's create an ItemPickaxe class in the item.tool package of our mod. This class will extend the vanilla ItemPickaxe and implement our ItemModelProvider interface.

In our ItemPickaxe constructor we'll:

  • Call the super constructor with the tool material
  • Set the unlocalized name and registry names
  • Store the name for use in the item model registration

We'll also override registerItemModel and use the stored name field to register our item model.

package net.shadowfacts.tutorial.item.tool;

import net.minecraft.item.Item;
import net.shadowfacts.tutorial.TutorialMod;
import net.shadowfacts.tutorial.item.ItemModelProvider;

public class ItemPickaxe extends net.minecraft.item.ItemPickaxe implements ItemModelProvider {

	private String name;

	public ItemPickaxe(ToolMaterial material, String name) {
		super(material);
		setRegistryName(name);
		setUnlocalizedName(name);
		this.name = name;
	}
	
	@Override
	public void registerItemModel(Item item) {
		TutorialMod.proxy.registerItemRenderer(this, 0, name);
	}

}

Next, we'll add our copper pickaxe to our ModItems class simply by adding a field and initializing it using our register method.

// ...
public class ModItems {
	// ...
	public static ItemPickaxe copperPickaxe;
	
	public static void init() {
		// ...
		copperPickaxe = register(new ItemPickaxe(TutorialMod.copperToolMaterial, "copper_pickaxe"));
	}
	// ...
}

We'll create a JSON model for our item at assets/tutorial/models/item/copper_pickaxe.json. This model will have a parent of item/handheld instead of item/generated so it inherits the transformations for handheld models.

{
	"parent": "item/handheld",
	"textures": {
		"layer0": "tutorial:items/copper_pickaxe"
	}
}

You can download the texture for the copper pickaxe here.

Lastly, we'll need a localization entry for the pick.

# Items
# ...
item.copper_pickaxe.name=Copper Pickaxe

Copper Pickaxe

Axe

First off, we'll need an ItemAxe class that extends the vanilla ItemAxe class and implements our ItemModelProvider interface.

If you look at the vanilla ItemAxe class, you'll notice that it has two constructors. One of them takes only a ToolMaterial whereas the other takes a ToolMaterial and two floats. Only vanilla ToolMaterials will work with the ToolMaterial only constructor, any modded materials will cause an ArrayIndexOutOfBoundsException because of the hardcoded values in the float arrays in the ItemAxe class. Forge provides the secondary constructor that accepts the two floats as well, allowing modders to add axes with their own tool materials.

In the ItemPickaxe constructor, we will:

  • Call the super constructor with the tool material and the damage and attack speeds used by the vanilla iron axe.
  • Set the unlocalized and registry names
  • Store the name for use in the item model registration

Additionally, we'll override registerItemModel and use the stored name to register our model.

package net.shadowfacts.tutorial.item.tool;

import net.minecraft.item.Item;
import net.shadowfacts.tutorial.TutorialMod;
import net.shadowfacts.tutorial.item.ItemModelProvider;

public class ItemAxe extends net.minecraft.item.ItemAxe implements ItemModelProvider {

	private String name;

	public ItemAxe(ToolMaterial material, String name) {
		super(material, 8f, -3.1f);
		setRegistryName(name);
		setUnlocalizedName(name);
		this.name = name;
	}
	
	@Override
	public void registerItemModel(Item item) {
		TutorialMod.proxy.registerItemRenderer(this, 0, name);
	}

}

Next, we'll add our copper axe to our ModItems class simply by adding a field and initializing it using our register method.

// ...
public class ModItems {
	// ...
	public static ItemAxe copperAxe;
	
	public static void init() {
		// ...
		copperAxe = register(new ItemAxe(TutorialMod.copperToolMaterial, "copper_axe"));
	}
	// ...
}

Additionally, we'll need a JSON item model. We'll create it at assets/tutorials/models/item/copper_axe.json.

Our model will have a parent of item/handheld instead of item/generated so it has the same transformations used by other hand-held items.

{
	"parent": "item/handheld",
	"textures": {
		"layer0": "tutorial:items/copper_axe"
	}
}

You can download the texture for the copper axe here.

Lastly, we'll need a localization entry for our axe.

# Items
# ...
item.copper_axe.name=Copper Axe

Copper Axe

Shovel

Firstly we'll create an ItemShovel class that extends the vanilla ItemSpade class and implements our ItemModelProvider interface.

In the ItemShovel constructor, we'll:

  • Call the super constructor with the tool material used
  • Set the unlocalized and registry names
  • Store the name to be used for item model registration

We'll also need to implement registerItemModel and register a item model for our shovel.

package net.shadowfacts.tutorial.item.tool;

import net.minecraft.item.Item;
import net.minecraft.item.ItemSpade;
import net.shadowfacts.tutorial.TutorialMod;
import net.shadowfacts.tutorial.item.ItemModelProvider;

public class ItemShovel extends ItemSpade implements ItemModelProvider {

	private String name;

	public ItemShovel(ToolMaterial material, String name) {
		super(material);
		setRegistryName(name);
		setUnlocalizedName(name);
		this.name = name;
	}
	
	@Override
	public void registerItemModel(Item item) {
		TutorialMod.proxy.registerItemRenderer(this, 0, name);
	}

}

Next, we'll add our copper shovel to our ModItems class simply by adding a field and initializing it using our register method.

// ...
public class ModItems {
	// ...
	public static ItemShovel copperShovel;
	
	public static void init() {
		// ...
		copperShovel = register(new ItemShovel(TutorialMod.copperToolMaterial, "copper_shovel"));
	}
	// ...
}

Next, we'll create a JSON item model for our shovel at assets/tutorial/models/item/copper_shovel.json. This model will have a parent of item/handheld, unlike our previous item models, so it inherits the transformations used by handheld items.

{
	"parent": "item/handheld",
	"textures": {
		"layer0": "tutorial:items/copper_shovel"
	}
}

You can download the texture for our shovel here.

We'll also need a localization entry for our shovel.

# Items
# ...
item.copper_shovel.name=Copper Shovel

Copper Shovel

Hoe

Let's create an ItemHoe class that extends the vanilla ItemHoe class and implements our ItemModelProvider interface.

In the ItemHoe constructor, we'll:

  • Call the super constructor with the tool material
  • Set the unlocalized and registry names
  • Store the item name to be used for item model registration

We'll also need to implement registerItemModel and register an item model for our hoe.

package net.shadowfacts.tutorial.item.tool;

import net.minecraft.item.Item;
import net.shadowfacts.tutorial.TutorialMod;
import net.shadowfacts.tutorial.item.ItemModelProvider;

public class ItemHoe extends net.minecraft.item.ItemHoe implements ItemModelProvider {

	private String name;

	public ItemHoe(ToolMaterial material, String name) {
		super(material);
		setRegistryName(name);
		setUnlocalizedName(name);
		this.name = name;
	}
	
	@Override
	public void registerItemModel(Item item) {
		TutorialMod.proxy.registerItemRenderer(this, 0, name);
	}

}

Next, we'll add our hoe to our ModItems class simply by adding a field and initializing it using our register method.

// ...
public class ModItems {
	// ...
	public static ItemSword copperHoe;
	
	public static void init() {
		// ...
		copperHoe = register(new ItemSword(TutorialMod.copperToolMaterial, "copper_hoe"));
	}
	// ...
}

Next, we'll create a JSON item model for our hoe at assets/tutorial/models/item/copper_hoe.json. This model will have a parent of item/handheld instead of item/generated so it inherits the transformations used by vanilla handheld items.

{
	"parent": "item/handheld",
	"textures": {
		"layer0": "tutorial:items/copper_hoe"
	}
}

You can download the copper hoe texture here.

Lastly, we'll need a localization entry for our hoe.

# Items
# ...
item.copper_hoe.name=Copper Hoe

Copper Hoe