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
use bevy_ecs::prelude::*;
use valence_server::event_loop::PacketEvent;
use valence_server::protocol::packets::play::AdvancementTabC2s;
use valence_server::Ident;

/// This event sends when the client changes or closes advancement's tab.
#[derive(Event, Clone, PartialEq, Eq, Debug)]
pub struct AdvancementTabChangeEvent {
    pub client: Entity,
    /// If None then the client has closed advancement's tabs.
    pub opened_tab: Option<Ident<String>>,
}

pub(crate) fn handle_advancement_tab_change(
    mut packets: EventReader<PacketEvent>,
    mut advancement_tab_change_events: EventWriter<AdvancementTabChangeEvent>,
) {
    for packet in packets.read() {
        if let Some(pkt) = packet.decode::<AdvancementTabC2s>() {
            advancement_tab_change_events.send(AdvancementTabChangeEvent {
                client: packet.client,
                opened_tab: match pkt {
                    AdvancementTabC2s::ClosedScreen => None,
                    AdvancementTabC2s::OpenedTab { tab_id } => Some(tab_id.into()),
                },
            });
        }
    }
}