azalea/
accept_resource_packs.rs

1use azalea_client::{
2    InConfigState,
3    chunks::handle_chunk_batch_finished_event,
4    inventory::InventorySet,
5    packet::{
6        config::SendConfigPacketEvent,
7        death_event_on_0_health,
8        game::{ResourcePackEvent, SendPacketEvent},
9    },
10    respawn::perform_respawn,
11};
12use azalea_protocol::packets::{
13    config,
14    game::s_resource_pack::{self, ServerboundResourcePack},
15};
16use bevy_app::Update;
17use bevy_ecs::prelude::*;
18
19use crate::app::{App, Plugin};
20
21/// A plugin that makes it so bots automatically accept resource packs.
22#[derive(Clone, Default)]
23pub struct AcceptResourcePacksPlugin;
24impl Plugin for AcceptResourcePacksPlugin {
25    fn build(&self, app: &mut App) {
26        app.add_systems(
27            Update,
28            accept_resource_pack
29                .before(perform_respawn)
30                .after(death_event_on_0_health)
31                .after(handle_chunk_batch_finished_event)
32                .after(InventorySet)
33                .after(azalea_client::brand::handle_end_login_state),
34        );
35    }
36}
37
38fn accept_resource_pack(
39    mut events: EventReader<ResourcePackEvent>,
40    mut commands: Commands,
41    query_in_config_state: Query<Option<&InConfigState>>,
42) {
43    for event in events.read() {
44        let Ok(in_config_state_option) = query_in_config_state.get(event.entity) else {
45            continue;
46        };
47
48        if in_config_state_option.is_some() {
49            commands.trigger(SendConfigPacketEvent::new(
50                event.entity,
51                config::ServerboundResourcePack {
52                    id: event.id,
53                    action: config::s_resource_pack::Action::Accepted,
54                },
55            ));
56            commands.trigger(SendConfigPacketEvent::new(
57                event.entity,
58                config::ServerboundResourcePack {
59                    id: event.id,
60                    action: config::s_resource_pack::Action::SuccessfullyLoaded,
61                },
62            ));
63        } else {
64            commands.trigger(SendPacketEvent::new(
65                event.entity,
66                ServerboundResourcePack {
67                    id: event.id,
68                    action: s_resource_pack::Action::Accepted,
69                },
70            ));
71            commands.trigger(SendPacketEvent::new(
72                event.entity,
73                ServerboundResourcePack {
74                    id: event.id,
75                    action: s_resource_pack::Action::SuccessfullyLoaded,
76                },
77            ));
78        }
79    }
80}