-
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add WorldPosition struct for packets
Add WorldPosition struct that deserialises according to: https://wiki.vg/Data_types#Type:Position
- Loading branch information
Showing
2 changed files
with
37 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use serde::Deserialize; | ||
|
||
pub struct WorldPosition { | ||
x: i32, | ||
y: i32, | ||
z: i32 | ||
} | ||
|
||
impl<'de> Deserialize<'de> for WorldPosition { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de> { | ||
struct Visitor; | ||
impl<'a> serde::de::Visitor<'a> for Visitor { | ||
type Value = WorldPosition; | ||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
formatter.write_str("An i64 int") | ||
} | ||
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> | ||
where | ||
E: serde::de::Error, { | ||
Ok(WorldPosition { | ||
x: (v >> 38) as i32, | ||
y: (v << 52 >> 52) as i32, | ||
z: (v << 26 >> 38) as i32 | ||
}) | ||
} | ||
} | ||
deserializer.deserialize_i64(Visitor) | ||
} | ||
} |