This is an old revision of the document!
Table of Contents
Adding Tools
Introduction
Creating a tool is very similar to making armor. It's a bit more complicated than making an item, but it's easy to make multiple of them
Creating a Tool Material class
The first step to creating a tool is to implement the `ToolMaterial` interface in a class
It's best to make the class as an enum, as it doesnt require you to create an object, like regular class, and enum constants make adding multiple tool materials easier.
public enum CustomToolMaterial implements ToolMaterial {; CustomToolMaterial(int miningLevel, int itemDurability, float miningSpeedMultiplier, float attackDamage, int enchantability, Supplier<Ingredient> repairIngredient) { } }
What each argument does:- 1. `miningLevel` refers to the strength of the tool necessary to mine any material. Wood is 1, Stone is 2, Iron is 3, Diamond is 4. 2. `itemDurability` refers to the initial durability of a tool. Gold is 32, Iron is 250, Netherite is 2031. 3. `miningSpeedMultiplier ` refers to how fast your tools can break blocks. Gold is 12.0f, Diamond is 8.0f, Iron is 6.0f. 4. `attackDamage` refers to the melee damage that your tools perform. Wood is 0.0f, Stone is 1.0f, Diamond is 3.0f 5. `enchantability` refers to the chance of getting high-level enchantments on your tools. Wood is 15, Stone is 5, Gold is 22, Iron is 14. 6. `repairIngredient` refers to the item that can repair your tools in an anvil. This will not be stored in an `Ingredient`, but in a `Lazy<Ingredient>`, which requires a `Supplier` in the constructor.
To make these values accessible from outside the constructor, create a final field for each of them and assign their values in the constructor.
public enum CustomToolMaterial implements ToolMaterial {; private final int miningLevel; private final int itemDurability; private final float miningSpeedMultiplier; private final float attackDamage; private final int enchantability; private final Lazy<Ingredient> repairIngredient; CustomToolMaterial(int miningLevel, int itemDurability, float miningSpeedMultiplier, float attackDamage, int enchantability, Supplier<Ingredient> repairIngredient) { this.miningLevel = miningLevel; this.itemDurability = itemDurability; this. miningSpeedMultiplier = miningSpeedMultiplier; this.attackDamage = attackDamage; this.enchantability = enchantability; this.repairIngredient = new Lazy<>(repairIngredient); } }
Now its time to implement the methods from the `ToolMaterial` interface. You should have something like this. Change the return value of each implemented method to the corresponding field.
public enum CustomToolMaterial implements ToolMaterial {; private final int miningLevel; private final int itemDurability; private final float miningSpeedMultiplier; private final float attackDamage; private final int enchantability; private final Lazy<Ingredient> repairIngredient; CustomToolMaterial(int miningLevel, int itemDurability, float miningSpeedMultiplier, float attackDamage, int enchantability, Supplier<Ingredient> repairIngredient) { this.miningLevel = miningLevel; this.itemDurability = itemDurability; this.miningSpeedMultiplier = miningSpeedMultiplier; this.attackDamage = attackDamage; this.enchantability = enchantability; this.repairIngredient = new Lazy<>(repairIngredient); } @Override public int getDurability() { return this.itemDurability; } @Override public float getMiningSpeedMultiplier() { return this.miningSpeedMultiplier; } @Override public float getAttackDamage() { return this.attackDamage; } @Override public int getMiningLevel() { return this.miningLevel; } @Override public int getEnchantability() { return this.enchantability; } @Override public Ingredient getRepairIngredient() { return this.repairIngredient.get(); } }
Next, create an enum constant. You can create multiple enum constants if you need multiple tool materials. This enum constant is for potato tools.
public enum CustomToolMaterial implements ToolMaterial { POTATO(1, 167, 4.8F, 1.1F, 11, () -> { return Ingredient.ofItems(Items.POTATO); }); [...] }
Creating the tool objects
Swords, shovels, pickaxes and axes take in four arguments : The Tool Material, The Attack Damage, The Attack Speed and Item Settings
public static ToolItem POTATO_SHOVEL = new ShovelItem(CustomToolMaterial.POTATO, 1.5F, -3.0F, new Item.Settings().group(ItemGroup.TOOLS)); public static ToolItem POTATO_SWORD = new SwordItem(CustomToolMaterial.POTATO, 3, -2.4F, new Item.Settings().group(ItemGroup.COMBAT));
Unfortunately, `PickaxeItem` , `HoeItem` and `AxeItem` only have protected constructors, so you'll have to make classes that extends each of them. Creating a subclass makes making multiple pickaxes or axes easier.
public class PickaxeSubclass extends PickaxeItem { public PickaxeSubclass(ToolMaterial material, int attackDamage, float attackSpeed, Settings settings) { super(material, attackDamage, attackSpeed, settings); } }
To make a pickaxe, hoe and axe, create objects of the subclasses.
public static ToolItem POTATO_PICKAXE = new PickaxeSubclass(CustomToolMaterial.POTATO, 1, -2.8F, new Item.Settings().group(ItemGroup.TOOLS)); public static ToolItem POTATO_AXE = new AxeSubclass(CustomToolMaterial.POTATO, 7.0F, -3.2F, new Item.Settings().group(ItemGroup.TOOLS)); public static ToolItem POTATO_HOE = new HoeSubclass(CustomToolMaterial.POTATO, 7, -3.2F, new Item.Settings().group(ItemGroup.TOOLS));
If you want to add any special attributes or behavior to your tool, create a class that extends one of the tool items, and override any required methods.
Registering Tools
Registering tools is done the same way you would register a normal item.
[...] @Override public void onInitialize() { }
Making your tool work with non-vanilla blocks
Visit the last section of https://fabricmc.net/wiki/tutorial:mining_levels