diff options
-rw-r--r-- | src/types.rs | 1 | ||||
-rw-r--r-- | src/v1_16_3.rs | 57 |
2 files changed, 56 insertions, 2 deletions
diff --git a/src/types.rs b/src/types.rs index 3a96b22..3c317a2 100644 --- a/src/types.rs +++ b/src/types.rs @@ -10,7 +10,6 @@ pub use super::chat::*; #[cfg(all(test, feature = "std"))] use crate::protocol::TestRandom; use crate::byte_order::{ProtoByteOrder, ByteOrder}; -use std::ops::Deref; // bool impl Serialize for bool { diff --git a/src/v1_16_3.rs b/src/v1_16_3.rs index 3eb5e03..a3c4e68 100644 --- a/src/v1_16_3.rs +++ b/src/v1_16_3.rs @@ -244,7 +244,7 @@ define_protocol!(753, Packet753, RawPacket753, RawPacket753Body => { entity_id: i32, is_hardcore: bool, gamemode: GameMode, - previous_gamemode: GameMode, + previous_gamemode: PreviousGameMode, worlds: CountedArray<String, VarInt>, dimension_codec: NamedNbtTag, dimension: NamedNbtTag, @@ -1636,6 +1636,61 @@ proto_byte_enum!(GameMode, 0x03 :: Spectator ); +#[derive(Clone, Debug, PartialEq)] +pub enum PreviousGameMode { + NoPrevious, + Previous(GameMode) +} + +impl PreviousGameMode { + pub fn id(&self) -> i8 { + use PreviousGameMode::*; + match self { + NoPrevious => -1, + Previous(mode) => mode.id() as i8, + } + } +} + +impl Serialize for PreviousGameMode { + fn mc_serialize<S: Serializer>(&self, to: &mut S) -> SerializeResult { + to.serialize_byte(self.id() as u8) + } +} + +impl Deserialize for PreviousGameMode { + fn mc_deserialize(data: &[u8]) -> DeserializeResult<'_, Self> { + let Deserialized{ value: id, data } = i8::mc_deserialize(data)?; + + use PreviousGameMode::*; + match id { + -1 => Deserialized::ok(NoPrevious, data), + other => Ok(GameMode::deserialize_with_id(other as u8, data)?.map(move |gm| Previous(gm))) + } + } +} + +impl Into<Option<GameMode>> for PreviousGameMode { + fn into(self) -> Option<GameMode> { + use PreviousGameMode::*; + match self { + NoPrevious => None, + Previous(mode) => Some(mode), + } + } +} + +#[cfg(all(test, feature = "std"))] +impl TestRandom for PreviousGameMode { + fn test_gen_random() -> Self { + use PreviousGameMode::*; + match <Option<GameMode> as TestRandom>::test_gen_random() { + Some(gamemode) => Previous(gamemode), + None => NoPrevious + } + } +} + proto_byte_enum!(WinGameAction, 0x00 :: Respawn, 0x01 :: RollCreditsAndRespawn |