aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/mc_buf/read.rs
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-04-24 16:18:51 -0500
committermat <github@matdoes.dev>2022-04-24 16:18:51 -0500
commitb7641ff308aab7840d2a2253ae50f8ee496b2a97 (patch)
treef33eb83f119da5aae08f56a5c7543db15594b01d /azalea-protocol/src/mc_buf/read.rs
parent97d392f4e5721d8aa8940f253918965ff0b40348 (diff)
downloadazalea-drasl-b7641ff308aab7840d2a2253ae50f8ee496b2a97.tar.xz
1.18.2 support
Diffstat (limited to 'azalea-protocol/src/mc_buf/read.rs')
-rwxr-xr-xazalea-protocol/src/mc_buf/read.rs21
1 files changed, 19 insertions, 2 deletions
diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs
index e036643e..1e031916 100755
--- a/azalea-protocol/src/mc_buf/read.rs
+++ b/azalea-protocol/src/mc_buf/read.rs
@@ -1,7 +1,9 @@
use async_trait::async_trait;
+use azalea_chat::component::Component;
use azalea_core::{
difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation,
};
+use serde::Deserialize;
use tokio::io::{AsyncRead, AsyncReadExt};
use super::MAX_STRING_LENGTH;
@@ -41,12 +43,12 @@ where
Ok(list)
}
- // fast varints stolen from https://github.com/luojia65/mc-varint/blob/master/src/lib.rs#L67
+ // fast varints modified from https://github.com/luojia65/mc-varint/blob/master/src/lib.rs#L67
/// Read a single varint from the reader and return the value, along with the number of bytes read
async fn read_varint(&mut self) -> Result<i32, String> {
let mut buffer = [0];
let mut ans = 0;
- for i in 0..4 {
+ for i in 0..5 {
self.read_exact(&mut buffer)
.await
.map_err(|_| "Invalid VarInt".to_string())?;
@@ -440,3 +442,18 @@ impl McBufReadable for Difficulty {
Ok(Difficulty::by_id(u8::read_into(buf).await?))
}
}
+
+// Component
+#[async_trait]
+impl McBufReadable for Component {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ let string = buf.read_utf().await?;
+ let json: serde_json::Value = serde_json::from_str(string.as_str())
+ .map_err(|e| "Component isn't valid JSON".to_string())?;
+ let component = Component::deserialize(json).map_err(|e| e.to_string())?;
+ Ok(component)
+ }
+}