From ba911a8a207eb47df7a055410570767b2e33c2ae Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 11 Dec 2021 15:17:42 -0600 Subject: correct minecraft-chat :tada: --- minecraft-protocol/Cargo.toml | 7 ++-- minecraft-protocol/src/connection.rs | 6 +-- minecraft-protocol/src/lib.rs | 2 +- minecraft-protocol/src/mc_buf.rs | 8 ++-- .../status/clientbound_status_response_packet.rs | 45 ++++++++++------------ minecraft-protocol/src/resolver.rs | 3 +- minecraft-protocol/src/server_status_pinger.rs | 14 ++++++- 7 files changed, 45 insertions(+), 40 deletions(-) (limited to 'minecraft-protocol') 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 { // 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 { + pub fn parse(string: &str) -> Result { 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( pub fn write_varint(buf: &mut Vec, 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, 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( Ok(string) } -pub fn write_utf_with_len(buf: &mut Vec, string: &String, len: usize) { +pub fn write_utf_with_len(buf: &mut Vec, 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( read_utf_with_len(buf, MAX_STRING_LENGTH.into()).await } -pub fn write_utf(buf: &mut Vec, string: &String) { +pub fn write_utf(buf: &mut Vec, 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, +#[derive(Clone, Debug, Deserialize)] +pub struct Players { + pub max: u32, + pub online: u32, + pub sample: Vec, } -#[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 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(()) -- cgit v1.2.3