rust-mc-status 2.0.0

High-performance asynchronous Rust library for querying Minecraft server status (Java & Bedrock)
Documentation
//! Advanced usage example for the rust-mc-status library.
//!
//! This example demonstrates advanced features:
//! - Batch queries with parallel processing
//! - Accessing detailed server information
//! - Working with plugins and mods
//! - Saving server favicons
//! - DNS information
//! - Handling different server types

use rust_mc_status::{McClient, ServerEdition, ServerInfo};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a client with optimized settings
    let client = McClient::new()
        .with_timeout(Duration::from_secs(5))
        .with_max_parallel(10);

    println!("=== Advanced Usage Examples ===\n");

    // List of servers to check (mix of Java and Bedrock)
    let servers = vec![
        ServerInfo {
            address: "mc.hypixel.net".to_string(),
            edition: ServerEdition::Java,
        },
        ServerInfo {
            address: "geo.hivebedrock.network:19132".to_string(),
            edition: ServerEdition::Bedrock,
        },
    ];

    println!("Requesting status for {} servers in parallel...\n", servers.len());
    let results = client.ping_many(&servers).await;

    for (server, result) in results {
        println!("{}", "=".repeat(60));
        println!("Server: {} ({:?})", server.address, server.edition);
        println!("{}", "-".repeat(60));

        match result {
            Ok(status) => {
                println!("Status: ✅ Online");
                println!("Latency: {:.2} ms", status.latency);
                println!("IP: {}:{}", status.ip, status.port);
                println!("Hostname: {}", status.hostname);

                // DNS information
                if let Some(dns) = &status.dns {
                    println!("DNS Information:");
                    println!("  A-records: {:?}", dns.a_records);
                    if let Some(cname) = &dns.cname {
                        println!("  CNAME: {}", cname);
                    }
                    println!("  TTL: {} seconds", dns.ttl);
                }

                println!();

                // Process data depending on server type
                match &status.data {
                    rust_mc_status::ServerData::Java(java_status) => {
                        println!("Java Edition Server:");
                        println!("  Version: {} (protocol: {})", 
                            java_status.version.name, 
                            java_status.version.protocol);
                        println!("  Players: {}/{}", 
                            java_status.players.online, 
                            java_status.players.max);
                        println!("  Description: {}", java_status.description);

                        // Show player sample if available
                        if let Some(sample) = &java_status.players.sample {
                            if !sample.is_empty() {
                                println!("  Online players sample:");
                                for player in sample.iter().take(5) {
                                    println!("    - {}", player.name);
                                }
                                if sample.len() > 5 {
                                    println!("    ... and {} more", sample.len() - 5);
                                }
                            }
                        }

                        // Optional fields
                        if let Some(ref map) = java_status.map {
                            println!("  Map: {}", map);
                        }

                        if let Some(ref gamemode) = java_status.gamemode {
                            println!("  Game mode: {}", gamemode);
                        }

                        if let Some(ref software) = java_status.software {
                            println!("  Server software: {}", software);
                        }

                        // Plugins
                        if let Some(ref plugins) = java_status.plugins {
                            if !plugins.is_empty() {
                                println!("  Plugins ({}):", plugins.len());
                                for plugin in plugins.iter().take(10) {
                                    let version_info = plugin
                                        .version
                                        .as_ref()
                                        .map(|v| format!(" v{}", v))
                                        .unwrap_or_default();
                                    println!("    - {}{}", plugin.name, version_info);
                            }
                                if plugins.len() > 10 {
                                    println!("    ... and {} more plugins", plugins.len() - 10);
                            }
                        }
                        }

                        // Mods
                        if let Some(ref mods) = java_status.mods {
                            if !mods.is_empty() {
                                println!("  Mods ({}):", mods.len());
                                for mod_ in mods.iter().take(10) {
                                    let version_info = mod_
                                        .version
                                        .as_ref()
                                        .map(|v| format!(" v{}", v))
                                        .unwrap_or_default();
                                    println!("    - {}{}", mod_.modid, version_info);
                            }
                                if mods.len() > 10 {
                                    println!("    ... and {} more mods", mods.len() - 10);
                                }
                            }
                        }

                        // Save favicon if available
                        if java_status.favicon.is_some() {
                            let filename = format!("{}_icon.png", 
                                server.address.replace(":", "_"));
                            match java_status.save_favicon(&filename) {
                                Ok(()) => {
                                    println!("  Favicon saved to: {}", filename);
                                }
                                Err(e) => {
                                    println!("  Failed to save favicon: {}", e);
                            }
                        }
                        }
                    }
                    rust_mc_status::ServerData::Bedrock(bedrock_status) => {
                        println!("Bedrock Edition Server:");
                        println!("  Edition: {}", bedrock_status.edition);
                        println!("  Version: {} (protocol: {})", 
                            bedrock_status.version, 
                            bedrock_status.protocol_version);
                        println!("  Players: {}/{}", 
                            bedrock_status.online_players, 
                            bedrock_status.max_players);
                        println!("  MOTD: {}", bedrock_status.motd);
                        if !bedrock_status.motd2.is_empty() {
                            println!("  MOTD2: {}", bedrock_status.motd2);
                        }

                        // Optional fields
                        if let Some(ref map) = bedrock_status.map {
                            println!("  Map: {}", map);
                        }

                        if let Some(ref software) = bedrock_status.software {
                            println!("  Server software: {}", software);
                        }

                        println!("  Game mode: {} ({})", 
                            bedrock_status.game_mode, 
                            bedrock_status.game_mode_numeric);
                        println!("  Server UID: {}", bedrock_status.server_uid);
                        println!("  Ports: IPv4={}, IPv6={}", 
                            bedrock_status.port_ipv4, 
                            bedrock_status.port_ipv6);
                    }
                }
            }
            Err(e) => {
                println!("Status: ❌ Error");
                println!("Error type: {:?}", e);
                println!("Error message: {}", e);
            }
        }

        println!();
    }

    println!("{}", "=".repeat(60));
    println!("\nAll queries completed!");

    Ok(())
}