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

system.runInterval(() => {
    world.getAllPlayers().forEach(player => {
        // Verifica se o jogador está caindo
        if (player.getVelocity().y < -0.6) { 
            // Obtém o bloco abaixo do jogador
            const blockBelow = player.dimension.getBlock({
                x: Math.floor(player.location.x),
                y: Math.floor(player.location.y) - 1,
                z: Math.floor(player.location.z)
            });

            // Se o bloco abaixo for o bloco específico, aplica Slow Falling antes do impacto
            if (blockBelow && blockBelow.typeId === "ecbl_bc:bounce_mushroom") {
                player.applyKnockback(0, 0, 1, 1.2);
                player.runCommand(`particle ecbl_bc:mushroom_spores ~~~`);
                player.runCommand(`particle plank_waao:mushroom_spores_1 ~~~`);
                player.runCommand(`particle plank_waao:mushroom_spores_2 ~~~`);
                player.runCommand(`particle plank_waao:mushroom_spores_3 ~~~`);
            }
        }
    });
});

class JumpSlime {
  onStepOn({ entity }) {
    system.run(() => {
      // Aplica um impulso para cima simulando o pulo do slime
      entity.applyKnockback(0, 0, 1, 1.2);

      // Toca o som do slime
      entity.dimension.playSound("block.slime_block.jump", entity.location);
      entity.runCommand(`particle ecbl_bc:mushroom_spores ~~~`);
      entity.runCommand(`particle plank_waao:mushroom_spores_1 ~~~`);
      entity.runCommand(`particle plank_waao:mushroom_spores_2 ~~~`);
      entity.runCommand(`particle plank_waao:mushroom_spores_3 ~~~`);
    });
  }
}

world.beforeEvents.worldInitialize.subscribe((event) => {
  event.blockComponentRegistry.registerCustomComponent("ecbl_bc:slime_jump", new JumpSlime());
});
