import {
  world
} from "@minecraft/server";
world.beforeEvents.worldInitialize.subscribe(initEvent => {

initEvent.blockComponentRegistry.registerCustomComponent('we:bruudlin_egg', {
    onEntityFallOn(e) {
      const {
        block
      } = e;
      const {
        x, y, z
      } = block.center();

      if(e.entity.typeId === 'we:bruudlin') return;
      
      if(e.fallDistance > 0.8) {
        e.dimension.runCommandAsync(`setblock ${x} ${y} ${z} air destroy`);
        const bruudlin = e.dimension.spawnEntity('we:bruudlin', block.center());
        bruudlin.triggerEvent('we:burst');
      }
    },
    onPlayerDestroy(e) {
      const {
        block
      } = e;
      const {
        x, y, z
      } = block.center();
      
      if(e.player.matches({gameMode: 'creative'})) return;

      e.dimension.runCommandAsync(`setblock ${x} ${y} ${z} air destroy`);
      const bruudlin = e.dimension.spawnEntity('we:bruudlin', block.center());
      bruudlin.triggerEvent('we:burst');
    }
  });
});