Make an interactable block with custom components - Simon's Digital Garden

Make an interactable block with custom components

NOTE: This guide was made for Minecraft Bedrock Preview 1.21.0.22, this will not work in 1.20

Requirements

  • Minecraft preview 1.21 or higher
  • Have a block created

Step 1: Register custom component

Custom compoenent registration can only be done in `world.beforeEvents.worldInitialize’ and will only take into effect in a world restart

world.beforeEvents.worldInitialize.subscribe((eventData) => {
    eventData.blockTypeRegistry.registerCustomComponent(
        'example:customEvent', // Name of custom event, this will go in the block JSON later
        new onCustomEvent(); // Custom event binding object
};

Step 2: Bind component

The next step is to bind the component to the event, there is a list of events here

export class onCustomEvent implements BlockCustomComponent {
    constructor() {
        this.onPlayerInteract = this.onPlayerInteract.bind(this); // Event type
    }
    onPlayerInteract(eventData: BlockComponentPlayerInteractEvent) {
        world.sendMessage("Block interacted");  // Event logic here
    }
}

Step 3: Add custom component to block

Once the event created, to add the custom component to the block, add to the block components the minecraft:custom_componentfeild with an array of custom component strings.

{
    "minecraft:block": {
        "components": {
            "minecraft:custom_components": ["example:customEvent"]
        }
    }
}