Handle block updates from the server
This commit is contained in:
parent
480b7afd3b
commit
646381968b
|
@ -385,7 +385,7 @@ state_packets!(
|
|||
// MultiBlockChange is used to update a batch of blocks in a single packet.
|
||||
MultiBlockChange {
|
||||
chunk_x: i32 =,
|
||||
chunk_y: i32 =,
|
||||
chunk_z: i32 =,
|
||||
records: LenPrefixed<VarInt, packet::BlockChangeRecord> =,
|
||||
}
|
||||
// ConfirmTransaction notifies the client whether a transaction was successful
|
||||
|
|
|
@ -286,6 +286,8 @@ impl Server {
|
|||
KeepAliveClientbound => on_keep_alive,
|
||||
ChunkData => on_chunk_data,
|
||||
ChunkUnload => on_chunk_unload,
|
||||
BlockChange => on_block_change,
|
||||
MultiBlockChange => on_multi_block_change,
|
||||
TeleportPlayer => on_teleport,
|
||||
TimeUpdate => on_time_update,
|
||||
ChangeGameState => on_game_state_change,
|
||||
|
@ -485,6 +487,28 @@ impl Server {
|
|||
fn on_chunk_unload(&mut self, chunk_unload: packet::play::clientbound::ChunkUnload) {
|
||||
self.world.unload_chunk(chunk_unload.x, chunk_unload.z);
|
||||
}
|
||||
|
||||
fn on_block_change(&mut self, block_change: packet::play::clientbound::BlockChange) {
|
||||
self.world.set_block(
|
||||
block_change.location.get_x(),
|
||||
block_change.location.get_y(),
|
||||
block_change.location.get_z(),
|
||||
block::Block::by_vanilla_id(block_change.block_id.0 as usize)
|
||||
);
|
||||
}
|
||||
|
||||
fn on_multi_block_change(&mut self, block_change: packet::play::clientbound::MultiBlockChange) {
|
||||
let ox = block_change.chunk_x << 4;
|
||||
let oz = block_change.chunk_z << 4;
|
||||
for record in block_change.records.data {
|
||||
self.world.set_block(
|
||||
ox + (record.xz >> 4) as i32,
|
||||
record.y as i32,
|
||||
oz + (record.xz & 0xF) as i32,
|
||||
block::Block::by_vanilla_id(record.block_id.0 as usize)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
|
|
|
@ -25,20 +25,20 @@ pub struct Position(u64);
|
|||
|
||||
impl Position {
|
||||
#[allow(dead_code)]
|
||||
fn new(x: i32, y: i32, z: i32) -> Position {
|
||||
pub fn new(x: i32, y: i32, z: i32) -> Position {
|
||||
Position((((x as u64) & 0x3FFFFFF) << 38) | (((y as u64) & 0xFFF) << 26) |
|
||||
((z as u64) & 0x3FFFFFF))
|
||||
}
|
||||
|
||||
fn get_x(&self) -> i32 {
|
||||
pub fn get_x(&self) -> i32 {
|
||||
((self.0 as i64) >> 38) as i32
|
||||
}
|
||||
|
||||
fn get_y(&self) -> i32 {
|
||||
pub fn get_y(&self) -> i32 {
|
||||
(((self.0 as i64) >> 26) & 0xFFF) as i32
|
||||
}
|
||||
|
||||
fn get_z(&self) -> i32 {
|
||||
pub fn get_z(&self) -> i32 {
|
||||
((self.0 as i64) << 38 >> 38) as i32
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue