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
use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use valence_protocol::packets::play::ClientStatusC2s;

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

pub struct StatusPlugin;

impl Plugin for StatusPlugin {
    fn build(&self, app: &mut App) {
        app.add_event::<RequestRespawnEvent>()
            .add_event::<RequestStatsEvent>()
            .add_systems(EventLoopPreUpdate, handle_status);
    }
}

#[derive(Event, Copy, Clone, PartialEq, Eq, Debug)]
pub struct RequestRespawnEvent {
    pub client: Entity,
}

#[derive(Event, Copy, Clone, PartialEq, Eq, Debug)]
pub struct RequestStatsEvent {
    pub client: Entity,
}

fn handle_status(
    mut packets: EventReader<PacketEvent>,
    mut respawn_events: EventWriter<RequestRespawnEvent>,
    mut request_stats_events: EventWriter<RequestStatsEvent>,
) {
    for packet in packets.read() {
        if let Some(pkt) = packet.decode::<ClientStatusC2s>() {
            match pkt {
                ClientStatusC2s::PerformRespawn => {
                    respawn_events.send(RequestRespawnEvent {
                        client: packet.client,
                    });
                }
                ClientStatusC2s::RequestStats => {
                    request_stats_events.send(RequestStatsEvent {
                        client: packet.client,
                    });
                }
            }
        }
    }
}