aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src/position.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2025-08-10 18:55:23 -0500
committerGitHub <noreply@github.com>2025-08-10 18:55:23 -0500
commit7120842f9d2c659a2f12d8922299c2a761bc5582 (patch)
tree0d7976ceec82d914e4c75f23adcdd5839f9960a4 /azalea-core/src/position.rs
parent3b659833c1ad4cca89b4cd553193edcb6d223163 (diff)
downloadazalea-drasl-7120842f9d2c659a2f12d8922299c2a761bc5582.tar.xz
Send correct data component checksums (#234)
* start implementing data component crc32 hashes * start doing serde impls for checksums * make more components hashable * make all data components serializable * support recursive components * fix simdnbt dep * update changelog * clippy
Diffstat (limited to 'azalea-core/src/position.rs')
-rw-r--r--azalea-core/src/position.rs39
1 files changed, 29 insertions, 10 deletions
diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs
index dd6a37e0..8ba381a5 100644
--- a/azalea-core/src/position.rs
+++ b/azalea-core/src/position.rs
@@ -13,8 +13,12 @@ use std::{
};
use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError};
+use serde::{Serialize, Serializer};
+use simdnbt::Deserialize;
-use crate::{direction::Direction, math, resource_location::ResourceLocation};
+use crate::{
+ codec_utils::IntArray, direction::Direction, math, resource_location::ResourceLocation,
+};
macro_rules! vec3_impl {
($name:ident, $type:ty) => {
@@ -295,8 +299,7 @@ macro_rules! vec3_impl {
/// Used to represent an exact position in the world where an entity could be.
///
/// For blocks, [`BlockPos`] is used instead.
-#[derive(Clone, Copy, Debug, Default, PartialEq, AzBuf)]
-#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
+#[derive(Clone, Copy, Debug, Default, PartialEq, AzBuf, serde::Deserialize, serde::Serialize)]
pub struct Vec3 {
pub x: f64,
pub y: f64,
@@ -362,7 +365,6 @@ impl Vec3 {
///
/// For entities (if the coordinates are floating-point), use [`Vec3`] instead.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
-#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct BlockPos {
pub x: i32,
pub y: i32,
@@ -425,6 +427,24 @@ impl BlockPos {
(self - other).length()
}
}
+impl serde::Serialize for BlockPos {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ // makes sure it gets serialized correctly for the checksum
+ IntArray([self.x, self.y, self.z]).serialize(serializer)
+ }
+}
+impl<'de> serde::Deserialize<'de> for BlockPos {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ let [x, y, z] = <[i32; 3]>::deserialize(deserializer)?;
+ Ok(BlockPos { x, y, z })
+ }
+}
/// Similar to [`BlockPos`] but it's serialized as 3 varints instead of one
/// 64-bit integer, so it can represent a bigger range of numbers.
@@ -661,10 +681,10 @@ impl From<ChunkSectionBlockPos> for u16 {
impl nohash_hasher::IsEnabled for ChunkSectionBlockPos {}
/// A block pos with an attached world
-#[derive(Debug, Clone, PartialEq)]
+#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct GlobalPos {
// this is actually a ResourceKey in Minecraft, but i don't think it matters?
- pub world: ResourceLocation,
+ pub dimension: ResourceLocation,
pub pos: BlockPos,
}
@@ -819,8 +839,7 @@ impl fmt::Display for Vec3 {
}
/// A 2D vector.
-#[derive(Clone, Copy, Debug, Default, PartialEq, AzBuf)]
-#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
+#[derive(Clone, Copy, Debug, Default, PartialEq, AzBuf, Deserialize, Serialize)]
pub struct Vec2 {
pub x: f32,
pub y: f32,
@@ -900,7 +919,7 @@ impl AzaleaRead for BlockPos {
impl AzaleaRead for GlobalPos {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(GlobalPos {
- world: ResourceLocation::azalea_read(buf)?,
+ dimension: ResourceLocation::azalea_read(buf)?,
pos: BlockPos::azalea_read(buf)?,
})
}
@@ -929,7 +948,7 @@ impl AzaleaWrite for BlockPos {
impl AzaleaWrite for GlobalPos {
fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> {
- ResourceLocation::azalea_write(&self.world, buf)?;
+ ResourceLocation::azalea_write(&self.dimension, buf)?;
BlockPos::azalea_write(&self.pos, buf)?;
Ok(())