Provides the functionality for registering custom components for items.

Example

registerCustomItemComponent.ts

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

Hierarchy

  • ItemComponentRegistry

Constructors

Methods

Constructors

Methods

  • Parameters

    • name: string

      The id that represents this custom component. Must have a namespace. This id can be specified in a item's JSON configuration under the 'minecraft:custom_components' item component.

    • itemCustomComponent: ItemCustomComponent

      The collection of event functions that will be called when the event occurs on an item using this custom component id.

    Returns void

    Remarks

    Registers an item custom component that can be used in item JSON configuration.

    Throws

    This function can throw errors.

    CustomComponentInvalidRegistryError

    CustomComponentNameError

    minecraftcommon.EngineError

    ItemCustomComponentAlreadyRegisteredError

    ItemCustomComponentReloadNewComponentError

    ItemCustomComponentReloadNewEventError

    ItemCustomComponentReloadVersionError

    Example

    registerComponent.ts

    import {
    BlockPermutation,
    ItemComponentMineBlockEvent,
    ItemCustomComponent,
    world,
    } from "@minecraft/server";

    class MineDiamondComponent implements ItemCustomComponent {
    onMineBlock(e: ItemComponentMineBlockEvent): void {
    const { minedBlockPermutation, block } = e;
    if (minedBlockPermutation.matches("minecraft:diamond_ore")) {
    block.setPermutation(BlockPermutation.resolve("minecraft:stone"));
    }
    }
    }

    world.beforeEvents.worldInitialize.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent(
    "jayly:custom_item",
    new MineDiamondComponent()
    );
    });