// --- 100% RELIABLE DETECTOR-BASED BUNKER BREACH SCRIPT ---

PlayerEvents.tick(event => {
    const { player, level, server } = event;

    // Failsafe checks running every 1 second (20 ticks) to prevent log clutter or lag
    if (!player || !level || !server || level.isClientSide()) return;
    if (player.age % 20 !== 0) return;

    // Scan a 15-block area around the player for any active entities
    let entities = level.getEntitiesWithin(AABB.of(player.x - 15, player.y - 15, player.z - 15, player.x + 15, player.y + 15, player.z + 15));
    let stalkerPresent = false;
    let stalkerX = player.x;
    let stalkerY = player.y;
    let stalkerZ = player.z;

    // Look through nearby entities to find the Stalker
    entities.forEach(entity => {
        if (String(entity.type) === 'stalker:stalker') {
            stalkerPresent = true;
            stalkerX = entity.x;
            stalkerY = entity.y;
            stalkerZ = entity.z;
        }
    });

    // If the stalker is detected near you, scan for SecurityCraft components to breach
    if (stalkerPresent) {
        let playerPos = player.blockPosition();

        for (let x = -6; x <= 6; x++) {
            for (let y = -3; y <= 3; y++) {
                for (let z = -6; z <= 6; z++) {
                    
                    let targetPos = playerPos.add(x, y, z);
                    let block = level.getBlock(targetPos);
                    let blockId = String(block.id);

                    if (blockId.includes('securitycraft:')) {
                        let state = block.blockState;
                        let modified = false;
                        
                        // 1. FORCIBLY OPEN DOORS: Flip properties to TRUE
                        if (state.hasProperty(BlockProperties.OPEN) && !state.getValue(BlockProperties.OPEN)) {
                            state = state.setValue(BlockProperties.OPEN, true);
                            modified = true;
                        }
                        
                        // 2. FORCIBLY SHORT TERMINALS: Flip keypads to POWERED states
                        if (state.hasProperty(BlockProperties.POWERED) && !state.getValue(BlockProperties.POWERED)) {
                            state = state.setValue(BlockProperties.POWERED, true);
                            modified = true;
                        }
                        
                        // Apply block transformations cleanly on a single frame
                        if (modified) {
                            level.setBlock(targetPos, state, 3);
                            
                            // Blast audio effects directly at the stalker's current position
                            server.runCommandSilent(`execute at ${player.username.getString()} run playsound minecraft:entity.zombie.attack_iron_door ambient @a ${stalkerX} ${stalkerY} ${stalkerZ} 2.0 0.5`);
                            server.runCommandSilent(`execute at ${player.username.getString()} run playsound minecraft:block.iron_trapdoor.open ambient @a ${stalkerX} ${stalkerY} ${stalkerZ} 2.0 0.4`);
                            
                            // Plaster notification across the screen actionbar
                            server.runCommandSilent(`execute at ${player.username.getString()} run title @a actionbar {"text":"KEYPAD SYSTEMS TERMINAL CRITICAL POWER FLUCTUATION","color":"red"}`);
                            return; 
                        }
                    }
                }
            }
        }
    }
});
