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

const scoreboardName = "gs_cb_settings";
const settingPlayers = {
    blockParticles: "blockParticles",
    mobVFX: "mobVFX",
    caveFog: "caveFog",
    fireDisasters: "fireDisasters",
    iceDisasters: "iceDisasters",
};

const sounds = {
    mush_ambience: { id: "gs_cb.mush_ambience", duration: 254.42 },
    magma_random: { id: "gs_cb.magma_random", duration: 4.80 },
    magma_ambience: { id: "gs_cb.magma_ambience", duration: 50.00 },
    magma_geyser: { id: "gs_cb.magma_geyser", duration: 5.00 },
    frozen_ambience: { id: "gs_cb.frozen_ambience", duration: 126.60 },
    frozen_butterflies: { id: "gs_cb.frozen_butterflies", duration: 67.10 },
};

const soundCooldowns = new Map();
const TICKS_PER_SECOND = 20;

function initializeScoreboard() {
    let scoreboard;
    try {
        scoreboard = world.scoreboard.getObjective(scoreboardName);
    } catch {
        scoreboard = world.scoreboard.addObjective(scoreboardName, "Settings");
    }
    return scoreboard;
}

function isSettingEnabled(settingName) {
    try {
        const scoreboard = world.scoreboard.getObjective(scoreboardName);
        const score = scoreboard.getScore(settingName);
        return score !== 0;
    } catch {
        return true;
    }
}

system.runTimeout(() => {
    initializeScoreboard();
}, 1);

function playSoundWithCooldown(dimension, location, soundInfo, cooldownKey, options = {}) {
    const currentTime = system.currentTick;
    const nextPlayableTime = soundCooldowns.get(cooldownKey) || 0;

    if (currentTime >= nextPlayableTime) {
        const playOptions = {
            volume: options.volume || 1.0,
            pitch: options.pitch || 1.0,
        };
        dimension.playSound(soundInfo.id, location, playOptions);

        let cooldownDurationTicks;
        if (soundInfo.id === sounds.magma_random.id) {
            const randomCooldownSeconds = Math.random() * (10 - 5) + 5;
            cooldownDurationTicks = Math.ceil(randomCooldownSeconds * TICKS_PER_SECOND);
        } else {
            cooldownDurationTicks = Math.ceil(soundInfo.duration * TICKS_PER_SECOND);
        }
        
        soundCooldowns.set(cooldownKey, currentTime + cooldownDurationTicks);
        return true;
    }
    return false;
}

system.afterEvents.scriptEventReceive.subscribe((e) => {
    const id = e.id;
    const entity = e.sourceEntity;
    if (!entity?.isValid()) return;

    system.runTimeout(() => {
        try {
            const soundLocation = entity.location;
            const volume = 0.6;
            const pitch = 1.0;

            if (
                id === "gs_cb:falling_magma" &&
                isSettingEnabled(settingPlayers.fireDisasters)
            ) {
                const radius = 2.0;
                const damage = 12;

                entity.dimension.spawnParticle(
                    "minecraft:large_explosion",
                    soundLocation
                );
                entity.dimension.playSound("random.explode", soundLocation, {
                    volume,
                    pitch,
                });

                const players = entity.dimension.getPlayers({
                    location: soundLocation,
                    maxDistance: radius,
                });

                for (const player of players) {
                    player.applyDamage(damage);
                }

                entity.triggerEvent("gs_cb:despawn");
            } else if (
                id === "gs_cb:falling_icicle" &&
                isSettingEnabled(settingPlayers.iceDisasters)
            ) {
                const radius = 1.25;
                const damage = 18;

                entity.dimension.spawnParticle(
                    "minecraft:egg_destroy_emitter",
                    soundLocation
                );
                entity.dimension.playSound("random.glass", soundLocation, {
                    volume,
                    pitch,
                });

                const players = entity.dimension.getPlayers({
                    location: soundLocation,
                    maxDistance: radius,
                });

                for (const player of players) {
                    player.applyDamage(damage);
                }

                entity.triggerEvent("gs_cb:despawn");
            }
        } catch (ignored) {}
    }, 8);
});

