azalea_client/plugins/packet/config/
mod.rs1mod events;
2
3use std::io::Cursor;
4
5use azalea_entity::LocalEntity;
6use azalea_protocol::{
7 packets::{ConnectionProtocol, config::*},
8 read::{ReadPacketError, deserialize_packet},
9};
10use bevy_ecs::prelude::*;
11pub use events::*;
12use tracing::{debug, warn};
13
14use super::{as_system, declare_packet_handlers};
15use crate::{
16 client::InConfigState,
17 connection::RawConnection,
18 cookies::{RequestCookieEvent, StoreCookieEvent},
19 disconnect::DisconnectEvent,
20 local_player::WorldHolder,
21 packet::game::{KeepAliveEvent, ResourcePackEvent},
22};
23
24pub fn process_raw_packet(
25 ecs: &mut World,
26 ctx: azalea_buf::Ctx,
27 player: Entity,
28 raw_packet: &[u8],
29) -> Result<(), Box<ReadPacketError>> {
30 let packet = deserialize_packet(ctx, &mut Cursor::new(raw_packet))?;
31 process_packet(ecs, player, &packet);
32 Ok(())
33}
34
35pub fn process_packet(ecs: &mut World, player: Entity, packet: &ClientboundConfigPacket) {
36 let mut handler = ConfigPacketHandler { player, ecs };
37
38 declare_packet_handlers!(
39 ClientboundConfigPacket,
40 packet,
41 handler,
42 [
43 cookie_request,
44 custom_payload,
45 disconnect,
46 finish_configuration,
47 keep_alive,
48 ping,
49 reset_chat,
50 registry_data,
51 resource_pack_pop,
52 resource_pack_push,
53 store_cookie,
54 transfer,
55 update_enabled_features,
56 update_tags,
57 select_known_packs,
58 custom_report_details,
59 server_links,
60 clear_dialog,
61 show_dialog,
62 code_of_conduct,
63 post_effects,
64 ]
65 );
66}
67
68pub struct ConfigPacketHandler<'a> {
69 pub ecs: &'a mut World,
70 pub player: Entity,
71}
72impl ConfigPacketHandler<'_> {
73 pub fn registry_data(&mut self, p: &ClientboundRegistryData) {
74 as_system::<Query<&WorldHolder>>(self.ecs, |mut query| {
75 let world_holder = query.get_mut(self.player).unwrap();
76 let mut world = world_holder.shared.write();
77
78 world.registries.append(
80 p.registry_id.clone(),
81 p.entries
82 .iter()
83 .cloned()
84 .map(|(ident, tag)| (ident, tag.map(|t| t.into())))
85 .collect(),
86 )
87 });
88 }
89
90 pub fn custom_payload(&mut self, p: &ClientboundCustomPayload) {
91 debug!("Got custom payload packet {p:?}");
92 }
93
94 pub fn disconnect(&mut self, p: &ClientboundDisconnect) {
95 warn!("Got disconnect packet {p:?}");
96 as_system::<MessageWriter<_>>(self.ecs, |mut events| {
97 events.write(DisconnectEvent {
98 entity: self.player,
99 reason: Some(p.reason.clone()),
100 });
101 });
102 }
103
104 pub fn finish_configuration(&mut self, _p: &ClientboundFinishConfiguration) {
105 debug!("got FinishConfiguration packet");
106
107 as_system::<(Commands, Query<&mut RawConnection>)>(
108 self.ecs,
109 |(mut commands, mut query)| {
110 let mut raw_conn = query.get_mut(self.player).unwrap();
111 raw_conn.state = ConnectionProtocol::Game;
112
113 commands.trigger(SendConfigPacketEvent::new(
114 self.player,
115 ServerboundFinishConfiguration,
116 ));
117
118 commands
121 .entity(self.player)
122 .remove::<InConfigState>()
123 .insert((
124 crate::JoinedClientBundle::default(),
125 LocalEntity,
128 ));
129 },
130 );
131 }
132
133 pub fn keep_alive(&mut self, p: &ClientboundKeepAlive) {
134 debug!(
135 "Got keep alive packet (in configuration) {p:?} for {:?}",
136 self.player
137 );
138
139 as_system::<Commands>(self.ecs, |mut commands| {
140 commands.trigger(KeepAliveEvent {
141 entity: self.player,
142 id: p.id,
143 });
144 commands.trigger(SendConfigPacketEvent::new(
145 self.player,
146 ServerboundKeepAlive { id: p.id },
147 ));
148 });
149 }
150
151 pub fn ping(&mut self, p: &ClientboundPing) {
152 debug!("Got ping packet (in configuration) {p:?}");
153
154 as_system::<Commands>(self.ecs, |mut commands| {
155 commands.trigger(ConfigPingEvent {
156 entity: self.player,
157 packet: p.clone(),
158 });
159 });
160 }
161
162 pub fn resource_pack_push(&mut self, p: &ClientboundResourcePackPush) {
163 debug!("Got resource pack push packet {p:?}");
164
165 as_system::<MessageWriter<_>>(self.ecs, |mut events| {
166 events.write(ResourcePackEvent {
167 entity: self.player,
168 id: p.id,
169 url: p.url.to_owned(),
170 hash: p.hash.to_owned(),
171 required: p.required,
172 prompt: p.prompt.to_owned(),
173 });
174 });
175 }
176
177 pub fn resource_pack_pop(&mut self, p: &ClientboundResourcePackPop) {
178 debug!("Got resource pack pop packet {p:?}");
179 }
180
181 pub fn update_enabled_features(&mut self, p: &ClientboundUpdateEnabledFeatures) {
182 debug!("Got update enabled features packet {p:?}");
183 }
184
185 pub fn update_tags(&mut self, _p: &ClientboundUpdateTags) {
186 debug!("Got update tags packet");
187 }
188
189 pub fn cookie_request(&mut self, p: &ClientboundCookieRequest) {
190 debug!("Got cookie request packet {p:?}");
191 as_system::<Commands>(self.ecs, |mut commands| {
192 commands.trigger(RequestCookieEvent {
193 entity: self.player,
194 key: p.key.clone(),
195 });
196 });
197 }
198 pub fn store_cookie(&mut self, p: &ClientboundStoreCookie) {
199 debug!("Got store cookie packet {p:?}");
200 as_system::<Commands>(self.ecs, |mut commands| {
201 commands.trigger(StoreCookieEvent {
202 entity: self.player,
203 key: p.key.clone(),
204 payload: p.payload.clone(),
205 });
206 });
207 }
208
209 pub fn reset_chat(&mut self, p: &ClientboundResetChat) {
210 debug!("Got reset chat packet {p:?}");
211 }
212
213 pub fn transfer(&mut self, p: &ClientboundTransfer) {
214 debug!("Got transfer packet {p:?}");
215 }
216
217 pub fn select_known_packs(&mut self, p: &ClientboundSelectKnownPacks) {
218 debug!("Got select known packs packet {p:?}");
219
220 as_system::<Commands>(self.ecs, |mut commands| {
221 commands.trigger(SendConfigPacketEvent::new(
223 self.player,
224 ServerboundSelectKnownPacks {
225 known_packs: vec![],
226 },
227 ));
228 });
229 }
230
231 pub fn server_links(&mut self, p: &ClientboundServerLinks) {
232 debug!("Got server links packet {p:?}");
233 }
234
235 pub fn custom_report_details(&mut self, p: &ClientboundCustomReportDetails) {
236 debug!("Got custom report details packet {p:?}");
237 }
238
239 pub fn clear_dialog(&mut self, p: &ClientboundClearDialog) {
240 debug!("Got clear dialog packet {p:?}");
241 }
242 pub fn show_dialog(&mut self, p: &ClientboundShowDialog) {
243 debug!("Got show dialog packet {p:?}");
244 }
245 pub fn code_of_conduct(&mut self, p: &ClientboundCodeOfConduct) {
246 debug!("Got code of conduct packet {p:?}");
247 }
248 pub fn post_effects(&mut self, p: &ClientboundPostEffects) {
249 debug!("Got post effects packet {p:?}");
250 }
251}