const DEEP_Y_LEVEL = 20;        
const GUNFIRE_PANIC_CHANCE = 0.30; 
const MINING_PANIC_CHANCE = 0.05;  

// --- GUNFIRE INTERCEPTOR ---
BlockEvents.rightClicked(event => {
    const { player, level, server, item } = event;
    if (!player || !level || !server || level.isClientSide()) return;

    if (player.y < DEEP_Y_LEVEL && item.id.includes('tacz:')) {
        if (Math.random() < GUNFIRE_PANIC_CHANCE) {
            let username = player.username.getString();
            let echoX = player.x + (Math.random() * 6 - 3);
            let echoY = player.y;
            let echoZ = player.z - 4; 

            server.scheduleInTicks(20, callback => {
                if (server.getPlayer(username)) {
                    let p = server.getPlayer(username);
                    server.runCommandSilent(`playsound stalker:throw master ${username} ${echoX} ${echoY} ${echoZ} 10 0.3`);
                    p.displayClientMessage(Component.darkGray("Something echoed back...").italic(true), true);
                    server.runCommandSilent(`effect give ${username} minecraft:blindness 3 1 true`);
                    server.runCommandSilent(`effect give ${username} minecraft:slowness 2 1 true`);
                }
            });
        }
    }
});

// --- FIXED: BLOCK MINING INTERCEPTOR ---
BlockEvents.broken(event => { // <--- FIXED HERE
    const { player, level, server, block } = event;
    if (!player || !level || !server || level.isClientSide()) return;

    if (player.y < DEEP_Y_LEVEL) {
        if (Math.random() < MINING_PANIC_CHANCE) {
            let username = player.username.getString();
            server.runCommandSilent(`playsound minecraft:entity.breeze.whirl master ${username} ${block.x} ${block.y} ${block.z} 5 0.5`);
            server.runCommandSilent(`particle minecraft:squid_ink ${block.x + 0.5} ${block.y + 0.5} ${block.z + 0.5} 0.3 0.3 0.3 0.01 15`);
            player.displayClientMessage(Component.red("Keep digging. I like the noise."), false);
            server.runCommandSilent(`effect give ${username} minecraft:speed 4 1 true`);
            server.runCommandSilent(`effect give ${username} minecraft:mining_fatigue 3 2 true`);
        }
    }
});