Serverbound chat fixed by fixing MCChat

This commit is contained in:
Garen Tyler 2021-03-19 23:27:17 -06:00
parent 3fee4aaf18
commit 1010258e9e
2 changed files with 33 additions and 19 deletions

View File

@ -3,6 +3,7 @@
pub use functions::*; pub use functions::*;
pub use numbers::*; pub use numbers::*;
pub use other::*; pub use other::*;
use serde_json::json;
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream; use tokio::net::TcpStream;
@ -68,7 +69,7 @@ pub mod functions {
pub mod other { pub mod other {
use super::*; use super::*;
use std::convert::{From, Into, TryFrom}; use std::convert::{From, Into, TryFrom};
use std::fmt::Display; use std::fmt::{Debug, Display};
/// The equivalent of a `bool`. /// The equivalent of a `bool`.
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq)]
@ -133,7 +134,7 @@ pub mod other {
} }
/// The equivalent of a `String`. /// The equivalent of a `String`.
#[derive(Debug, PartialEq)] #[derive(PartialEq)]
pub struct MCString { pub struct MCString {
pub value: String, 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 { impl Display for MCString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 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<Vec<u8>> for MCString { impl TryFrom<Vec<u8>> for MCString {
@ -275,14 +286,13 @@ pub mod other {
fn into(self) -> Vec<u8> { fn into(self) -> Vec<u8> {
// Just output // Just output
// {"text": "<data>"} // {"text": "<data>"}
let mut out = vec![]; Into::<MCString>::into(
let mut temp = vec![]; json!({
temp.extend_from_slice(&"{\"text\": \"".to_owned().into_bytes()); "text": self.text.value
temp.extend_from_slice(&self.text.value.into_bytes()); })
temp.extend_from_slice(&"\"}".to_owned().into_bytes()); .to_string(),
out.extend_from_slice(&Into::<Vec<u8>>::into(MCVarInt::from(temp.len() as i32))); )
out.extend_from_slice(&temp); .into()
out
} }
} }
impl MCChat { impl MCChat {

View File

@ -2,7 +2,7 @@
pub mod packets; pub mod packets;
use crate::{mctypes::*, CONFIG, FAVICON}; use crate::{mctypes::*, CONFIG, FAVICON};
use log::debug; use log::{debug, info};
use packets::*; use packets::*;
use serde_json::json; use serde_json::json;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -186,12 +186,9 @@ impl NetworkClient {
} else if packet_id == ServerboundChatMessage::id() { } else if packet_id == ServerboundChatMessage::id() {
let serverboundchatmessage = let serverboundchatmessage =
self.get_packet::<ServerboundChatMessage>().await?; self.get_packet::<ServerboundChatMessage>().await?;
self.send_chat_message(format!( let reply = format!("<{}> {}", self.get_name(), serverboundchatmessage.text);
"<{}> {}", info!("{}", reply);
self.username.as_ref().unwrap_or(&"unknown".to_owned()), self.send_chat_message(reply).await?;
serverboundchatmessage.text
))
.await?;
} else { } else {
let _ = read_bytes(&mut self.stream, Into::<i32>::into(packet_length) as usize) let _ = read_bytes(&mut self.stream, Into::<i32>::into(packet_length) as usize)
.await?; .await?;
@ -250,7 +247,7 @@ impl NetworkClient {
} }
/// Send a keep alive packet to the client. /// 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. // Keep alive ping to client.
let clientboundkeepalive = KeepAlivePing::new(); let clientboundkeepalive = KeepAlivePing::new();
self.send_packet(clientboundkeepalive).await?; self.send_packet(clientboundkeepalive).await?;
@ -260,4 +257,11 @@ impl NetworkClient {
self.last_keep_alive = Instant::now(); self.last_keep_alive = Instant::now();
Ok(()) Ok(())
} }
pub fn get_name(&self) -> String {
self.username
.as_ref()
.unwrap_or(&"unknown".to_owned())
.to_string()
}
} }