use std::time::Duration;
use mcapi::slp::SlpClient;
#[tokio::main]
async fn main() {
let Some(arg) = std::env::args().nth(1) else {
println!("Usage: cargo run --example server_ping -- <host[:port]>");
return;
};
let (host, port) = match arg.rsplit_once(':') {
Some((h, p)) if !h.contains(':') || h.starts_with('[') => (h, p.parse::<u16>().ok()),
_ => (arg.as_str(), None),
};
let client = SlpClient::builder().timeout(Duration::from_secs(5)).build();
match client.ping_java(host, port).await {
Ok(report) => {
println!(
"{} ({}) -> {}/{} players, protocol {}, {} ms",
report.status.description.to_stripped_text(),
report.status.version.name,
report.status.players.online,
report.status.players.max,
report.status.version.protocol,
report.latency.as_millis()
);
}
Err(err) => eprintln!("Ping failed for '{host}': {err}"),
}
}