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

system.afterEvents.scriptEventReceive.subscribe(event => {
    const { sourceEntity, id, message } = event;
    let entity = sourceEntity;

    if (id === "dodo:ender_chest_door" && message === 'enderChestParticles') {
        let particleCount = 0;

        entity.setProperty('dodo:particles', false);

        system.run(runParticle);
        function runParticle() {
            if (particleCount < 40) {
                let randomYOffset = Math.random();
                try {
                    entity.runCommandAsync(`particle minecraft:end_chest ~ ~${randomYOffset} ~`)
                } catch (e) {}
                particleCount++;
                system.run(runParticle);
            }
        }
    }
})