|
| 1 | +use std::borrow::Cow; |
| 2 | + |
| 3 | +use bevy_ecs::prelude::*; |
| 4 | +use serde::Deserialize; |
| 5 | +use valence_core::ident::Ident; |
| 6 | +use valence_core::protocol::encode::{PacketWriter, WritePacket}; |
| 7 | +use valence_core::protocol::var_int::VarInt; |
| 8 | +use valence_core::protocol::{packet_id, Decode, Encode, Packet}; |
| 9 | +use valence_core::Server; |
| 10 | + |
| 11 | +#[derive(Clone, Debug, Encode, Decode, Packet)] |
| 12 | +#[packet(id = packet_id::SYNCHRONIZE_TAGS_S2C)] |
| 13 | +pub struct SynchronizeTagsS2c<'a> { |
| 14 | + pub registries: Cow<'a, [Registry]>, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug, Resource, Default)] |
| 18 | +pub struct TagsRegistry { |
| 19 | + pub registries: Vec<Registry>, |
| 20 | + cached_packet: Vec<u8>, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Debug, Deserialize, Clone, PartialEq, Eq, Encode, Decode)] |
| 24 | +pub struct Registry { |
| 25 | + pub registry: Ident<String>, |
| 26 | + pub tags: Vec<TagEntry>, |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Debug, Deserialize, Clone, PartialEq, Eq, Encode, Decode)] |
| 30 | +pub struct TagEntry { |
| 31 | + pub name: Ident<String>, |
| 32 | + pub entries: Vec<VarInt>, |
| 33 | +} |
| 34 | + |
| 35 | +impl<'a> TagsRegistry { |
| 36 | + pub(crate) fn build_synchronize_tags(&'a self) -> SynchronizeTagsS2c<'a> { |
| 37 | + SynchronizeTagsS2c { |
| 38 | + registries: Cow::Borrowed(&self.registries), |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + pub fn sync_tags_packet(&self) -> &Vec<u8> { |
| 43 | + &self.cached_packet |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +pub(crate) fn init_tags_registry(mut tags: ResMut<TagsRegistry>) { |
| 48 | + let registries = |
| 49 | + serde_json::from_str::<Vec<Registry>>(include_str!("../../../extracted/tags.json")) |
| 50 | + .expect("tags.json is invalid"); |
| 51 | + tags.registries = registries; |
| 52 | +} |
| 53 | + |
| 54 | +pub(crate) fn cache_tags_packet(server: Res<Server>, tags: ResMut<TagsRegistry>) { |
| 55 | + if tags.is_changed() { |
| 56 | + let tags = tags.into_inner(); |
| 57 | + let packet = tags.build_synchronize_tags(); |
| 58 | + let mut bytes = vec![]; |
| 59 | + let mut scratch = vec![]; |
| 60 | + let mut writer = |
| 61 | + PacketWriter::new(&mut bytes, server.compression_threshold(), &mut scratch); |
| 62 | + writer.write_packet(&packet); |
| 63 | + tags.cached_packet = bytes; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[cfg(test)] |
| 68 | +mod tests { |
| 69 | + use super::*; |
| 70 | + use crate::RegistryPlugin; |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn smoke_test() { |
| 74 | + let mut app = bevy_app::App::new(); |
| 75 | + app.add_plugin(RegistryPlugin); |
| 76 | + app.insert_resource(Server::default()); |
| 77 | + app.update(); |
| 78 | + |
| 79 | + let tags_registry = app.world.get_resource::<TagsRegistry>().unwrap(); |
| 80 | + let packet = tags_registry.build_synchronize_tags(); |
| 81 | + assert!(!packet.registries.is_empty()); |
| 82 | + assert!(!tags_registry.cached_packet.is_empty()); |
| 83 | + } |
| 84 | +} |
0 commit comments