From 1010258e9eed76a5cbef0023e95167ad2c7a1761 Mon Sep 17 00:00:00 2001 From: Garen Tyler Date: Fri, 19 Mar 2021 23:27:17 -0600 Subject: [PATCH] Serverbound chat fixed by fixing MCChat --- src/mctypes.rs | 32 +++++++++++++++++++++----------- src/server/net/mod.rs | 20 ++++++++++++-------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/mctypes.rs b/src/mctypes.rs index 8b76d2e..9b84337 100644 --- a/src/mctypes.rs +++ b/src/mctypes.rs @@ -3,6 +3,7 @@ pub use functions::*; pub use numbers::*; pub use other::*; +use serde_json::json; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpStream; @@ -68,7 +69,7 @@ pub mod functions { pub mod other { use super::*; use std::convert::{From, Into, TryFrom}; - use std::fmt::Display; + use std::fmt::{Debug, Display}; /// The equivalent of a `bool`. #[derive(Debug, Copy, Clone, PartialEq)] @@ -133,7 +134,7 @@ pub mod other { } /// The equivalent of a `String`. - #[derive(Debug, PartialEq)] + #[derive(PartialEq)] pub struct MCString { pub value: String, } @@ -176,9 +177,19 @@ pub mod other { } } } + impl Debug for MCString { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "MCString {{ \"{}\" ({} chars) }}", + self.value, + self.value.len() + ) + } + } impl Display for MCString { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "\"{}\" ({} chars)", self.value, self.value.len()) + write!(f, "{}", self.value) } } impl TryFrom> for MCString { @@ -275,14 +286,13 @@ pub mod other { fn into(self) -> Vec { // Just output // {"text": ""} - let mut out = vec![]; - let mut temp = vec![]; - temp.extend_from_slice(&"{\"text\": \"".to_owned().into_bytes()); - temp.extend_from_slice(&self.text.value.into_bytes()); - temp.extend_from_slice(&"\"}".to_owned().into_bytes()); - out.extend_from_slice(&Into::>::into(MCVarInt::from(temp.len() as i32))); - out.extend_from_slice(&temp); - out + Into::::into( + json!({ + "text": self.text.value + }) + .to_string(), + ) + .into() } } impl MCChat { diff --git a/src/server/net/mod.rs b/src/server/net/mod.rs index 53d3bea..8ae3f48 100644 --- a/src/server/net/mod.rs +++ b/src/server/net/mod.rs @@ -2,7 +2,7 @@ pub mod packets; use crate::{mctypes::*, CONFIG, FAVICON}; -use log::debug; +use log::{debug, info}; use packets::*; use serde_json::json; use std::time::{Duration, Instant}; @@ -186,12 +186,9 @@ impl NetworkClient { } else if packet_id == ServerboundChatMessage::id() { let serverboundchatmessage = self.get_packet::().await?; - self.send_chat_message(format!( - "<{}> {}", - self.username.as_ref().unwrap_or(&"unknown".to_owned()), - serverboundchatmessage.text - )) - .await?; + let reply = format!("<{}> {}", self.get_name(), serverboundchatmessage.text); + info!("{}", reply); + self.send_chat_message(reply).await?; } else { let _ = read_bytes(&mut self.stream, Into::::into(packet_length) as usize) .await?; @@ -250,7 +247,7 @@ impl NetworkClient { } /// Send a keep alive packet to the client. - async fn keep_alive(&mut self) -> tokio::io::Result<()> { + pub async fn keep_alive(&mut self) -> tokio::io::Result<()> { // Keep alive ping to client. let clientboundkeepalive = KeepAlivePing::new(); self.send_packet(clientboundkeepalive).await?; @@ -260,4 +257,11 @@ impl NetworkClient { self.last_keep_alive = Instant::now(); Ok(()) } + + pub fn get_name(&self) -> String { + self.username + .as_ref() + .unwrap_or(&"unknown".to_owned()) + .to_string() + } }