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

world.afterEvents.entityDie.subscribe((event) => {
    const { deadEntity, damageSource } = event;

    if (!deadEntity) return;

    if (deadEntity.typeId === "pixell_bbo:aphemus") {
        const deadEntityLocation = deadEntity?.location;
        const deadEntityDimension = deadEntity?.dimension;

        if (!deadEntityLocation || !deadEntityDimension) return;

        const volume = new BlockVolume(
            {
                x: deadEntityLocation.x - 2,
                y: deadEntityLocation.y,
                z: deadEntityLocation.z - 2,
            },
            {
                x: deadEntityLocation.x + 2,
                y: deadEntityLocation.y,
                z: deadEntityLocation.z + 2,
            }
        );

        const blocks = deadEntityDimension.fillBlocks(volume, "pixell_bbo:craterus_vomiting");

        const blockLocations = Array.from(blocks.getBlockLocationIterator());

        for (let block of blockLocations) {
            const random = Math.round(Math.random() * 100);

            const chance = 50;

            if (random <= chance) {
                const b = deadEntityDimension.getBlock(block);

                b.setType("minecraft:air");
            }
        }
    }
});
