User Tools

Site Tools


tutorial:tools

This is an old revision of the document!


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.

  1. public enum CustomToolMaterial implements ToolMaterial {;
  2. CustomToolMaterial(int miningLevel, int itemDurability, float miningSpeedMultiplier, 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. `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.

  1. public enum CustomToolMaterial implements ToolMaterial {;
  2. private final int miningLevel;
  3. private final int itemDurability;
  4. private final float miningSpeedMultiplier;
  5. private final float attackDamage;
  6. private final int enchantability;
  7. private final Lazy<Ingredient> repairIngredient;
  8.  
  9. CustomToolMaterial(int miningLevel, int itemDurability, float miningSpeedMultiplier, float attackDamage, int enchantability, Supplier<Ingredient> repairIngredient) {
  10. this.miningLevel = miningLevel;
  11. this.itemDurability = itemDurability;
  12. this. miningSpeedMultiplier = miningSpeedMultiplier;
  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 miningSpeedMultiplier;
  5. private final float attackDamage;
  6. private final int enchantability;
  7. private final Lazy<Ingredient> repairIngredient;
  8.  
  9. CustomToolMaterial(int miningLevel, int itemDurability, float miningSpeedMultiplier, float attackDamage, int enchantability, Supplier<Ingredient> repairIngredient) {
  10. this.miningLevel = miningLevel;
  11. this.itemDurability = itemDurability;
  12. this.miningSpeedMultiplier = miningSpeedMultiplier;
  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 getMiningSpeedMultiplier() {
  25. return this.miningSpeedMultiplier;
  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, () -> {
  3. return Ingredient.ofItems(Items.POTATO);
  4. });
  5.  
  6. [...]
  7. }

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

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.

  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, 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.

  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. }

Making your tool work with non-vanilla blocks

tutorial/tools.1596773239.txt.gz · Last modified: 2020/08/07 04:07 by boogiemonster1o1