import {
  world,
  EntityDamageCause
} from "@minecraft/server";
world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('we:phozl_dencha', {
    onTick(e) {
      const {
        block
      } = e;
      if (block.typeId !== 'we:phozl_dencha_block') return;
      const permutation = block.permutation;
      const above = block.above().typeId;
      const below = block.below().typeId;


      if (above !== 'we:phozl_dencha_block') {
        const newPermutation = permutation.withState('we:height', 0);
        block.setPermutation(newPermutation);
        return;
      } else if (above === 'we:phozl_dencha_block' && block.above(2).typeId === 'we:phozl_dencha_block' && below !== 'we:phozl_dencha_block') {
        const newPermutation = permutation.withState('we:height', 3);
        block.setPermutation(newPermutation);
        return;
      } else if (above === 'we:phozl_dencha_block' && block.above(2).typeId !== 'we:phozl_dencha_block') {
        const newPermutation = permutation.withState('we:height', 1);
        block.setPermutation(newPermutation);
        return;
      } else if (below === 'we:phozl_dencha_block' && above === 'we:phozl_dencha_block' && block.above(2).typeId === 'we:phozl_dencha_block') {

        const newPermutation = permutation.withState('we:height', 2);
        block.setPermutation(newPermutation);
        return;
      }
    }
  });
  initEvent.blockComponentRegistry.registerCustomComponent('we:boosted_fall_damage', {
    onEntityFallOn(e) {
      const damage = Math.ceil((e.fallDistance * 2) - 2);
      if (damage <= 0) return;
      e.entity.applyDamage(damage, {
        cause: "stalagmite"
      });
    }
  });
});