aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src/position.rs
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2023-12-13 22:03:54 -0600
committermat <git@matdoes.dev>2023-12-13 22:03:54 -0600
commitcc0717f45e14233a1002d1cb709aba298f89ef8c (patch)
treeab15156f4dd997d6aff5fd192fdd4f8c3a3c3070 /azalea-core/src/position.rs
parent5b7ed4852c5b47a47a231832d418bad0d69fa563 (diff)
downloadazalea-drasl-cc0717f45e14233a1002d1cb709aba298f89ef8c.tar.xz
i didn't actually commit the code
Diffstat (limited to 'azalea-core/src/position.rs')
-rwxr-xr-xazalea-core/src/position.rs39
1 files changed, 37 insertions, 2 deletions
diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs
index 196a70d5..4cdf3f18 100755
--- a/azalea-core/src/position.rs
+++ b/azalea-core/src/position.rs
@@ -213,7 +213,7 @@ impl BlockPos {
/// Chunk coordinates are used to represent where a chunk is in the world. You
/// can convert the x and z to block coordinates by multiplying them by 16.
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, McBuf)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ChunkPos {
pub x: i32,
pub z: i32,
@@ -233,12 +233,38 @@ impl Add<ChunkPos> for ChunkPos {
}
}
}
+
+// reading ChunkPos is done in reverse, so z first and then x
+// ........
+// mojang why
impl From<ChunkPos> for u64 {
#[inline]
fn from(pos: ChunkPos) -> Self {
- ((pos.x as u64) << 32) | (pos.z as u64)
+ (pos.x as u64) | ((pos.z as u64) << 32)
+ }
+}
+impl From<u64> for ChunkPos {
+ #[inline]
+ fn from(pos: u64) -> Self {
+ ChunkPos {
+ x: (pos) as i32,
+ z: (pos >> 32) as i32,
+ }
+ }
+}
+impl McBufReadable for ChunkPos {
+ fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
+ let long = u64::read_from(buf)?;
+ Ok(ChunkPos::from(long))
}
}
+impl McBufWritable for ChunkPos {
+ fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
+ u64::from(*self).write_into(buf)?;
+ Ok(())
+ }
+}
+
impl Hash for ChunkPos {
#[inline]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
@@ -600,4 +626,13 @@ mod tests {
ChunkSectionBlockPos::new(0, 4, 0)
);
}
+
+ #[test]
+ fn test_read_chunk_pos_from() {
+ let mut buf = Vec::new();
+ ChunkPos::new(2, -1).write_into(&mut buf).unwrap();
+ let mut buf = Cursor::new(&buf[..]);
+ let chunk_pos = ChunkPos::from(u64::read_from(&mut buf).unwrap());
+ assert_eq!(chunk_pos, ChunkPos::new(2, -1));
+ }
}