User Tools

Site Tools


tutorial:tools

Adding Tools

Creating a Tool Material

Tools require a ToolMaterial, which defines the following behavior:

  • durability
  • mining speed
  • attack damage
  • mining level
  • enchantability
  • repair ingredient

In other words, Tool Materials defines the base functionality for tools of that type, and tools can choose to use the values provided by the material, or roll with their own.

Vanilla Tool Materials can be found in ToolMaterials. We will create a separate class for our material:

  1. public class PotatoToolMaterial implements ToolMaterial {
  2.  
  3. [...]
  4. }

ToolMaterial has a number of methods you will need to implement:

Durability

getDurability defines the base durability tools will have when they use this material. In vanilla, all tools of the same type have the same durability.

  1. @Override
  2. public int getDurability() {
  3. return 500;
  4. }

Mining Speed

getMiningSpeedMultiplier defines how fast tools are when mining blocks. For a general sense of scale, Wooden has a speed of 2.0F, and Diamond has a speed of 8.0F.

  1. @Override
  2. public float getMiningSpeedMultiplier() {
  3. return 5.0F;
  4. }

Attack Damage

getAttackDamage returns the base damage of the tool. Note that most tools ask for an integer damage amount in their constructor, which means the resulting damage is (float) materialDamage + (int) toolDamage + 1. If you want your tool to entirely control its damage amount in its constructor, you can make your material return an attack damage of 0F.

  1. @Override
  2. public float getAttackDamage() {
  3. return 3.0F;
  4. }

Mining Level

getMiningLevel sets the mining level of a tool. Diamond has a mining level of 3, and a value of 3+ is required to mine Obsidian.

  1. @Override
  2. public int getMiningLevel() {
  3. return 2;
  4. }

Enchantability

getEnchantability defines how enchantable a tool is. Gold comes in at 22 Enchatability, while Diamond sits at 10. Higher enchantability means better (and higher-level) enchantments.

  1. @Override
  2. public int getEnchantability() {
  3. return 15;
  4. }

Repair Ingredient

getRepairIngredient returns the Ingredient required to repair a tool in an anvil.

  1. @Override
  2. public Ingredient getRepairIngredient() {
  3. return Ingredient.ofItems(Items.POTATO);
  4. }

ToolMaterials do not have to be registered. A good way to pass them out to tools that require them is by keeping an instance somewhere (and then referencing it when you need it). In this case, we will put our instance at the top of the Tool Material class:

  1. public class PotatoToolMaterial implements ToolMaterial {
  2.  
  3. public static final PotatoToolMaterial INSTANCE = new PotatoToolMaterial();
  4.  
  5. [...]
  6. }

PotatoToolMaterial can now be referenced with PotatoToolMaterial.INSTANCE. Alternatively, you can implement an enum similar to vanilla's ToolMaterials class that implements ToolMaterial, which provides the instance and also allows easy creation of multiple tool materials.

Creating Tools

In newer versions, all base tool class constructors are public and can be used directly to register the item. This constructor lets you specify attack damage and attack speed of the tool.

public static ToolItem POTATO_PICKAXE = new PickaxeItem(PotatoToolMaterial.INSTANCE, 1, -2.8F, new FabricItemSettings());
public static ToolItem POTATO_AXE = new AxeItem(PotatoToolMaterial.INSTANCE, 7.0F, -3.2F, new FabricItemSettings());
public static ToolItem POTATO_HOE = new HoeItem(PotatoToolMaterial.INSTANCE, 7, -3.2F, new FabricItemSettings());

Creating Tool Subclasses

This section is not necessary in the current version of Fabric. This is a good way to implement special attributes or behaviors for your tool, however.

In older versions, all base tool classes (PickaxeItem, ShovelItem, HoeItem, AxeItem, SwordItem) require a ToolMaterial, an attack speed (float), an additional attack damage amount (float for AxeItem; int for the rest), and an Item.Settings instance.

PickaxeItem , HoeItem and AxeItem have protected constructors, which means you will need to create your own classes with public constructors:

  1. public class CustomPickaxeItem extends PickaxeItem {
  2. public CustomPickaxeItem(ToolMaterial material, int attackDamage, float attackSpeed, Settings settings) {
  3. super(material, attackDamage, attackSpeed, settings);
  4. }
  5. }

Using the public classes:

public static ToolItem POTATO_PICKAXE = new CustomPickaxeItem(PotatoToolMaterial.INSTANCE, 1, -2.8F, new Item.Settings());
public static ToolItem POTATO_AXE = new CustomAxeItem(PotatoToolMaterial.INSTANCE, 7.0F, -3.2F, new Item.Settings());
public static ToolItem POTATO_HOE = new CustomHoeItem(PotatoToolMaterial.INSTANCE, 7, -3.2F, new Item.Settings());

Registering Tools

For a recap on registering items, read through the item tutorial here.

Making your tool work with non-vanilla blocks

tutorial/tools.txt · Last modified: 2023/09/07 05:32 by drakonkinst