1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! 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(())
}