User Tools

Site Tools


Sidebar

Setup

Basics

Items

Blocks and Block Entities

Fluids

Entities

World Generation

Miscellaneous

Events

Mixins

Advanced

Tutorials for Minecraft 1.15

Tutorials for Minecraft 1.14

Documentation

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.

Creating Tools

All base tool classes (PickaxeItem, ShovelItem, HoeItem, AxeItem, SwordItem) require a ToolMaterial, an attack speed (float), an additional attack damage amount (int), and an Item.Settings instance.

  1. public static ToolItem POTATO_SHOVEL = new ShovelItem(PotatoToolMaterial.INSTANCE, 1.5F, -3.0F, new Item.Settings().group(ItemGroup.TOOLS));
  2. public static ToolItem POTATO_SWORD = new SwordItem(PotatoToolMaterial.INSTANCE, 3, -2.4F, new Item.Settings().group(ItemGroup.COMBAT));

`PickaxeItem` , `HoeItem` and `AxeItem` have protected constructors, which means you will need to create your own sub-class with a public constructor:

  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 custom subclass:

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

If you want to add any special attributes or behaviors to your tool, create a subclass that extends one of the base tool classes, and override any required methods.

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: 2020/09/14 01:50 by draylar