aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/mc_buf
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-01-03 00:14:41 -0600
committermat <github@matdoes.dev>2022-01-03 00:14:41 -0600
commit96eba2b39a596dd19c29a93aaa3b5bb9b700ba62 (patch)
tree9a585864e81fd01da15bdc9ca5e4e3e7a69eb1f4 /azalea-protocol/src/mc_buf
parent394f996df27bedc68be6c1f9e9764e8f78ba6282 (diff)
downloadazalea-drasl-96eba2b39a596dd19c29a93aaa3b5bb9b700ba62.tar.xz
difficulty packet
Diffstat (limited to 'azalea-protocol/src/mc_buf')
-rw-r--r--azalea-protocol/src/mc_buf/read.rs38
-rw-r--r--azalea-protocol/src/mc_buf/write.rs19
2 files changed, 55 insertions, 2 deletions
diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs
index 5127860e..0fa1d099 100644
--- a/azalea-protocol/src/mc_buf/read.rs
+++ b/azalea-protocol/src/mc_buf/read.rs
@@ -1,5 +1,8 @@
use async_trait::async_trait;
-use azalea_core::{game_type::GameType, resource_location::ResourceLocation};
+use azalea_core::{
+ difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation,
+};
+use num_traits::FromPrimitive;
use tokio::io::{AsyncRead, AsyncReadExt};
use super::MAX_STRING_LENGTH;
@@ -338,6 +341,28 @@ impl McBufReadable for bool {
}
}
+// u8
+#[async_trait]
+impl McBufReadable for u8 {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ buf.read_byte().await
+ }
+}
+
+// i8
+#[async_trait]
+impl McBufReadable for i8 {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ buf.read_byte().await.map(|i| i as i8)
+ }
+}
+
// GameType
#[async_trait]
impl McBufReadable for GameType {
@@ -386,3 +411,14 @@ impl McBufReadable for azalea_nbt::Tag {
buf.read_nbt().await
}
}
+
+// Difficulty
+#[async_trait]
+impl McBufReadable for Difficulty {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ Ok(Difficulty::by_id(u8::read_into(buf).await?))
+ }
+}
diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs
index 6fbe6eab..fd9faeb4 100644
--- a/azalea-protocol/src/mc_buf/write.rs
+++ b/azalea-protocol/src/mc_buf/write.rs
@@ -1,6 +1,9 @@
use async_trait::async_trait;
-use azalea_core::{game_type::GameType, resource_location::ResourceLocation};
+use azalea_core::{
+ difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation,
+};
use byteorder::{BigEndian, WriteBytesExt};
+use num_traits::FromPrimitive;
use std::io::Write;
use super::MAX_STRING_LENGTH;
@@ -255,6 +258,13 @@ impl McBufWritable for bool {
}
}
+// i8
+impl McBufWritable for i8 {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ buf.write_byte(*self as u8)
+ }
+}
+
// GameType
impl McBufWritable for GameType {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
@@ -284,3 +294,10 @@ impl McBufWritable for azalea_nbt::Tag {
buf.write_nbt(self)
}
}
+
+// Difficulty
+impl McBufWritable for Difficulty {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ u8::write_into(&self.id(), buf)
+ }
+}