world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
    const PARTICLE_SOUND_RANGE = 12; 

    blockComponentRegistry.registerCustomComponent("gs_cb:fire_1", {
        onTick: ({ block }) => {
            if (!isSettingEnabled(settingPlayers.blockParticles)) return;

            const blockLocation = block.location;
            const particleAnchorLoc = { x: blockLocation.x + 0.5, y: blockLocation.y, z: blockLocation.z + 0.5 };
            const soundAnchorLoc = { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 };
            
            block.dimension.spawnParticle("gs_cb:butterfly_particle", particleAnchorLoc);

            const playersInRange = block.dimension.getPlayers({ location: soundAnchorLoc, maxDistance: PARTICLE_SOUND_RANGE });
            const cooldownKey = `block_${blockLocation.x}_${blockLocation.y}_${blockLocation.z}_${sounds.frozen_butterflies.id}`;

            if (playersInRange.length > 0) {
                playSoundWithCooldown(block.dimension, soundAnchorLoc, sounds.frozen_butterflies, cooldownKey, { volume: 0.8 });
            } else {
                if (soundCooldowns.has(cooldownKey)) {
                    soundCooldowns.delete(cooldownKey);
                }
            }
        },
    });

    blockComponentRegistry.registerCustomComponent("gs_cb:fire_2", {
        onTick: ({ block }) => {
            if (!isSettingEnabled(settingPlayers.blockParticles)) return;
            let location = block.location;
            let anchorLoc = { x: location.x + 0.5, y: location.y, z: location.z + 0.5 };
            block.dimension.spawnParticle("gs_cb:holelight_particle", anchorLoc);
        },
    });

    const magmaticParticleBlockIds = ["gs_cb:fire_3", "gs_cb:fire_4", "gs_cb:fire_5", "gs_cb:fire_6"];
    const magmaticParticleNames = ["gs_cb:magmatic_clastic_1", "gs_cb:magmatic_exhalation_1", "gs_cb:magmatic_exhalation_2", "gs_cb:magmatic_exhalation_3"];

    magmaticParticleBlockIds.forEach((blockId, index) => {
        blockComponentRegistry.registerCustomComponent(blockId, { 
            onTick: ({ block }) => {
                if (!isSettingEnabled(settingPlayers.blockParticles)) return;

                const blockLocation = block.location;
                const particleAnchorLoc = { x: blockLocation.x + 0.5, y: blockLocation.y - 0.5, z: blockLocation.z + 0.5 };
                const soundAnchorLoc = { x: blockLocation.x + 0.5, y: blockLocation.y, z: blockLocation.z + 0.5 };
                
                block.dimension.spawnParticle(magmaticParticleNames[index], particleAnchorLoc);

                const playersInRange = block.dimension.getPlayers({ location: soundAnchorLoc, maxDistance: PARTICLE_SOUND_RANGE });
                if (playersInRange.length > 0) {
                    const cooldownKey = `block_${blockLocation.x}_${blockLocation.y}_${blockLocation.z}_${sounds.magma_geyser.id}`;
                    playSoundWithCooldown(block.dimension, soundAnchorLoc, sounds.magma_geyser, cooldownKey, { volume: 0.7 });
                }
            },
        });
    });

    blockComponentRegistry.registerCustomComponent("gs_cb:mushroom_floor_1", { onTick: ({ block }) => { if (!isSettingEnabled(settingPlayers.caveFog)) return; let loc = block.location; block.dimension.spawnParticle( "gs_cb:mushroom_floor_fog.4x4", {x: loc.x+0.5,y: loc.y,z: loc.z+0.5} ); }, });
    blockComponentRegistry.registerCustomComponent("gs_cb:mushroom_floor_2", { onTick: ({ block }) => { if (!isSettingEnabled(settingPlayers.caveFog)) return; let loc = block.location; block.dimension.spawnParticle( "gs_cb:mushroom_floor_fog.6x6", {x: loc.x+0.5,y: loc.y,z: loc.z+0.5} ); }, });
    blockComponentRegistry.registerCustomComponent("gs_cb:mushroom_floor_3", { onTick: ({ block }) => { if (!isSettingEnabled(settingPlayers.caveFog)) return; let loc = block.location; block.dimension.spawnParticle( "gs_cb:mushroom_floor_fog.8x8", {x: loc.x+0.5,y: loc.y,z: loc.z+0.5} ); }, });
    blockComponentRegistry.registerCustomComponent("gs_cb:mushroom_floor_4", { onTick: ({ block }) => { if (!isSettingEnabled(settingPlayers.caveFog)) return; let loc = block.location; block.dimension.spawnParticle( "gs_cb:mushroom_floor_fog.10x10", {x: loc.x+0.5,y: loc.y,z: loc.z+0.5} ); }, });
    blockComponentRegistry.registerCustomComponent("gs_cb:mushroom_floor_face_1", { onTick: ({ block }) => { if (!isSettingEnabled(settingPlayers.caveFog)) return; let loc = block.location; block.dimension.spawnParticle( "gs_cb:mushroom_floor_fog.face.4x4", {x: loc.x+0.5,y: loc.y,z: loc.z+0.5} ); }, });
    blockComponentRegistry.registerCustomComponent("gs_cb:mushroom_floor_face_2", { onTick: ({ block }) => { if (!isSettingEnabled(settingPlayers.caveFog)) return; let loc = block.location; block.dimension.spawnParticle( "gs_cb:mushroom_floor_fog.face.6x6", {x: loc.x+0.5,y: loc.y,z: loc.z+0.5} ); }, });
    blockComponentRegistry.registerCustomComponent("gs_cb:mushroom_floor_face_3", { onTick: ({ block }) => { if (!isSettingEnabled(settingPlayers.caveFog)) return; let loc = block.location; block.dimension.spawnParticle( "gs_cb:mushroom_floor_fog.face.8x8", {x: loc.x+0.5,y: loc.y,z: loc.z+0.5} ); }, });
    blockComponentRegistry.registerCustomComponent("gs_cb:mushroom_floor_face_4", { onTick: ({ block }) => { if (!isSettingEnabled(settingPlayers.caveFog)) return; let loc = block.location; block.dimension.spawnParticle( "gs_cb:mushroom_floor_fog.face.10x10", {x: loc.x+0.5,y: loc.y,z: loc.z+0.5} ); }, });

    const AMBIENT_SOUND_RANGE = 32;
    const EFFECT_DISASTER_RANGE = 25;

    function handleAmbientSoundForCloud(block, ambientSoundInfo) {
        const blockLocation = block.location;
        const particleAnchorLoc = { x: blockLocation.x + 0.5, y: blockLocation.y, z: blockLocation.z + 0.5 };
        const soundDetectionOrigin = { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 };

        if (ambientSoundInfo === sounds.mush_ambience && isSettingEnabled(settingPlayers.caveFog)) {
            block.dimension.spawnParticle("gs_cb:mushroom_island_clouds", particleAnchorLoc);
        } else if (ambientSoundInfo === sounds.magma_ambience && isSettingEnabled(settingPlayers.caveFog)) {
            block.dimension.spawnParticle("gs_cb:magma_mist", particleAnchorLoc);
            block.dimension.spawnParticle("gs_cb:magma_dust", particleAnchorLoc);
        } else if (ambientSoundInfo === sounds.frozen_ambience && isSettingEnabled(settingPlayers.caveFog)) {
            block.dimension.spawnParticle("gs_cb:frozen_mist", particleAnchorLoc);
            block.dimension.spawnParticle("gs_cb:frozen_dust", particleAnchorLoc);
        }

        const playersInSoundRange = block.dimension.getPlayers({
            location: soundDetectionOrigin,
            maxDistance: AMBIENT_SOUND_RANGE
        });
        const allPlayersInDimensionChunks = block.dimension.getPlayers({ location: soundDetectionOrigin });

        const inRangePlayerIds = new Set(playersInSoundRange.map(p => p.id));

        for (const player of playersInSoundRange) {
            const cooldownKey = `player_${player.id}_${ambientSoundInfo.id}`;
            playSoundWithCooldown(player.dimension, player.location, ambientSoundInfo, cooldownKey, { volume: 0.5 });
        }

        for (const player of allPlayersInDimensionChunks) {
            if (!inRangePlayerIds.has(player.id)) {
                const cooldownKey = `player_${player.id}_${ambientSoundInfo.id}`;
                if (soundCooldowns.has(cooldownKey)) {
                    soundCooldowns.delete(cooldownKey);
                }
            }
        }
    }


    blockComponentRegistry.registerCustomComponent("gs_cb:mushroom_island_cloud_1", {
        onTick: ({ block }) => {
            handleAmbientSoundForCloud(block, sounds.mush_ambience);
        },
    });

    blockComponentRegistry.registerCustomComponent("gs_cb:fire_floor_1", { onTick: ({ block }) => { const loc = block.location; const anchor = {x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){ block.dimension.spawnParticle("gs_cb:fire_floor_fog.4x4",anchor); } const players = block.dimension.getPlayers({location:anchor,maxDistance:4}); for(const p of players){ if(p.hasTag("gs_cb:fire_res"))continue; p.setOnFire(2);} }, });
    blockComponentRegistry.registerCustomComponent("gs_cb:fire_floor_2", { onTick: ({ block }) => { const loc = block.location; const anchor = {x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){ block.dimension.spawnParticle("gs_cb:fire_floor_fog.6x6",anchor); } const players = block.dimension.getPlayers({location:anchor,maxDistance:5.7}); for(const p of players){ if(p.hasTag("gs_cb:fire_res"))continue; p.setOnFire(2);} }, });
    blockComponentRegistry.registerCustomComponent("gs_cb:fire_floor_3", { onTick: ({ block }) => { const loc = block.location; const anchor = {x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){ block.dimension.spawnParticle("gs_cb:fire_floor_fog.8x8",anchor); } const players = block.dimension.getPlayers({location:anchor,maxDistance:8}); for(const p of players){ if(p.hasTag("gs_cb:fire_res"))continue; p.setOnFire(2);} }, });
    blockComponentRegistry.registerCustomComponent("gs_cb:fire_floor_4", { onTick: ({ block }) => { const loc = block.location; const anchor = {x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){ block.dimension.spawnParticle("gs_cb:fire_floor_fog.10x10",anchor); } const players = block.dimension.getPlayers({location:anchor,maxDistance:10}); for(const p of players){ if(p.hasTag("gs_cb:fire_res"))continue; p.setOnFire(2);} }, });
    blockComponentRegistry.registerCustomComponent("gs_cb:fire_floor_face_1", { onTick: ({ block }) => { const loc = block.location; const anchor = {x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){ block.dimension.spawnParticle("gs_cb:fire_floor_fog.face.4x4",anchor); } const players = block.dimension.getPlayers({location:anchor,maxDistance:4}); for(const p of players){ if(p.hasTag("gs_cb:fire_res"))continue; p.setOnFire(2);} }, });
    blockComponentRegistry.registerCustomComponent("gs_cb:fire_floor_face_2", { onTick: ({ block }) => { const loc = block.location; const anchor = {x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){ block.dimension.spawnParticle("gs_cb:fire_floor_fog.face.6x6",anchor); } const players = block.dimension.getPlayers({location:anchor,maxDistance:5.7}); for(const p of players){ if(p.hasTag("gs_cb:fire_res"))continue; p.setOnFire(2);} }, });
    blockComponentRegistry.registerCustomComponent("gs_cb:fire_floor_face_3", { onTick: ({ block }) => { const loc = block.location; const anchor = {x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){ block.dimension.spawnParticle("gs_cb:fire_floor_fog.face.8x8",anchor); } const players = block.dimension.getPlayers({location:anchor,maxDistance:8}); for(const p of players){ if(p.hasTag("gs_cb:fire_res"))continue; p.setOnFire(2);} }, });
    blockComponentRegistry.registerCustomComponent("gs_cb:fire_floor_face_4", { onTick: ({ block }) => { const loc = block.location; const anchor = {x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){ block.dimension.spawnParticle("gs_cb:fire_floor_fog.face.10x10",anchor); } const players = block.dimension.getPlayers({location:anchor,maxDistance:10}); for(const p of players){ if(p.hasTag("gs_cb:fire_res"))continue; p.setOnFire(2);} }, });

    blockComponentRegistry.registerCustomComponent("gs_cb:fire_island_cloud_1", {
        onTick: ({ block }) => {
            handleAmbientSoundForCloud(block, sounds.magma_ambience);

            const blockLocation = block.location;
            const soundAndEffectOrigin = { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 };
            
            const playersInEffectRange = block.dimension.getPlayers({
                location: soundAndEffectOrigin,
                maxDistance: EFFECT_DISASTER_RANGE
            });

            for (const player of playersInEffectRange) {
                if (!player.hasTag("gs_cb:fire_res")) {
                    player.setOnFire(2);
                }
                if (isSettingEnabled(settingPlayers.fireDisasters) && Math.random() <= 0.5 && !player.hasTag("gs_cb:fire_res")) {
                    const rayStart = player.getHeadLocation();
                    let direction;
                    if (Math.random() < 0.2) { direction = { x: 0, y: 1, z: 0 }; }
                    else { const spread = 1.15; direction = { x: (Math.random() - 0.5) * spread, y: 1, z: (Math.random() - 0.5) * spread }; }
                    const hit = player.dimension.getBlockFromRay(rayStart, direction, { maxDistance: 32, includeLiquidBlocks: false, includePassableBlocks: false });
                    if (hit?.block) {
                        const hitLoc = hit.block.location;
                        const spawnLoc = { x: hitLoc.x + 0.5, y: hitLoc.y - 1, z: hitLoc.z + 0.5 };
                        block.dimension.spawnEntity("gs_cb:falling_magma", spawnLoc);
                    }
                }
            }

            if (Math.random() < 0.33) {
                if (playersInEffectRange.length > 0) {
                    const magmaRandomCooldownKey = `block_${blockLocation.x}_${blockLocation.y}_${blockLocation.z}_${sounds.magma_random.id}`;
                    playSoundWithCooldown(block.dimension, soundAndEffectOrigin, sounds.magma_random, magmaRandomCooldownKey, { volume: 0.9 });
                }
            }
        },
    });

    blockComponentRegistry.registerCustomComponent("gs_cb:ice_floor_1", { onTick: ({ block }) => { let loc=block.location; let anchor={x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){block.dimension.spawnParticle("gs_cb:ice_floor_fog.4x4",anchor);} const pL=block.dimension.getPlayers({location:anchor,maxDistance:4}); for(const p of pL){if(p.hasTag("gs_cb:ice_res"))continue;p.addEffect("poison",40,{amplifier:1,showParticles:false});}},});
    blockComponentRegistry.registerCustomComponent("gs_cb:ice_floor_2", { onTick: ({ block }) => { let loc=block.location; let anchor={x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){block.dimension.spawnParticle("gs_cb:ice_floor_fog.6x6",anchor);} const pL=block.dimension.getPlayers({location:anchor,maxDistance:6}); for(const p of pL){if(p.hasTag("gs_cb:ice_res"))continue;p.addEffect("poison",40,{amplifier:1,showParticles:false});}},});
    blockComponentRegistry.registerCustomComponent("gs_cb:ice_floor_3", { onTick: ({ block }) => { let loc=block.location; let anchor={x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){block.dimension.spawnParticle("gs_cb:ice_floor_fog.8x8",anchor);} const pL=block.dimension.getPlayers({location:anchor,maxDistance:8}); for(const p of pL){if(p.hasTag("gs_cb:ice_res"))continue;p.addEffect("poison",40,{amplifier:1,showParticles:false});}},});
    blockComponentRegistry.registerCustomComponent("gs_cb:ice_floor_4", { onTick: ({ block }) => { let loc=block.location; let anchor={x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){block.dimension.spawnParticle("gs_cb:ice_floor_fog.10x10",anchor);} const pL=block.dimension.getPlayers({location:anchor,maxDistance:10}); for(const p of pL){if(p.hasTag("gs_cb:ice_res"))continue;p.addEffect("poison",40,{amplifier:1,showParticles:false});}},});
    blockComponentRegistry.registerCustomComponent("gs_cb:ice_floor_face_1", { onTick: ({ block }) => { let loc=block.location; let anchor={x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){block.dimension.spawnParticle("gs_cb:ice_floor_fog.face.4x4",anchor);} const pL=block.dimension.getPlayers({location:anchor,maxDistance:4}); for(const p of pL){if(p.hasTag("gs_cb:ice_res"))continue;p.addEffect("poison",40,{amplifier:1,showParticles:false});}},});
    blockComponentRegistry.registerCustomComponent("gs_cb:ice_floor_face_2", { onTick: ({ block }) => { let loc=block.location; let anchor={x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){block.dimension.spawnParticle("gs_cb:ice_floor_fog.face.6x6",anchor); } const pL=block.dimension.getPlayers({location:anchor,maxDistance:6}); for(const p of pL){if(p.hasTag("gs_cb:ice_res"))continue;p.addEffect("poison",40,{amplifier:1,showParticles:false});}},});
    blockComponentRegistry.registerCustomComponent("gs_cb:ice_floor_face_3", { onTick: ({ block }) => { let loc=block.location; let anchor={x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){block.dimension.spawnParticle("gs_cb:ice_floor_fog.face.8x8",anchor);} const pL=block.dimension.getPlayers({location:anchor,maxDistance:8}); for(const p of pL){if(p.hasTag("gs_cb:ice_res"))continue;p.addEffect("poison",40,{amplifier:1,showParticles:false});}},});
    blockComponentRegistry.registerCustomComponent("gs_cb:ice_floor_face_4", { onTick: ({ block }) => { let loc=block.location; let anchor={x:loc.x+0.5,y:loc.y,z:loc.z+0.5}; if(isSettingEnabled(settingPlayers.caveFog)){block.dimension.spawnParticle("gs_cb:ice_floor_fog.face.10x10",anchor);} const pL=block.dimension.getPlayers({location:anchor,maxDistance:10}); for(const p of pL){if(p.hasTag("gs_cb:ice_res"))continue;p.addEffect("poison",40,{amplifier:1,showParticles:false});}},});

    blockComponentRegistry.registerCustomComponent("gs_cb:ice_island_cloud_1", {
        onTick: ({ block }) => {
            handleAmbientSoundForCloud(block, sounds.frozen_ambience);

            const soundAndEffectOrigin = { x: block.location.x + 0.5, y: block.location.y + 0.5, z: block.location.z + 0.5 };
            
            const playersInEffectRange = block.dimension.getPlayers({
                location: soundAndEffectOrigin,
                maxDistance: EFFECT_DISASTER_RANGE
            });
            for (const player of playersInEffectRange) {
                if (!player.hasTag("gs_cb:ice_res")) {
                    player.addEffect("poison", 40, { amplifier: 1, showParticles: false });
                }
                if (isSettingEnabled(settingPlayers.iceDisasters) && Math.random() <= 0.5 && !player.hasTag("gs_cb:ice_res")) {
                     const rayStart = player.getHeadLocation();
                    let direction;
                    if (Math.random() < 0.2) { direction = { x: 0, y: 1, z: 0 }; }
                    else { const spread = 1.15; direction = { x: (Math.random() - 0.5) * spread, y: 1, z: (Math.random() - 0.5) * spread }; }
                    const hit = player.dimension.getBlockFromRay(rayStart, direction, { maxDistance: 32, includeLiquidBlocks: false, includePassableBlocks: false });
                    if (hit?.block) {
                        const hitLoc = hit.block.location;
                        const spawnLoc = { x: hitLoc.x + 0.5, y: hitLoc.y - 1, z: hitLoc.z + 0.5 };
                        block.dimension.spawnEntity("gs_cb:falling_icicle", spawnLoc);
                    }
                }
            }
        },
    });

    blockComponentRegistry.registerCustomComponent("gs_cb:amethyst_island_cloud_1", {
        onTick: ({ block }) => {
            const blockLocation = block.location;
            const particleAnchorLoc = { x: blockLocation.x + 0.5, y: blockLocation.y, z: blockLocation.z + 0.5 };
            block.dimension.spawnParticle("gs_cb:amethyst_mist", particleAnchorLoc);
            block.dimension.spawnParticle("gs_cb:amethyst_dust", particleAnchorLoc);
        },
    });
});