valence_server/
resource_pack.rs

1use bevy_app::prelude::*;
2use bevy_ecs::prelude::*;
3use valence_protocol::packets::play::{ResourcePackSendS2c, ResourcePackStatusC2s};
4use valence_protocol::text::Text;
5use valence_protocol::WritePacket;
6
7use crate::client::Client;
8use crate::event_loop::{EventLoopPreUpdate, PacketEvent};
9
10pub struct ResourcePackPlugin;
11
12impl Plugin for ResourcePackPlugin {
13    fn build(&self, app: &mut App) {
14        app.add_event::<ResourcePackStatusEvent>()
15            .add_systems(EventLoopPreUpdate, handle_resource_pack_status);
16    }
17}
18
19#[derive(Event, Copy, Clone, PartialEq, Eq, Debug)]
20pub struct ResourcePackStatusEvent {
21    pub client: Entity,
22    pub status: ResourcePackStatusC2s,
23}
24
25impl Client {
26    /// Requests that the client download and enable a resource pack.
27    ///
28    /// # Arguments
29    /// * `url` - The URL of the resource pack file.
30    /// * `hash` - The SHA-1 hash of the resource pack file. The value must be a
31    ///   40-character hexadecimal string.
32    /// * `forced` - Whether a client should be kicked from the server upon
33    ///   declining the pack (this is enforced client-side)
34    /// * `prompt_message` - A message to be displayed with the resource pack
35    ///   dialog.
36    pub fn set_resource_pack(
37        &mut self,
38        url: &str,
39        hash: &str,
40        forced: bool,
41        prompt_message: Option<Text>,
42    ) {
43        self.write_packet(&ResourcePackSendS2c {
44            url,
45            hash: hash.into(),
46            forced,
47            prompt_message: prompt_message.map(|t| t.into()),
48        });
49    }
50}
51
52fn handle_resource_pack_status(
53    mut packets: EventReader<PacketEvent>,
54    mut events: EventWriter<ResourcePackStatusEvent>,
55) {
56    for packet in packets.read() {
57        if let Some(pkt) = packet.decode::<ResourcePackStatusC2s>() {
58            events.send(ResourcePackStatusEvent {
59                client: packet.client,
60                status: pkt,
61            });
62        }
63    }
64}