advanced_usage/
advanced_usage.rs

1//! Example of advanced usage of the rust-mc-status library
2//! Demonstrates new capabilities of the library
3
4use mc_server_status::{McClient, ServerEdition, ServerInfo};
5use std::time::Duration;
6
7#[tokio::main]
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let client = McClient::new()
10        .with_timeout(Duration::from_secs(5))
11        .with_max_parallel(10);
12
13    // List of servers to check
14    let servers = vec![
15        ServerInfo {
16            address: "mc.hypixel.net".to_string(),
17            edition: ServerEdition::Java,
18        },
19        ServerInfo {
20            address: "geo.hivebedrock.network:19132".to_string(),
21            edition: ServerEdition::Bedrock,
22        },
23        ServerInfo {
24            address: "mc233.cn".to_string(),
25            edition: ServerEdition::Java,
26        },
27    ];
28
29    println!("Requesting status for {} servers...", servers.len());
30    let results = client.ping_many(&servers).await;
31
32    for (server, result) in results {
33        println!("\n{}", "=".repeat(50));
34        println!("Server: {} ({:?})", server.address, server.edition);
35
36        match result {
37            Ok(status) => {
38                println!("Status: ✅ Online (latency: {:.2} ms)", status.latency);
39                println!("IP: {}, Port: {}", status.ip, status.port);
40                println!("Hostname: {}", status.hostname);
41
42                // DNS information
43                if let Some(dns) = status.dns {
44                    println!(
45                        "DNS: A-records: {:?}, CNAME: {:?}",
46                        dns.a_records, dns.cname
47                    );
48                }
49
50                // Processing data depending on server type
51                match status.data {
52                    mc_server_status::ServerData::Java(java_status) => {
53                        println!(
54                            "Version: {} (protocol: {})",
55                            java_status.version.name, java_status.version.protocol
56                        );
57                        println!(
58                            "Players: {}/{}",
59                            java_status.players.online, java_status.players.max
60                        );
61                        println!("Description: {}", java_status.description);
62
63                        // Используем ссылку вместо перемещения
64                        if let Some(ref map) = java_status.map {
65                            println!("Map: {}", map);
66                        }
67
68                        if let Some(ref gamemode) = java_status.gamemode {
69                            println!("Game mode: {}", gamemode);
70                        }
71
72                        if let Some(ref software) = java_status.software {
73                            println!("Server software: {}", software);
74                        }
75
76                        // Plugins and mods
77                        if let Some(ref plugins) = java_status.plugins {
78                            println!("Plugins ({}):", plugins.len());
79                            for plugin in plugins.iter().take(5) {
80                                println!(
81                                    "  - {} {}",
82                                    plugin.name,
83                                    plugin.version.as_deref().unwrap_or("")
84                                );
85                            }
86                            if plugins.len() > 5 {
87                                println!("  ... and {} more", plugins.len() - 5);
88                            }
89                        }
90
91                        if let Some(ref mods) = java_status.mods {
92                            println!("Mods ({}):", mods.len());
93                            for mod_ in mods.iter().take(5) {
94                                println!(
95                                    "  - {} {}",
96                                    mod_.modid,
97                                    mod_.version.as_deref().unwrap_or("")
98                                );
99                            }
100                            if mods.len() > 5 {
101                                println!("  ... and {} more", mods.len() - 5);
102                            }
103                        }
104
105                        // Saving icon
106                        if let Some(ref _favicon) = java_status.favicon {
107                            if let Err(e) = java_status.save_favicon("server_icon.png") {
108                                println!("Failed to save icon: {}", e);
109                            } else {
110                                println!("Icon saved as server_icon.png");
111                            }
112                        }
113                    }
114                    mc_server_status::ServerData::Bedrock(bedrock_status) => {
115                        println!("Edition: {}", bedrock_status.edition);
116                        println!("Version: {}", bedrock_status.version);
117                        println!(
118                            "Players: {}/{}",
119                            bedrock_status.online_players, bedrock_status.max_players
120                        );
121                        println!("MOTD: {}", bedrock_status.motd);
122
123                        if let Some(ref map) = bedrock_status.map {
124                            println!("Map: {}", map);
125                        }
126
127                        if let Some(ref software) = bedrock_status.software {
128                            println!("Server software: {}", software);
129                        }
130
131                        println!(
132                            "Game mode: {} ({})",
133                            bedrock_status.game_mode, bedrock_status.game_mode_numeric
134                        );
135                    }
136                }
137            }
138            Err(e) => {
139                println!("Status: ❌ Error: {}", e);
140            }
141        }
142    }
143
144    Ok(())
145}