Skip to content

Instantly share code, notes, and snippets.

@BoogieMonster1O1
Last active September 30, 2020 07:05
Show Gist options
  • Save BoogieMonster1O1/4f860cdadcf6e8de1691bb3dcb1fe18d to your computer and use it in GitHub Desktop.
Save BoogieMonster1O1/4f860cdadcf6e8de1691bb3dcb1fe18d to your computer and use it in GitHub Desktop.

Adding Tools

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. Creating a tool is very similar to creating armor.

public enum CustomToolMaterial implements ArmorMaterial {;
    CustomToolMaterial(int miningLevel, int itemDurability, float miningSpeed, 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. 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.

public enum CustomToolMaterial implements ArmorMaterial {;
    private final int miningLevel;
    private final int itemDurability;
    private final float miningSpeed;
    private final float attackDamage;
    private final int enchantability;
    private final Lazy<Ingredient> repairIngredient;

    CustomToolMaterial(int miningLevel, int itemDurability, float miningSpeed, float attackDamage, int enchantability, Supplier<Ingredient> repairIngredient) {
        this.miningLevel = miningLevel;
        this.itemDurability = itemDurability;
        this.miningSpeed = miningSpeed;
        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 miningSpeed;
    private final float attackDamage;
    private final int enchantability;
    private final Lazy<Ingredient> repairIngredient;

    CustomToolMaterial(int miningLevel, int itemDurability, float miningSpeed, float attackDamage, int enchantability, Supplier<Ingredient> repairIngredient) {
        this.miningLevel = miningLevel;
        this.itemDurability = itemDurability;
        this.miningSpeed = miningSpeed;
        this.attackDamage = attackDamage;
        this.enchantability = enchantability;
        this.repairIngredient = new Lazy<>(repairIngredient);
    }

    @Override
    public int getDurability() {
        return this.itemDurability;
    }

    @Override
    public float getMiningSpeed() {
        return this.miningSpeed;
    }

    @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, () -> Ingredient.ofItems(Items.POTATO));
    
    [...]
}

Registering the tools

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

public class PickaxeSubclass extends PickaxeItem {
    public PickaxeSubclass(ToolMaterial material, int attackDamage, float attackSpeed, Settings settings) {
        super(material, attackDamage, attackSpeed, settings);
    }
}

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 and texturing a tool is done the exact same way as an item. After creating an object for each tool, register them in your mod initializer class just like how you would with a regular item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment