Skip to main content

azalea_client/plugins/
block_update.rs

1use azalea_block::BlockState;
2use azalea_core::position::BlockPos;
3use bevy_app::{App, Plugin, Update};
4use bevy_ecs::prelude::*;
5
6use crate::{
7    chunks::handle_receive_chunk_event, interact::BlockStatePredictionHandler,
8    local_player::WorldHolder,
9};
10
11pub struct BlockUpdatePlugin;
12impl Plugin for BlockUpdatePlugin {
13    fn build(&self, app: &mut App) {
14        app.add_systems(
15            Update,
16            // has to be after ReceiveChunkEvent is handled so if we get
17            // chunk+blockupdate in one Update then the block update actually
18            // gets applied
19            handle_block_update_event.after(handle_receive_chunk_event),
20        );
21    }
22}
23
24/// A component that holds the list of block updates that need to be handled.
25///
26/// This is updated by `read_packets` (in `PreUpdate`) and handled/cleared by
27/// [`handle_block_update_event`] (`Update`).
28///
29/// This is a component instead of an ECS event for performance reasons.
30#[derive(Clone, Component, Debug, Default)]
31pub struct QueuedServerBlockUpdates {
32    pub list: Vec<(BlockPos, BlockState)>,
33}
34
35pub fn handle_block_update_event(
36    mut query: Query<(
37        &mut QueuedServerBlockUpdates,
38        &WorldHolder,
39        &mut BlockStatePredictionHandler,
40    )>,
41) {
42    for (mut queued, world_holder, mut prediction_handler) in query.iter_mut() {
43        let world = world_holder.shared.read();
44        for (pos, block_state) in queued.list.drain(..) {
45            if !prediction_handler.update_known_server_state(pos, block_state) {
46                world.chunks.set_block_state(pos, block_state);
47            }
48        }
49    }
50}