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 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<Vec<u8>> for MCString {
@ -275,14 +286,13 @@ pub mod other {
fn into(self) -> Vec<u8> {
// Just output
// {"text": "<data>"}
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::<Vec<u8>>::into(MCVarInt::from(temp.len() as i32)));
out.extend_from_slice(&temp);
out
Into::<MCString>::into(
json!({
"text": self.text.value
})
.to_string(),
)
.into()
}
}
impl MCChat {

View File

@ -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::<ServerboundChatMessage>().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::<i32>::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()
}
}