import { world } from "./eternal/constants";

/** @type {import("@minecraft/server").BlockCustomComponent} */
const trapdoorComponent = {
  onPlayerInteract({ block, dimension }) {
    // Obtém o estado atual da trapdoor
    const isOpen = block.permutation.getState("ecbl_bc:open");

    // Define o som com base no estado atual
    const sound = isOpen ? "close.wooden_trapdoor" : "open.wooden_trapdoor";

    // Reproduz o som antes de mudar o estado do bloco
    dimension.playSound(sound, block.location, { pitch: 1, volume: 1 });

    // Alterna o estado da trapdoor
    block.setPermutation(block.permutation.withState("ecbl_bc:open", !isOpen));
  },
};

world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
  blockComponentRegistry.registerCustomComponent("ecbl_bc:trapdoor", trapdoorComponent);
});
