Skip to content

Commit

Permalink
Add WorldPosition struct for packets
Browse files Browse the repository at this point in the history
Add WorldPosition struct that deserialises according to:
https://wiki.vg/Data_types#Type:Position
  • Loading branch information
lukas0008 committed Aug 13, 2024
1 parent 03f9b37 commit cce0161
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pumpkin-protocol/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use std::io::{self, Write};
#![feature(test)]
extern crate test;

use bytebuf::{packet_id::Packet, ByteBuffer, DeserializerError};
use bytes::Buf;
use serde::{Deserialize, Serialize, Serializer};
use std::io::{self, Write};
use thiserror::Error;

pub mod bytebuf;
pub mod client;
pub mod server;
pub mod uuid;

pub mod packet_decoder;
pub mod packet_encoder;
pub mod server;
pub mod uuid;
pub mod position;

pub const CURRENT_MC_PROTOCOL: u32 = 767;

Expand Down
31 changes: 31 additions & 0 deletions pumpkin-protocol/src/position.rs
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)
}
}

0 comments on commit cce0161

Please sign in to comment.