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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use derive_more::Deref;
use valence_protocol::packets::play::player_action_c2s::PlayerAction;
use valence_protocol::packets::play::{PlayerActionC2s, PlayerActionResponseS2c};
use valence_protocol::{BlockPos, Direction, VarInt, WritePacket};

use crate::client::{Client, UpdateClientsSet};
use crate::event_loop::{EventLoopPreUpdate, PacketEvent};

pub struct ActionPlugin;

impl Plugin for ActionPlugin {
    fn build(&self, app: &mut App) {
        app.add_event::<DiggingEvent>()
            .add_systems(EventLoopPreUpdate, handle_player_action)
            .add_systems(
                PostUpdate,
                acknowledge_player_actions.in_set(UpdateClientsSet),
            );
    }
}

#[derive(Event, Copy, Clone, Debug)]
pub struct DiggingEvent {
    pub client: Entity,
    pub position: BlockPos,
    pub direction: Direction,
    pub state: DiggingState,
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum DiggingState {
    Start,
    Abort,
    Stop,
}

#[derive(Component, Copy, Clone, PartialEq, Eq, Default, Debug, Deref)]
pub struct ActionSequence(i32);

impl ActionSequence {
    pub fn update(&mut self, val: i32) {
        self.0 = self.0.max(val);
    }

    pub fn get(&self) -> i32 {
        self.0
    }
}

fn handle_player_action(
    mut clients: Query<&mut ActionSequence>,
    mut packets: EventReader<PacketEvent>,
    mut digging_events: EventWriter<DiggingEvent>,
) {
    for packet in packets.read() {
        if let Some(pkt) = packet.decode::<PlayerActionC2s>() {
            if let Ok(mut seq) = clients.get_mut(packet.client) {
                seq.update(pkt.sequence.0);
            }

            // TODO: check that digging is happening within configurable distance to client.
            // TODO: check that blocks are being broken at the appropriate speeds.

            match pkt.action {
                PlayerAction::StartDestroyBlock => {
                    digging_events.send(DiggingEvent {
                        client: packet.client,
                        position: pkt.position,
                        direction: pkt.direction,
                        state: DiggingState::Start,
                    });
                }
                PlayerAction::AbortDestroyBlock => {
                    digging_events.send(DiggingEvent {
                        client: packet.client,
                        position: pkt.position,
                        direction: pkt.direction,
                        state: DiggingState::Abort,
                    });
                }
                PlayerAction::StopDestroyBlock => {
                    digging_events.send(DiggingEvent {
                        client: packet.client,
                        position: pkt.position,
                        direction: pkt.direction,
                        state: DiggingState::Stop,
                    });
                }
                PlayerAction::DropAllItems => {}
                PlayerAction::DropItem => {}
                PlayerAction::ReleaseUseItem => {}
                PlayerAction::SwapItemWithOffhand => {}
            }
        }
    }
}

fn acknowledge_player_actions(
    mut clients: Query<(&mut Client, &mut ActionSequence), Changed<ActionSequence>>,
) {
    for (mut client, mut action_seq) in &mut clients {
        if action_seq.0 != 0 {
            client.write_packet(&PlayerActionResponseS2c {
                sequence: VarInt(action_seq.0),
            });

            action_seq.0 = 0;
        }
    }
}