User Tools

Site Tools


Sidebar

Setup

Basics

Items

Blocks and Block Entities

Fluids

Entities

World Generation

Miscellaneous

Events

Mixins

Advanced

Tutorials for Minecraft 1.14

Documentation

tutorial:tools

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 a regular class, and enum constants make adding tool materials easier.

  1. public enum CustomToolMaterial implements ToolMaterial {;
  2. CustomToolMaterial(int miningLevel, int itemDurability, float miningSpeed, float attackDamage, int enchantability, Supplier<Ingredient> repairIngredient) {
  3.  
  4. }
  5. }

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. `miningSpeed` 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.

  1. public enum CustomToolMaterial implements ToolMaterial {;
  2. private final int miningLevel;
  3. private final int itemDurability;
  4. private final float miningSpeed;
  5. private final float attackDamage;
  6. private final int enchantability;
  7. private final Lazy<Ingredient> repairIngredient;
  8.  
  9. CustomToolMaterial(int miningLevel, int itemDurability, float miningSpeed, float attackDamage, int enchantability, Supplier<Ingredient> repairIngredient) {
  10. this.miningLevel = miningLevel;
  11. this.itemDurability = itemDurability;
  12. this.miningSpeed = miningSpeed;
  13. this.attackDamage = attackDamage;
  14. this.enchantability = enchantability;
  15. this.repairIngredient = new Lazy<>(repairIngredient);
  16. }
  17. }

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.

  1. public enum CustomToolMaterial implements ToolMaterial {;
  2. private final int miningLevel;
  3. private final int itemDurability;
  4. private final float miningSpeed;
  5. private final float attackDamage;
  6. private final int enchantability;
  7. private final Lazy<Ingredient> repairIngredient;
  8.  
  9. CustomToolMaterial(int miningLevel, int itemDurability, float miningSpeed, float attackDamage, int enchantability, Supplier<Ingredient> repairIngredient) {
  10. this.miningLevel = miningLevel;
  11. this.itemDurability = itemDurability;
  12. this.miningSpeed = miningSpeed;
  13. this.attackDamage = attackDamage;
  14. this.enchantability = enchantability;
  15. this.repairIngredient = new Lazy<>(repairIngredient);
  16. }
  17.  
  18. @Override
  19. public int getDurability() {
  20. return this.itemDurability;
  21. }
  22.  
  23. @Override
  24. public float getMiningSpeed() {
  25. return this.miningSpeed;
  26. }
  27.  
  28. @Override
  29. public float getAttackDamage() {
  30. return this.attackDamage;
  31. }
  32.  
  33. @Override
  34. public int getMiningLevel() {
  35. return this.miningLevel;
  36. }
  37.  
  38. @Override
  39. public int getEnchantability() {
  40. return this.enchantability;
  41. }
  42.  
  43. @Override
  44. public Ingredient getRepairIngredient() {
  45. return this.repairIngredient.get();
  46. }
  47. }

Next, create an enum constant. You can create multiple enum constants if you need multiple tool materials. This enum constant is for potato tools.

  1. public enum CustomToolMaterial implements ToolMaterial {
  2. POTATO(1, 167, 4.8F, 1.1F, 11, () -> Ingredient.ofItems(Items.POTATO));
  3.  
  4. [...]
  5. }

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

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

Unfortunately, `PickaxeItem` and `AxeItem` only have a protected constructor, so you'll have to make classes that extends each of them. Creating a subclass makes making multiple pickaxes or axes easier.

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

To make a pickaxe and axe, create objects of the subclasses.

public static ToolItem POTATO_PICKAXE = new PickaxeSubclass(CustomToolMaterial.POTATO, 1, -2.8F, ItemGroup.TOOLS);
public static ToolItem POTATO_AXE = new AxeSubclass(CustomToolMaterial.POTATO, 7.0F, -3.2F, 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.

  1. [...]
  2.  
  3. @Override
  4. public void onInitialize() {
  5. Registry.register(Registry.ITEM,new Identifier("tutorial","potato_pickaxe"), POTATO_PICKAXE);
  6. Registry.register(Registry.ITEM,new Identifier("tutorial","potato_axe"), POTATO_AXE);
  7. Registry.register(Registry.ITEM,new Identifier("tutorial","potato_sword"), POTATO_SWORD);
  8. Registry.register(Registry.ITEM,new Identifier("tutorial","potato_hoe"), POTATO_HOE);
  9. Registry.register(Registry.ITEM,new Identifier("tutorial","potato_shovel"), POTATO_SHOVEL);
  10. }
tutorial/tools.txt · Last modified: 2020/06/09 06:13 by boogiemonster1o1