import { world, EntityDamageCause } from '@minecraft/server';

const simple_interaction = {
    onPlayerInteract(event) {
        const block = event.block;
        const { x, y, z } = block.location;
        const player = event.player;
        const dimension = block.dimension;
        const damageVolume = { x: 0.6, y: 0.625, z: 0.6 };
        if (block.typeId === "furnideco:rustic.mounted_goat_horn") {
            player.playSound("furnideco.goat_horn", { location: block });
            dimension.spawnParticle("furnideco:wave", { x: x + 0.5, y: y + 0.25, z: z + 0.5 });
        } else if (block.typeId === "furnideco:rustic.trash_can") {
            const entities = dimension.getEntities({
                location: block.location,
                volume: damageVolume,
                type: "item"
            });
            for (const entity of entities) {
                entity.applyDamage(255, {
                    cause: EntityDamageCause.fire
                });
            }
            player.playSound("block.blastfurnace.fire_crackle", { location: block });
            dimension.spawnParticle("furnideco:dust", { x: x + 0.5, y: y + 0.25, z: z + 0.5 });
        }
    }
};

world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
    blockComponentRegistry.registerCustomComponent(
        "furnideco:simple_interaction",
        simple_interaction
    );
});
