Tool Durability

experimental

intermediate

scripting

PLEASE READ

This page will be part of a rewrite to accomodate for the removal of the Holiday Creator Feature experimental toggle. Expect this page to be rewritten or removed when this happens.

Introduction

1.16.100+ items have different durability mechanic than 1.10 and 1.16 items. Now you need to define when will the item get durability damage and also an event that does it. What will be discussed on this page:

  • Durability component
  • Event that updates durability
  • Damaging entities
  • Block breaking
  • repair_amount value
  • on_tool_used event

Components

BP/items/my_item.json#componentsCopy
json
"minecraft:durability": {
    "max_durability": 200
}
1
2
3

minecraft:durability will give your item a set max durability

Event

Item event

BP/items/my_item.json#eventsCopy
json
"durability_update": {
    "damage": {
        "type": "none",
        "amount": 1,
        "target": "self"
    }
}
1
2
3
4
5
6
7

When this event is called the item (self target) will receive durability damage. Looks simple, doesn't it?

Script event

For the script methods, we'll be using a function to damage our item

This function supports unbreaking on items

BP/scripts/main.jsCopy
js
function damage_item(item) {
    // Get durability
    const durabilityComponent = item.getComponent("durability")
    var unbreaking = 0
    // Get unbreaking level
    if (item.hasComponent("enchantments")) {
        unbreaking = item.getComponent("enchantments").enchantments.getEnchantment("unbreaking")
        if (!unbreaking) {
            unbreaking = 0
        } else {
            unbreaking = unbreaking.level
        }
    }
    // Apply damage
    if (durabilityComponent.damage == durabilityComponent.maxDurability) {

        return
    }
    durabilityComponent.damage += Number(Math.round(Math.random() * 100) <= durabilityComponent.getDamageChance(unbreaking))
    return item
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Damaging entities

Using scripts

Experimental Script

This script uses @minecraft/server 1.9.0-beta, which will change in the next minecraft update.

For format versions 1.20.40 and onward, on_hurt_entity no longer works.

This provides a way to damage weapons using scripts

BP/scripts/main.jsCopy
js
// Add your item IDs into this array
const my_items = ["wiki:silver_dagger"]

world.afterEvents.entityHurt.subscribe(event => {
    // If there's no source entity, skip
    if (!event.damageSource.damagingEntity) return

    // Get equipped weapon
    const equipment = event.damageSource.damagingEntity.getComponent("minecraft:equippable")
    if (!equipment) return
    const weapon = equipment.getEquipment(EquipmentSlot.Mainhand)

    // If there's no weapon, skip
    if (!weapon) return

    // If the item is not in our item IDs, skip
    if (!my_items.includes(weapon.typeId)) return
    let newItem = damage_item(weapon)
    equipment.setEquipment(EquipmentSlot.Mainhand, newItem)
    if (!newItem) {
        if (event.damageSource.damagingEntity instanceof Player) {
            event.damageSource.damagingEntity.playSound("random.break")
        }
    }
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

on_hurt_entity

WARNING

on_hurt_entity was removed in format version 1.20.40

on_hurt_entity can be defined in "minecraft:weapon" component. It tells the game what event should happen when player hurts entity using this item.

BP/items/my_item.json#componentsCopy
json
"minecraft:weapon": {
    "on_hurt_entity": {
        "event": "durability_update"
    }
}
1
2
3
4
5

Block breaking

Using scripts

Experimental Script

This script uses @minecraft/server 1.9.0-beta, which will change in the next minecraft update.

For format versions 1.20.20 and onward, on_dig no longer works.

This provides a way to damage digger items by using scripts

BP/scripts/main.jsCopy
js
// Add your item IDs into this array
const my_items = ["wiki:obsidian_pickaxe"]

world.afterEvents.playerBreakBlock.subscribe(event => {
    // If there's no item, skip
    if (!event.itemStackAfterBreak) return
    // If the item is not in our item IDs, skip
    if (!my_items.includes(event.itemStackAfterBreak.typeId)) return

    // If player is in creative, skip
    if (world.getPlayers({
        gameMode: GameMode.creative
    }).includes(event.player)) return
    const newItem = damage_item(event.itemStackAfterBreak)
    event.player.getComponent("minecraft:equippable").setEquipment(EquipmentSlot.Mainhand, newItem)
    if (!newItem) {
        event.player.playSound("random.break")
    }
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

on_dig

WARNING

on_dig was removed in format version 1.20.20

on_dig can be defined in "minecraft:digger" component. It tells the game what event should happen when player dug a block using this item.

BP/items/my_item.json#componentsCopy
json
"minecraft:digger": {
    "use_efficiency": true,
    "destroy_speeds": [
        {
            "block": {
                "tags": "q.any_tag('wood')"
            },
            "speed": 8,
            "on_dig": {
                // Defines event that should happen when block with tag wood was dug.
                "event": "durability_update"
            }
        }
    ],
    "on_dig": {
        // Defines event that should happen when any block was destroyed.
        "event": "durability_update"
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

repair_amount

repair_amount can be defined in "minecraft:repairable" component. It tells the game how much of the item's durability should be back when it was repaired.

BP/items/my_item.json#componentsCopy
json
"minecraft:repairable": {
    "repair_items": [
        {
            "repair_amount": "context.other->q.remaining_durability + 0.05 * context.other->q.max_durability",
            "items": [
                "bs:silver",
                "bs:silver_axe"
            ]
        }
    ]
}
1
2
3
4
5
6
7
8
9
10
11

Formula explanation:

"context.other->q.remaining_durability + 0.05 * context.other->q.max_durability"

The final durability will be durability of the first axe + durability of the second axe + 5% of 2nd axe MAX durability.

on_tool_used

(This might not work now) on_tool_used is special event that can be called using tags. Tags work kinda like runtime identifiers for entities. Known tags:

TagEffectsHow can be called
minecraft:is_axeStrips logsBy interacting with blocks that axe interacts with
minecraft:is_hoeMakes farmlandBy interacting with blocks that hoe interacts with
minecraft:is_pickaxeUnknownUnknown
minecraft:is_swordUnknownUnknown

You can apply these tags this way:

BP/items/my_item.json#componentsCopy
json
"tag:minecraft:is_axe": {}
1

Contributors