import {
  world,
  EntityDamageCause
} from "@minecraft/server";
world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('we:ostrich_egg', {
    onTick(e) {
      const {
        block
      } = e;
      const {
        x, y, z
      } = block.center();
      if (block.typeId !== 'we:ostrich_egg_block') return;
      const permutation = block.permutation;
      const startState = permutation.getState('we:hatching_stage');

      if (startState == 2) return;
      if (startState == 1) {
        e.dimension.playSound('block.turtle_egg.break', block.center(), {pitch: 0.8, volume: 1})
        e.dimension.spawnParticle('we:egg_hatched', block.center())
        e.dimension.runCommandAsync(`execute positioned ${x} ${y} ${z} run function egg_hatch`);
        return
      }
      if (startState == 0) {
        e.dimension.playSound('block.turtle_egg.crack', block.center(), {pitch: 0.8, volume: 1})
        e.dimension.spawnParticle('we:egg_hatched', block.center())
        const newPermutation = permutation.withState('we:hatching_stage', 1);
        block.setPermutation(newPermutation);
        return;
      }
    }
  });
});