BlockEvents.rightClicked(event => {
    if (event.level.clientSide) return;

    let player = event.player;
    if (!player) return;

    let blockId = String(event.block.id);

    // Target any standard chest or modded storage locker
    if (blockId.includes('chest') || blockId.includes('barrel') || blockId.includes('shulker')) {
        
        // 0.4% Chance (1 out of 250 chest openings)
        if (Math.random() < 0.004) {
            let username = player.username ? String(player.username) : String(player.getName().getString());
            
            // Wait exactly 4 ticks (0.2 seconds) after they look inside, then kick them out of the menu
            event.server.scheduleInTicks(4, callback => {
                player.closeMenu(); // Natively kicks the player out of the inventory screen
                
                // Blast the mechanical conduit malfunction static mixed with a heavy wood snap (Pitch 0.4)
                event.server.runCommandSilent(`playsound minecraft:block.conduit.deactivate ambient ${username} ~ ~ ~ 2.0 0.4`);
                event.server.runCommandSilent(`playsound minecraft:entity.zombie.attack_wooden_door ambient ${username} ~ ~ ~ 1.5 0.3`);
                
                // Flash 2 seconds of total blindness darkness
                event.server.runCommandSilent(`effect give ${username} minecraft:darkness 2 0 true`);
                
                player.setStatusMessage(Text.darkRed("SOMETHING SLAMMED THE LID SHUT FROM THE INSIDE."));
            });
        }
    }
});