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

system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity: entity }) => {
    switch(id) {
        case "sapphire_meteors:starspark_place_flowers": {
            entity.dimension.placeFeature("sapphire_meteors:scatter_flowers_feature", entity.location);
            break;
        }
        case "sapphire_meteors:starspark_eat": {
            const { x, y, z } = entity.location;
            const ores = [
                "minecraft:coal_ore",
                "minecraft:iron_ore",
                "minecraft:copper_ore",
                "minecraft:gold_ore",
                "minecraft:redstone_ore",
                "minecraft:emerald_ore",
                "minecraft:lapis_ore",
                "minecraft:diamond_ore",
                "minecraft:deepslate_coal_ore",
                "minecraft:deepslate_iron_ore",
                "minecraft:deepslate_copper_ore",
                "minecraft:deepslate_gold_ore",
                "minecraft:deepslate_redstone_ore",
                "minecraft:deepslate_emerald_ore",
                "minecraft:deepslate_lapis_ore",
                "minecraft:deepslate_diamond_ore",
                "sapphire_meteors:gravity_ore"
            ];

            for (let dx = -8; dx <= 8; dx++) {
                for (let dy = -8; dy <= 8; dy++) {
                    for (let dz = -8; dz <= 8; dz++) {
                        const bx = x + dx;
                        const by = y + dy;
                        const bz = z + dz;
                        try {
                            const block = entity.dimension?.getBlock({ x: bx, y: by, z: bz });

                            if (ores.includes(block.typeId)) {
                                block.dimension.spawnEntity("sapphire_meteors:starspark_outline", { x: block.center().x, y: block.location.y, z: block.center().z});
                            }
                        } catch {}
                    }
                }
            }
            break;
        }
    }
});
