Making a custom block component. I want it so when a stone block is adjacent to my custom block, it sends a message one time, if this condition becomes false again (no adjacent block) and then becomes true, it should send the message again. Here's what I have:
// Import necessary modules from Minecraft server API
import { world } from '@minecraft/server';
// Subscribe to the 'worldInitialize' event to register custom components
world.beforeEvents.worldInitialize.subscribe(eventData => {
// Register a custom component named kai:on_tick for trapdoor interaction with redstone
eventData.blockTypeRegistry.registerCustomComponent('kai:on_tick', {
onTick(e) {
// Destructure event data for easier access
const { block, dimension } = e;
const { x, y, z } = block.location;
// Define directions to check
const directions = [
{ dx: 1, dy: 0, dz: 0 }, // East
{ dx: -1, dy: 0, dz: 0 }, // West
{ dx: 0, dy: 0, dz: 1 }, // South
{ dx: 0, dy: 0, dz: -1 }, // North
{ dx: 0, dy: 1, dz: 0 }, // Up
{ dx: 0, dy: -1, dz: 0 } // Down
];
// Check each direction for a stone block
for (const { dx, dy, dz } of directions) {
const blockAdjacent = dimension.getBlock({ x: x + dx, y: y + dy, z: z + dz });
// Send message if a stone block is found
if (blockAdjacent?.typeId === 'minecraft:stone') {
world.sendMessage("I have the power!!!");
break;
}
}
}
});
});