use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use valence_protocol::packets::play::{ResourcePackSendS2c, ResourcePackStatusC2s};
use valence_protocol::text::Text;
use valence_protocol::WritePacket;
use crate::client::Client;
use crate::event_loop::{EventLoopPreUpdate, PacketEvent};
pub struct ResourcePackPlugin;
impl Plugin for ResourcePackPlugin {
fn build(&self, app: &mut App) {
app.add_event::<ResourcePackStatusEvent>()
.add_systems(EventLoopPreUpdate, handle_resource_pack_status);
}
}
#[derive(Event, Copy, Clone, PartialEq, Eq, Debug)]
pub struct ResourcePackStatusEvent {
pub client: Entity,
pub status: ResourcePackStatusC2s,
}
impl Client {
pub fn set_resource_pack(
&mut self,
url: &str,
hash: &str,
forced: bool,
prompt_message: Option<Text>,
) {
self.write_packet(&ResourcePackSendS2c {
url,
hash: hash.into(),
forced,
prompt_message: prompt_message.map(|t| t.into()),
});
}
}
fn handle_resource_pack_status(
mut packets: EventReader<PacketEvent>,
mut events: EventWriter<ResourcePackStatusEvent>,
) {
for packet in packets.read() {
if let Some(pkt) = packet.decode::<ResourcePackStatusC2s>() {
events.send(ResourcePackStatusEvent {
client: packet.client,
status: pkt,
});
}
}
}