1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use valence_math::Vec3;
use valence_protocol::packets::play::PlayerInteractBlockC2s;
use valence_protocol::{BlockPos, Direction, Hand};

use crate::action::ActionSequence;
use crate::event_loop::{EventLoopPreUpdate, PacketEvent};

pub struct InteractBlockPlugin;

impl Plugin for InteractBlockPlugin {
    fn build(&self, app: &mut App) {
        app.add_event::<InteractBlockEvent>()
            .add_systems(EventLoopPreUpdate, handle_interact_block);
    }
}

#[derive(Event, Copy, Clone, Debug)]
pub struct InteractBlockEvent {
    pub client: Entity,
    /// The hand that was used
    pub hand: Hand,
    /// The location of the block that was interacted with
    pub position: BlockPos,
    /// The face of the block that was clicked
    pub face: Direction,
    /// The position inside of the block that was clicked on
    pub cursor_pos: Vec3,
    /// Whether or not the player's head is inside a block
    pub head_inside_block: bool,
    /// Sequence number for synchronization
    pub sequence: i32,
}

fn handle_interact_block(
    mut packets: EventReader<PacketEvent>,
    mut clients: Query<&mut ActionSequence>,
    mut events: EventWriter<InteractBlockEvent>,
) {
    for packet in packets.read() {
        if let Some(pkt) = packet.decode::<PlayerInteractBlockC2s>() {
            if let Ok(mut action_seq) = clients.get_mut(packet.client) {
                action_seq.update(pkt.sequence.0);
            }

            // TODO: check that the block interaction is valid.

            events.send(InteractBlockEvent {
                client: packet.client,
                hand: pkt.hand,
                position: pkt.position,
                face: pkt.face,
                cursor_pos: pkt.cursor_pos,
                head_inside_block: pkt.head_inside_block,
                sequence: pkt.sequence.0,
            });
        }
    }
}