feat(bedrock): add block update packet
This commit is contained in:
1 parent
7481a6e703
commit
3399f51671
3 files changed
+76
-2
No files matched your search
@@ -37,6 +37,7 @@ pub mod start_game;
|
||||
pub mod transfer;
|
||||
pub mod update_abilities;
|
||||
pub mod update_attributes;
|
||||
pub mod update_block;
|
||||
|
||||
pub use add_actor::*;
|
||||
pub use add_player::*;
|
||||
@@ -77,3 +78,4 @@ pub use start_game::*;
|
||||
pub use transfer::*;
|
||||
pub use update_abilities::*;
|
||||
pub use update_attributes::*;
|
||||
pub use update_block::*;
|
||||
@@ -0,0 +1,36 @@
|
||||
use std::io::{Error, Write};
|
||||
|
||||
use pumpkin_macros::packet;
|
||||
use pumpkin_util::math::position::BlockPos;
|
||||
|
||||
use crate::{codec::var_uint::VarUInt, serial::PacketWrite};
|
||||
|
||||
#[packet(21)]
|
||||
pub struct CUpdateBlock {
|
||||
pub position: BlockPos,
|
||||
pub block_runtime_id: VarUInt,
|
||||
pub flags: VarUInt,
|
||||
pub layer: VarUInt,
|
||||
}
|
||||
|
||||
impl CUpdateBlock {
|
||||
#[must_use]
|
||||
pub const fn new(position: BlockPos, block_runtime_id: u32) -> Self {
|
||||
Self {
|
||||
position,
|
||||
block_runtime_id: VarUInt(block_runtime_id),
|
||||
flags: VarUInt(0x3), // neighbors | network
|
||||
layer: VarUInt(0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PacketWrite for CUpdateBlock {
|
||||
fn write<W: Write>(&self, writer: &mut W) -> Result<(), Error> {
|
||||
self.position.write(writer)?;
|
||||
self.block_runtime_id.write(writer)?;
|
||||
self.flags.write(writer)?;
|
||||
self.layer.write(writer)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -976,12 +976,48 @@ impl World {
|
||||
let chunk_pos = Vector2::new(chunk_section.x, chunk_section.z);
|
||||
if updates.len() == 1 {
|
||||
let (block_pos, block_state_id) = updates[0];
|
||||
self.broadcast_to_chunk(
|
||||
let be_block_id = BlockState::to_be_network_id(block_state_id);
|
||||
self.broadcast_to_chunk_editioned_sync(
|
||||
chunk_pos,
|
||||
&CBlockUpdate::new(block_pos, i32::from(block_state_id).into()),
|
||||
&pumpkin_protocol::bedrock::client::CUpdateBlock::new(
|
||||
block_pos,
|
||||
be_block_id as u32,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
self.broadcast_to_chunk(chunk_pos, &CMultiBlockUpdate::new(&updates));
|
||||
let players = self.players.load();
|
||||
let mut java_recipients = Vec::new();
|
||||
|
||||
let recipients = players.iter().filter(|p| {
|
||||
let center = p.living_entity.entity.chunk_pos.load();
|
||||
let view_distance = get_view_distance(p).get() as i32;
|
||||
is_within_view_distance(chunk_pos, center, view_distance)
|
||||
});
|
||||
|
||||
for p in recipients {
|
||||
match &p.client {
|
||||
ClientPlatform::Java(_) => java_recipients.push(p),
|
||||
ClientPlatform::Bedrock(be_client) => {
|
||||
for (block_pos, block_state_id) in &updates {
|
||||
let be_block_id = BlockState::to_be_network_id(*block_state_id);
|
||||
be_client.try_enqueue_packet(
|
||||
&pumpkin_protocol::bedrock::client::CUpdateBlock::new(
|
||||
*block_pos,
|
||||
be_block_id as u32,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let recipients_by_version =
|
||||
Self::collect_java_recipients_by_version(java_recipients.into_iter());
|
||||
Self::broadcast_java_grouped(
|
||||
&CMultiBlockUpdate::new(&updates),
|
||||
recipients_by_version,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user