azalea/
accept_resource_packs.rs

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