diff options
Diffstat (limited to 'azalea-protocol')
| -rw-r--r-- | azalea-protocol/src/mc_buf/read.rs | 38 | ||||
| -rw-r--r-- | azalea-protocol/src/mc_buf/write.rs | 19 | ||||
| -rw-r--r-- | azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs | 8 | ||||
| -rw-r--r-- | azalea-protocol/src/packets/game/mod.rs | 15 |
4 files changed, 77 insertions, 3 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) + } +} diff --git a/azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs b/azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs new file mode 100644 index 00000000..e12cfff3 --- /dev/null +++ b/azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs @@ -0,0 +1,8 @@ +use azalea_core::difficulty::Difficulty; +use packet_macros::GamePacket; + +#[derive(Clone, Debug, GamePacket)] +pub struct ClientboundChangeDifficultyPacket { + pub difficulty: Difficulty, + pub locked: bool, +} diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 43b3ca3d..4efe72fb 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -1,3 +1,4 @@ +pub mod clientbound_change_difficulty_packet; pub mod clientbound_custom_payload_packet; pub mod clientbound_login_packet; pub mod clientbound_update_view_distance_packet; @@ -18,12 +19,16 @@ where ClientboundCustomPayloadPacket( clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, ), + ClientboundChangeDifficultyPacket( + clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, + ), } #[async_trait] impl ProtocolPacket for GamePacket { fn id(&self) -> u32 { match self { + GamePacket::ClientboundChangeDifficultyPacket(_packet) => 0x0e, GamePacket::ClientboundCustomPayloadPacket(_packet) => 0x18, GamePacket::ClientboundLoginPacket(_packet) => 0x26, GamePacket::ClientboundUpdateViewDistancePacket(_packet) => 0x4a, @@ -31,7 +36,12 @@ impl ProtocolPacket for GamePacket { } fn write(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { - Ok(()) + match self { + GamePacket::ClientboundChangeDifficultyPacket(packet) => packet.write(buf), + GamePacket::ClientboundCustomPayloadPacket(packet) => packet.write(buf), + GamePacket::ClientboundLoginPacket(packet) => packet.write(buf), + GamePacket::ClientboundUpdateViewDistancePacket(packet) => packet.write(buf), + } } /// Read a packet by its id, ConnectionProtocol, and flow @@ -45,6 +55,9 @@ impl ProtocolPacket for GamePacket { { Ok(match flow { PacketFlow::ServerToClient => match id { + 0x0e => clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket + ::read(buf) + .await?, 0x18 => clientbound_custom_payload_packet::ClientboundCustomPayloadPacket::read(buf).await?, 0x26 => clientbound_login_packet::ClientboundLoginPacket::read(buf).await?, 0x4a => clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket |
