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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:moa_egg_block_hatching", {
    onTick: eventData => {
      const block = eventData.block;
      const blockLocation = block.location;
      if (block.permutation.matches("nicothekid:moa_egg_block")) {
        system.run(() => {
          block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          block.dimension.playSound("block.turtle_egg.crack", blockLocation, { pitch: 1.0, volume: 1.0 });
          block.dimension.runCommandAsync(`summon nicothekid:giant_moa ${blockLocation.x + 0.5} ${blockLocation.y} ${blockLocation.z + 0.5} ~ ~ minecraft:giant_moa_born`);
          return;
        });
      }
      else return;
    }
  });
});