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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:myceling_egg_block_hatching", {
    onTick: eventData => {
      const block = eventData.block;
      const blockLocation = block.location;
      if (block.permutation.matches("nicothekid:myceling_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:myceling ${blockLocation.x + 0.5} ${blockLocation.y} ${blockLocation.z + 0.5} ~ ~ minecraft:myceling_born`);
          return;
        });
      }
      else return;
    }
  });
});