diff options
| author | mat <github@matdoes.dev> | 2021-12-11 15:17:42 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2021-12-11 15:17:42 -0600 |
| commit | ba911a8a207eb47df7a055410570767b2e33c2ae (patch) | |
| tree | 4a53d384f08b7272ba287bdb326f3d7fcb09f289 /minecraft-protocol | |
| parent | 6026c74430f311c9217b77e7ac07d183efde5bce (diff) | |
| download | azalea-drasl-ba911a8a207eb47df7a055410570767b2e33c2ae.tar.xz | |
correct minecraft-chat :tada:
Diffstat (limited to 'minecraft-protocol')
| -rw-r--r-- | minecraft-protocol/Cargo.toml | 7 | ||||
| -rw-r--r-- | minecraft-protocol/src/connection.rs | 6 | ||||
| -rw-r--r-- | minecraft-protocol/src/lib.rs | 2 | ||||
| -rw-r--r-- | minecraft-protocol/src/mc_buf.rs | 8 | ||||
| -rw-r--r-- | minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs | 45 | ||||
| -rw-r--r-- | minecraft-protocol/src/resolver.rs | 3 | ||||
| -rw-r--r-- | minecraft-protocol/src/server_status_pinger.rs | 14 |
7 files changed, 45 insertions, 40 deletions
diff --git a/minecraft-protocol/Cargo.toml b/minecraft-protocol/Cargo.toml index 7894ecfd..3cbf663b 100644 --- a/minecraft-protocol/Cargo.toml +++ b/minecraft-protocol/Cargo.toml @@ -7,12 +7,13 @@ version = "0.1.0" [dependencies] async-recursion = "^0.3.2" +async-trait = "0.1.51" byteorder = "^1.4.3" bytes = "^1.1.0" +minecraft-chat = {path = "../minecraft-chat"} +serde = {version = "1.0.130", features = ["serde_derive"]} +serde_json = "^1.0.72" thiserror = "^1.0.30" tokio = {version = "^1.14.0", features = ["io-util", "net", "macros"]} tokio-util = "^0.6.9" trust-dns-resolver = "^0.20.3" -async-trait = "0.1.51" -minecraft-chat = { path = "../minecraft-chat" } -serde_json = "^1.0.72" diff --git a/minecraft-protocol/src/connection.rs b/minecraft-protocol/src/connection.rs index cfca403c..2fe03dfb 100644 --- a/minecraft-protocol/src/connection.rs +++ b/minecraft-protocol/src/connection.rs @@ -45,7 +45,7 @@ impl Connection { self.state = state; } - pub async fn read_packet(&mut self) -> Result<(), String> { + pub async fn read_packet(&mut self) -> Result<Packet, String> { // what this does: // 1. reads the first 5 bytes, probably only some of this will be used to get the packet length // 2. how much we should read = packet length - 5 @@ -69,9 +69,7 @@ impl Connection { ) .await?; - println!("packet: {:?}", packet); - - Ok(()) + Ok(packet) } /// Write a packet to the server diff --git a/minecraft-protocol/src/lib.rs b/minecraft-protocol/src/lib.rs index 88b3603f..aaf3da50 100644 --- a/minecraft-protocol/src/lib.rs +++ b/minecraft-protocol/src/lib.rs @@ -23,7 +23,7 @@ pub struct ServerIpAddress { impl ServerAddress { /// Convert a Minecraft server address (host:port, the port is optional) to a ServerAddress - pub fn parse(string: &String) -> Result<ServerAddress, String> { + pub fn parse(string: &str) -> Result<ServerAddress, String> { if string.is_empty() { return Err("Empty string".to_string()); } diff --git a/minecraft-protocol/src/mc_buf.rs b/minecraft-protocol/src/mc_buf.rs index a9ad1642..6c812058 100644 --- a/minecraft-protocol/src/mc_buf.rs +++ b/minecraft-protocol/src/mc_buf.rs @@ -49,7 +49,7 @@ pub async fn read_varint<T: AsyncRead + std::marker::Unpin>( pub fn write_varint(buf: &mut Vec<u8>, mut value: i32) { let mut buffer = [0]; if value == 0 { - buf.write(&buffer).unwrap(); + buf.write_all(&buffer).unwrap(); } while value != 0 { buffer[0] = (value & 0b0111_1111) as u8; @@ -57,7 +57,7 @@ pub fn write_varint(buf: &mut Vec<u8>, mut value: i32) { if value != 0 { buffer[0] |= 0b1000_0000; } - buf.write(&buffer).unwrap(); + buf.write_all(&buffer).unwrap(); } } @@ -134,7 +134,7 @@ pub async fn read_utf_with_len<T: AsyncRead + std::marker::Unpin>( Ok(string) } -pub fn write_utf_with_len(buf: &mut Vec<u8>, string: &String, len: usize) { +pub fn write_utf_with_len(buf: &mut Vec<u8>, string: &str, len: usize) { if string.len() > len { panic!( "String too big (was {} bytes encoded, max {})", @@ -152,7 +152,7 @@ pub async fn read_utf<T: AsyncRead + std::marker::Unpin>( read_utf_with_len(buf, MAX_STRING_LENGTH.into()).await } -pub fn write_utf(buf: &mut Vec<u8>, string: &String) { +pub fn write_utf(buf: &mut Vec<u8>, string: &str) { write_utf_with_len(buf, string, MAX_STRING_LENGTH.into()); } diff --git a/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs b/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs index 20db9fe1..0868a062 100644 --- a/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs +++ b/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs @@ -1,5 +1,6 @@ use async_trait::async_trait; use minecraft_chat::component::Component; +use serde::{Deserialize, Deserializer}; use serde_json::Value; use tokio::io::BufReader; @@ -8,29 +9,29 @@ use crate::{ packets::{Packet, PacketTrait}, }; -#[derive(Clone, Debug)] -struct Version { - name: String, - protocol: u32, +#[derive(Clone, Debug, Deserialize)] +pub struct Version { + pub name: String, + pub protocol: u32, } -#[derive(Clone, Debug)] -struct SamplePlayer { - id: String, - name: String, +#[derive(Clone, Debug, Deserialize)] +pub struct SamplePlayer { + pub id: String, + pub name: String, } -#[derive(Clone, Debug)] -struct Players { - max: u32, - online: u32, - sample: Vec<SamplePlayer>, +#[derive(Clone, Debug, Deserialize)] +pub struct Players { + pub max: u32, + pub online: u32, + pub sample: Vec<SamplePlayer>, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Deserialize)] pub struct ClientboundStatusResponsePacket { - // version: Version, - description: Component, + pub version: Version, + pub description: Component, } #[async_trait] @@ -47,13 +48,9 @@ impl PacketTrait for ClientboundStatusResponsePacket { let status_string = mc_buf::read_utf(buf).await?; let status_json: Value = serde_json::from_str(status_string.as_str()).expect("Server status isn't valid JSON"); - let description_string: &Value = status_json.get("description").unwrap(); - - // this.status = GsonHelper.fromJson(GSON, friendlyByteBuf.readUtf(32767), ServerStatus.class); - Ok(ClientboundStatusResponsePacket { - // version: status_json.get("version"), - description: Component::new(description_string)?, - } - .get()) + + Ok(ClientboundStatusResponsePacket::deserialize(status_json) + .map_err(|e| e.to_string())? + .get()) } } diff --git a/minecraft-protocol/src/resolver.rs b/minecraft-protocol/src/resolver.rs index b751e05f..24687a6e 100644 --- a/minecraft-protocol/src/resolver.rs +++ b/minecraft-protocol/src/resolver.rs @@ -29,8 +29,7 @@ pub async fn resolve_address(address: &ServerAddress) -> Result<ServerIpAddress, .await; // if it resolves that means it's a redirect so we call resolve_address again with the new host - if srv_redirect_result.is_ok() { - let redirect_result = srv_redirect_result.unwrap(); + if let Ok(redirect_result) = srv_redirect_result { let redirect_srv = redirect_result .iter() .next() diff --git a/minecraft-protocol/src/server_status_pinger.rs b/minecraft-protocol/src/server_status_pinger.rs index 0e12a6a7..ae41ed51 100644 --- a/minecraft-protocol/src/server_status_pinger.rs +++ b/minecraft-protocol/src/server_status_pinger.rs @@ -3,7 +3,7 @@ use crate::{ packets::{ handshake::client_intention_packet::ClientIntentionPacket, status::serverbound_status_request_packet::ServerboundStatusRequestPacket, - ConnectionProtocol, PacketTrait, + ConnectionProtocol, Packet, PacketTrait, }, resolver, ServerAddress, }; @@ -33,7 +33,17 @@ pub async fn ping_server(address: &ServerAddress) -> Result<(), String> { conn.send_packet(ServerboundStatusRequestPacket {}.get()) .await; - conn.read_packet().await.unwrap(); + let packet = conn.read_packet().await.unwrap(); + + match packet { + Packet::ClientboundStatusResponsePacket(p) => { + println!("{:?}", p); + println!("{}", p.description.to_ansi(None)); + } + _ => { + println!("unexpected packet {:?}", packet); + } + } Ok(()) |
