Add WorldPosition struct for packets

Add WorldPosition struct that deserialises according to:
https://wiki.vg/Data_types#Type:Position
This commit is contained in:
lukas0008 2024-08-13 23:56:40 +02:00
parent 03f9b377d5
commit cce0161e3d
2 changed files with 37 additions and 4 deletions

View File

@ -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;

View File

@ -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)
}
}