From 5029a09963b5753c1f9b7f777f28e1c0951343e7 Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 6 Dec 2021 00:28:40 -0600 Subject: Initial commit --- minecraft-protocol/src/lib.rs | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 minecraft-protocol/src/lib.rs (limited to 'minecraft-protocol/src/lib.rs') diff --git a/minecraft-protocol/src/lib.rs b/minecraft-protocol/src/lib.rs new file mode 100644 index 00000000..8c647dc2 --- /dev/null +++ b/minecraft-protocol/src/lib.rs @@ -0,0 +1,53 @@ +use std::net::IpAddr; +use std::net::TcpStream; +use std::str::FromStr; + +use tokio::runtime::Runtime; + +pub mod connection; +pub mod friendly_byte_buf; +pub mod packets; +pub mod resolver; +pub mod server_status_pinger; + +#[derive(Debug)] +pub struct ServerAddress { + pub host: String, + pub port: u16, +} + +#[derive(Debug)] +pub struct ServerIpAddress { + pub ip: IpAddr, + pub port: u16, +} + +impl ServerAddress { + /// Convert a Minecraft server address (host:port, the port is optional) to a ServerAddress + pub fn parse(string: &String) -> Result { + if string.is_empty() { + return Err("Empty string".to_string()); + } + let mut parts = string.split(':'); + let host = parts.next().ok_or("No host specified")?.to_string(); + // default the port to 25565 + let port = parts.next().unwrap_or("25565"); + let port = u16::from_str(port).map_err(|_| "Invalid port specified")?; + Ok(ServerAddress { host, port }) + } +} + +pub async fn connect(address: ServerAddress) -> Result<(), Box> { + let resolved_address = resolver::resolve_address(&address).await; + println!("Resolved address: {:?}", resolved_address); + Ok(()) +} + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + let result = 2 + 2; + assert_eq!(result, 4); + } +} -- cgit v1.2.3