1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

handle SectionBlocksUpdate packet

This commit is contained in:
mat 2022-10-08 01:01:54 -05:00
parent 82c04004db
commit 4a0d5e7e96
5 changed files with 28 additions and 9 deletions

View file

@ -1,7 +1,7 @@
use crate::{movement::MoveDirection, Account, Player};
use azalea_auth::game_profile::GameProfile;
use azalea_chat::component::Component;
use azalea_core::{ChunkPos, ResourceLocation, Vec3};
use azalea_core::{BlockPos, ChunkPos, ResourceLocation, Vec3};
use azalea_protocol::{
connect::{Connection, ConnectionError, ReadConnection, WriteConnection},
packets::{
@ -631,7 +631,10 @@ impl Client {
}
ClientboundGamePacket::SectionBlocksUpdate(p) => {
debug!("Got section blocks update packet {:?}", p);
// TODO: update world
let mut dimension = client.dimension.lock();
for state in &p.states {
dimension.set_block_state(&(p.section_pos + state.pos), state.state);
}
}
ClientboundGamePacket::GameEvent(p) => {
debug!("Got game event packet {:?}", p);

View file

@ -172,6 +172,18 @@ impl ChunkSectionBlockPos {
}
}
impl Add<ChunkSectionBlockPos> for ChunkSectionPos {
type Output = BlockPos;
fn add(self, rhs: ChunkSectionBlockPos) -> Self::Output {
BlockPos {
x: self.x * 16 + rhs.x as i32,
y: self.y * 16 + rhs.y as i32,
z: self.z * 16 + rhs.z as i32,
}
}
}
/// A block pos with an attached dimension
#[derive(Debug, Clone)]
pub struct GlobalPos {
@ -403,8 +415,8 @@ mod tests {
fn test_read_blockpos_from() {
let mut buf = Vec::new();
13743895338965u64.write_into(&mut buf).unwrap();
let buf = &mut &buf[..];
let block_pos = BlockPos::read_from(buf).unwrap();
let mut buf = Cursor::new(&buf[..]);
let block_pos = BlockPos::read_from(&mut buf).unwrap();
assert_eq!(block_pos, BlockPos::new(49, -43, -3));
}
}

View file

@ -94,7 +94,7 @@ mod tests {
.write_into(&mut buf)
.unwrap();
let mut buf = &mut &buf[..];
let mut buf = Cursor::new(&buf[..]);
assert_eq!(
ResourceLocation::read_from(&mut buf).unwrap(),

View file

@ -1,5 +1,7 @@
use azalea_buf::{BufReadError, McBuf};
use azalea_buf::{McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable};
use azalea_block::BlockState;
use azalea_buf::{
BufReadError, McBuf, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable,
};
use azalea_core::{ChunkSectionBlockPos, ChunkSectionPos};
use azalea_protocol_macros::ClientboundGamePacket;
use std::io::{Cursor, Write};
@ -14,7 +16,7 @@ pub struct ClientboundSectionBlocksUpdatePacket {
#[derive(Clone, Debug)]
pub struct BlockStateWithPosition {
pub pos: ChunkSectionBlockPos,
pub state: u32,
pub state: BlockState,
}
impl McBufReadable for BlockStateWithPosition {
@ -22,6 +24,8 @@ impl McBufReadable for BlockStateWithPosition {
let data = u64::var_read_from(buf)?;
let position_part = data & 4095;
let state = (data >> 12) as u32;
let state = BlockState::try_from(state)
.map_err(|_| BufReadError::UnexpectedEnumVariant { id: state as i32 })?;
let pos = ChunkSectionBlockPos {
x: (position_part >> 8 & 15) as u8,
y: (position_part & 15) as u8,

View file

@ -12,7 +12,7 @@ use std::{
io::{Cursor, Read},
};
use thiserror::Error;
use tokio::io::{AsyncRead, AsyncReadExt};
use tokio::io::AsyncRead;
use tokio_util::codec::{BytesCodec, FramedRead};
#[derive(Error, Debug)]