Contains a set of events that will be raised for an item. This object must be bound using the ItemComponentRegistry.

import { world } from "@minecraft/server";

world.beforeEvents.worldInitialize.subscribe((initEvent) => {
initEvent.itemComponentRegistry.registerCustomComponent("custom:item", {
onBeforeDurabilityDamage(event) {
const { attackingEntity, durabilityDamage, hitEntity, itemStack } =
event;
// Your code here
},
onCompleteUse(event) {
const { itemStack, source } = event;
// Your code here
},
onConsume(event) {
const { itemStack, source } = event;
// Your code here
},
onHitEntity(event) {
const { attackingEntity, hadEffect, hitEntity, itemStack } = event;
// Your code here
},
onMineBlock(event) {
const { block, itemStack, minedBlockPermutation, source } = event;
// Your code here
},
onUse(event) {
const { itemStack, source } = event;
// Your code here
},
onUseOn(event) {
const { source, usedOnBlockPermutation } = event;
// Your code here
},
});
});
interface ItemCustomComponent {
    onBeforeDurabilityDamage?: (
        arg: ItemComponentBeforeDurabilityDamageEvent,
    ) => void;
    onCompleteUse?: (arg: ItemComponentCompleteUseEvent) => void;
    onConsume?: (arg: ItemComponentConsumeEvent) => void;
    onHitEntity?: (arg: ItemComponentHitEntityEvent) => void;
    onMineBlock?: (arg: ItemComponentMineBlockEvent) => void;
    onUse?: (arg: ItemComponentUseEvent) => void;
    onUseOn?: (arg: ItemComponentUseOnEvent) => void;
}

Properties

onBeforeDurabilityDamage?: (
    arg: ItemComponentBeforeDurabilityDamageEvent,
) => void
onCompleteUse?: (arg: ItemComponentCompleteUseEvent) => void
onConsume?: (arg: ItemComponentConsumeEvent) => void
onHitEntity?: (arg: ItemComponentHitEntityEvent) => void
onMineBlock?: (arg: ItemComponentMineBlockEvent) => void

This function will be called when an item containing this component is used to mine a block.

onUse?: (arg: ItemComponentUseEvent) => void

This function will be called when an item containing this component is used by a player.

onUseOn?: (arg: ItemComponentUseOnEvent) => void