shadowfacts.net/site/tutorials/forge-modding-112/tools.md

11 KiB

metadata.title = "Tools"
metadata.date = "2016-08-14 15:04:00 -0400"
metadata.series = "forge-modding-112"
metadata.seriesName = "Forge Mods for 1.12"

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

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 implement 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;

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

	private String name;

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

}

Next, we'll add our copper sword to our ModItems class.

// ...
public class ModItems {
	// ...
	public static ItemSword copperSword = new ItemSword(TutorialMod.copperToolMaterial, "copper_sword");
	
	public static void register(IForgeRegistry<Item> registry) {
		registry.registerAll(
				// ...
				copperSword
		);
	}

	public static void registerModels() {
		// ...
		copperSword.registerItemModel();
	}

}

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.

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 implement 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;

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

	private String name;

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

}

Next, we'll add our copper pickaxe to our ModItems class.

// ...
public class ModItems {
	// ...
	public static ItemPickaxe copperPickaxe = new ItemPickaxe(TutorialMod.copperToolMaterial, "copper_pickaxe");
	
	public static void register(IForgeRegistry<Item> registry) {
		registry.registerAll(
				// ...
				copperPickaxe
		);
	}
	
	public static void registerModels() {
		// ...
		copperPickaxe.registerItemModel();
	}

}

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.

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 implement 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;

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

	private String name;

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

}

Next, we'll add our copper axe to our ModItems class.

// ...
public class ModItems {
	// ...
	public static ItemAxe copperAxe = new ItemAxe(TutorialMod.copperToolMaterial, "copper_axe");
	
	public static void register(IForgeRegistry<Item> registry) {
		registry.registerAll(
				// ...
				copperAxe
		);
	}
	
	public static void registerModels() {
		// ...
		copperAxe.registerItemModel();
	}

}

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.

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;

public class ItemShovel extends ItemSpade {

	private String name;

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

}

Next, we'll add our copper shovel to our ModItems class.

// ...
public class ModItems {
	// ...
	public static ItemShovel copperShovel = new ItemShovel(TutorialMod.copperToolMaterial, "copper_shovel");
	
	public static void register(IForgeRegistry<Item> registry) {
		registry.registerAll(
				// ...
				copperShovel
		);
	}
	
	public static void registerModels() {
		// ...
		copperShovel.registerItemModel();
	}

}

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.

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;

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

	private String name;

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

}

Next, we'll add our hoe to our ModItems class.

// ...
public class ModItems {
	// ...
	public static ItemSword copperHoe = new ItemSword(TutorialMod.copperToolMaterial, "copper_hoe");
	
	public static void register(IForgeRegistry<Item> registry) {
		registry.registerAll(
				// ...
				copperHoe
		);
	}
	// ...
}

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