EntityEvents.spawned(event => {
    // 1. Client-Side Safety Valve
    if (event.level.clientSide) return;

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

    // 2. Strict Object String Casting
    let entityId = String(entity.type); 

    // Match any instance matching your stalker-1.2.2.jar namespace
    if (entityId.includes('stalker')) {
        console.info(`[STALKER DETECTED] Processing quest trigger for ID: ${entityId}`);
        
        // 3. Precise 3D Coordinate Targeting
        let ex = entity.x;
        let ey = entity.y;
        let ez = entity.z;

        // Use standard Java max value to force a true 3D spherical radius search (no Y-axis blind spots)
        let player = event.level.getNearestPlayer(ex, ey, ez, 64.0, false);
        
        if (player) {
            // 4. Bulletproof Username Extraction for 1.20.1 Forge
            let username = player.username ? String(player.username) : String(player.getName().getString());
            
            // Execute the updated Quest ID command cleanly via the server console instance
            event.server.runCommandSilent(`ftbquests change_progress ${username} complete 159144AF230B9AE4`);
            console.info(`[STALKER SUCCESS] Quest 159144AF230B9AE4 completed for player: ${username}`);
        } else {
            console.warn(`[STALKER WARNING] Stalker spawned at X:${Math.round(ex)} Y:${Math.round(ey)} Z:${Math.round(ez)}, but no player was within 64 blocks!`);
        }
    }
});