Contains a set of events that will be raised for an item.
This object must be bound using the ItemComponentRegistry.
Example: quickstart.js
import { system } from"@minecraft/server";
// Register custom item components before the world finishes loading. system.beforeEvents.startup.subscribe(({ itemComponentRegistry }) => { itemComponentRegistry.registerCustomComponent("custom:starter_item_logic", { // Runs before durability damage is applied when hitting an entity. onBeforeDurabilityDamage(event) { const { attackingEntity, hitEntity, itemStack } = event;
// Example beginner behavior: make the item unbreakable. event.durabilityDamage = 0;
console.warn( `[ItemCustomComponent] ${attackingEntity?.typeId} hit ${hitEntity?.typeId} with ${itemStack?.typeId}.` ); },
// Runs when the item is used by a player. onUse(event) { const { source, itemStack } = event; console.warn(`[ItemCustomComponent] ${source?.typeId} used ${itemStack?.typeId}.`); },
// Runs when the item is used on a block. onUseOn(event) { const { source, usedOnBlockPermutation } = event; console.warn(`[ItemCustomComponent] ${source?.typeId} used item on ${usedOnBlockPermutation?.type.id}.`); },
// Runs when the item mines a block. onMineBlock(event) { const { source, block, minedBlockPermutation } = event; console.warn( `[ItemCustomComponent] ${source?.typeId} mined ${block?.typeId} and broke ${minedBlockPermutation?.type.id}.` ); },
// Runs when the item hits an entity. onHitEntity(event) { const { attackingEntity, hitEntity, hadEffect } = event; console.warn( `[ItemCustomComponent] ${attackingEntity?.typeId} hit ${hitEntity?.typeId}. hadEffect=${hadEffect}` ); },
// Requires minecraft:use_modifiers on the item. onCompleteUse(event) { const { source, itemStack } = event; console.warn(`[ItemCustomComponent] ${source?.typeId} completed use on ${itemStack?.typeId}.`); },
// Requires minecraft:use_modifiers and minecraft:food on the item. onConsume(event) { const { source, itemStack } = event; console.warn(`[ItemCustomComponent] ${source?.typeId} consumed ${itemStack?.typeId}.`); }, }); });
// Register custom item components before the world finishes loading. system.beforeEvents.startup.subscribe(({ itemComponentRegistry }) => { itemComponentRegistry.registerCustomComponent("custom:starter_item_logic", { // Runs before durability damage is applied when hitting an entity. onBeforeDurabilityDamage(event: ItemComponentBeforeDurabilityDamageEvent, _params: CustomComponentParameters) { const { attackingEntity, hitEntity, itemStack } = event;
// Example beginner behavior: make the item unbreakable. event.durabilityDamage = 0;
console.warn( `[ItemCustomComponent] ${attackingEntity?.typeId} hit ${hitEntity?.typeId} with ${itemStack?.typeId}.` ); },
// Runs when the item is used by a player. onUse(event: ItemComponentUseEvent, _params: CustomComponentParameters) { const { source, itemStack } = event; console.warn(`[ItemCustomComponent] ${source?.typeId} used ${itemStack?.typeId}.`); },
// Runs when the item is used on a block. onUseOn(event: ItemComponentUseOnEvent, _params: CustomComponentParameters) { const { source, usedOnBlockPermutation } = event; console.warn(`[ItemCustomComponent] ${source?.typeId} used item on ${usedOnBlockPermutation?.type.id}.`); },
// Runs when the item mines a block. onMineBlock(event: ItemComponentMineBlockEvent, _params: CustomComponentParameters) { const { source, block, minedBlockPermutation } = event; console.warn( `[ItemCustomComponent] ${source?.typeId} mined ${block?.typeId} and broke ${minedBlockPermutation?.type.id}.` ); },
// Runs when the item hits an entity. onHitEntity(event: ItemComponentHitEntityEvent, _params: CustomComponentParameters) { const { attackingEntity, hitEntity, hadEffect } = event; console.warn( `[ItemCustomComponent] ${attackingEntity?.typeId} hit ${hitEntity?.typeId}. hadEffect=${hadEffect}` ); },
// Requires minecraft:use_modifiers on the item. onCompleteUse(event: ItemComponentCompleteUseEvent, _params: CustomComponentParameters) { const { source, itemStack } = event; console.warn(`[ItemCustomComponent] ${source?.typeId} completed use on ${itemStack?.typeId}.`); },
// Requires minecraft:use_modifiers and minecraft:food on the item. onConsume(event: ItemComponentConsumeEvent, _params: CustomComponentParameters) { const { source, itemStack } = event; console.warn(`[ItemCustomComponent] ${source?.typeId} consumed ${itemStack?.typeId}.`); }, }); });
Contains a set of events that will be raised for an item. This object must be bound using the ItemComponentRegistry.
Example: quickstart.js
Example: quickstart.ts
Example: registerItemComponents_v1.ts
Example: registerItemComponents_v2.ts
Register your component during startup, then attach it in your item JSON using your component identifier (for example,
custom:starter_item_logic).Setup Steps
system.beforeEvents.startup.componentsobject.Event Handlers You Can Use
onBeforeDurabilityDamage(event): Runs before durability damage is applied when hitting an entity. You can modifyevent.durabilityDamage.onCompleteUse(event): Runs when an item's use duration finishes. Dependency: item must includeminecraft:use_modifiers.onConsume(event): Runs when an item is consumed. Dependency: item must include bothminecraft:use_modifiersandminecraft:food.onHitEntity(event): Runs when the item hits an entity.onMineBlock(event): Runs when the item mines a block.onUse(event): Runs when a player uses the item.onUseOn(event): Runs when the item is used on a block.Item JSON Example
Use format version
1.21.90or newer and bind your custom component id:If you also use
onConsume, addminecraft:foodtocomponents.