|
| 1 | +mod events; |
| 2 | + |
| 3 | +use azalea_protocol::packets::config::*; |
| 4 | +use azalea_protocol::packets::ConnectionProtocol; |
| 5 | +use bevy_ecs::prelude::*; |
| 6 | +use bevy_ecs::system::SystemState; |
| 7 | +pub use events::*; |
| 8 | +use tracing::{debug, warn}; |
| 9 | + |
| 10 | +use super::as_system; |
| 11 | +use crate::client::InConfigState; |
| 12 | +use crate::disconnect::DisconnectEvent; |
| 13 | +use crate::packet::game::KeepAliveEvent; |
| 14 | +use crate::raw_connection::RawConnection; |
| 15 | +use crate::{declare_packet_handlers, InstanceHolder}; |
| 16 | + |
| 17 | +pub fn process_packet_events(ecs: &mut World) { |
| 18 | + let mut events_owned = Vec::new(); |
| 19 | + let mut system_state: SystemState<EventReader<ReceiveConfigPacketEvent>> = |
| 20 | + SystemState::new(ecs); |
| 21 | + let mut events = system_state.get_mut(ecs); |
| 22 | + for ReceiveConfigPacketEvent { |
| 23 | + entity: player_entity, |
| 24 | + packet, |
| 25 | + } in events.read() |
| 26 | + { |
| 27 | + // we do this so `ecs` isn't borrowed for the whole loop |
| 28 | + events_owned.push((*player_entity, packet.clone())); |
| 29 | + } |
| 30 | + for (player_entity, packet) in events_owned { |
| 31 | + let mut handler = ConfigPacketHandler { |
| 32 | + player: player_entity, |
| 33 | + ecs, |
| 34 | + }; |
| 35 | + |
| 36 | + declare_packet_handlers!( |
| 37 | + ClientboundConfigPacket, |
| 38 | + packet, |
| 39 | + handler, |
| 40 | + [ |
| 41 | + cookie_request, |
| 42 | + custom_payload, |
| 43 | + disconnect, |
| 44 | + finish_configuration, |
| 45 | + keep_alive, |
| 46 | + ping, |
| 47 | + reset_chat, |
| 48 | + registry_data, |
| 49 | + resource_pack_pop, |
| 50 | + resource_pack_push, |
| 51 | + store_cookie, |
| 52 | + transfer, |
| 53 | + update_enabled_features, |
| 54 | + update_tags, |
| 55 | + select_known_packs, |
| 56 | + custom_report_details, |
| 57 | + server_links, |
| 58 | + ] |
| 59 | + ); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +pub struct ConfigPacketHandler<'a> { |
| 64 | + pub ecs: &'a mut World, |
| 65 | + pub player: Entity, |
| 66 | +} |
| 67 | +impl ConfigPacketHandler<'_> { |
| 68 | + pub fn registry_data(&mut self, p: ClientboundRegistryData) { |
| 69 | + as_system::<Query<&mut InstanceHolder>>(self.ecs, |mut query| { |
| 70 | + let instance_holder = query.get_mut(self.player).unwrap(); |
| 71 | + let mut instance = instance_holder.instance.write(); |
| 72 | + |
| 73 | + // add the new registry data |
| 74 | + instance.registries.append(p.registry_id, p.entries); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + pub fn custom_payload(&mut self, p: ClientboundCustomPayload) { |
| 79 | + debug!("Got custom payload packet {p:?}"); |
| 80 | + } |
| 81 | + |
| 82 | + pub fn disconnect(&mut self, p: ClientboundDisconnect) { |
| 83 | + warn!("Got disconnect packet {p:?}"); |
| 84 | + as_system::<EventWriter<_>>(self.ecs, |mut events| { |
| 85 | + events.send(DisconnectEvent { |
| 86 | + entity: self.player, |
| 87 | + reason: Some(p.reason), |
| 88 | + }); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + pub fn finish_configuration(&mut self, p: ClientboundFinishConfiguration) { |
| 93 | + debug!("got FinishConfiguration packet: {p:?}"); |
| 94 | + |
| 95 | + as_system::<(Commands, Query<&mut RawConnection>)>( |
| 96 | + self.ecs, |
| 97 | + |(mut commands, mut query)| { |
| 98 | + let mut raw_conn = query.get_mut(self.player).unwrap(); |
| 99 | + |
| 100 | + raw_conn |
| 101 | + .write_packet(ServerboundFinishConfiguration) |
| 102 | + .expect( |
| 103 | + "we should be in the right state and encoding this packet shouldn't fail", |
| 104 | + ); |
| 105 | + raw_conn.set_state(ConnectionProtocol::Game); |
| 106 | + |
| 107 | + // these components are added now that we're going to be in the Game state |
| 108 | + commands |
| 109 | + .entity(self.player) |
| 110 | + .remove::<InConfigState>() |
| 111 | + .insert(crate::JoinedClientBundle::default()); |
| 112 | + }, |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + pub fn keep_alive(&mut self, p: ClientboundKeepAlive) { |
| 117 | + debug!( |
| 118 | + "Got keep alive packet (in configuration) {p:?} for {:?}", |
| 119 | + self.player |
| 120 | + ); |
| 121 | + |
| 122 | + as_system::<(Query<&RawConnection>, EventWriter<_>)>(self.ecs, |(query, mut events)| { |
| 123 | + let raw_conn = query.get(self.player).unwrap(); |
| 124 | + |
| 125 | + events.send(KeepAliveEvent { |
| 126 | + entity: self.player, |
| 127 | + id: p.id, |
| 128 | + }); |
| 129 | + raw_conn |
| 130 | + .write_packet(ServerboundKeepAlive { id: p.id }) |
| 131 | + .unwrap(); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + pub fn ping(&mut self, p: ClientboundPing) { |
| 136 | + debug!("Got ping packet (in configuration) {p:?}"); |
| 137 | + |
| 138 | + as_system::<Query<&RawConnection>>(self.ecs, |query| { |
| 139 | + let raw_conn = query.get(self.player).unwrap(); |
| 140 | + |
| 141 | + raw_conn.write_packet(ServerboundPong { id: p.id }).unwrap(); |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + pub fn resource_pack_push(&mut self, p: ClientboundResourcePackPush) { |
| 146 | + debug!("Got resource pack push packet {p:?}"); |
| 147 | + |
| 148 | + as_system::<Query<&RawConnection>>(self.ecs, |query| { |
| 149 | + let raw_conn = query.get(self.player).unwrap(); |
| 150 | + |
| 151 | + // always accept resource pack |
| 152 | + raw_conn |
| 153 | + .write_packet(ServerboundResourcePack { |
| 154 | + id: p.id, |
| 155 | + action: s_resource_pack::Action::Accepted, |
| 156 | + }) |
| 157 | + .unwrap(); |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + pub fn resource_pack_pop(&mut self, p: ClientboundResourcePackPop) { |
| 162 | + debug!("Got resource pack pop packet {p:?}"); |
| 163 | + } |
| 164 | + |
| 165 | + pub fn update_enabled_features(&mut self, p: ClientboundUpdateEnabledFeatures) { |
| 166 | + debug!("Got update enabled features packet {p:?}"); |
| 167 | + } |
| 168 | + |
| 169 | + pub fn update_tags(&mut self, _p: ClientboundUpdateTags) { |
| 170 | + debug!("Got update tags packet"); |
| 171 | + } |
| 172 | + |
| 173 | + pub fn cookie_request(&mut self, p: ClientboundCookieRequest) { |
| 174 | + debug!("Got cookie request packet {p:?}"); |
| 175 | + |
| 176 | + as_system::<Query<&RawConnection>>(self.ecs, |query| { |
| 177 | + let raw_conn = query.get(self.player).unwrap(); |
| 178 | + |
| 179 | + raw_conn |
| 180 | + .write_packet(ServerboundCookieResponse { |
| 181 | + key: p.key, |
| 182 | + // cookies aren't implemented |
| 183 | + payload: None, |
| 184 | + }) |
| 185 | + .unwrap(); |
| 186 | + }); |
| 187 | + } |
| 188 | + |
| 189 | + pub fn reset_chat(&mut self, p: ClientboundResetChat) { |
| 190 | + debug!("Got reset chat packet {p:?}"); |
| 191 | + } |
| 192 | + |
| 193 | + pub fn store_cookie(&mut self, p: ClientboundStoreCookie) { |
| 194 | + debug!("Got store cookie packet {p:?}"); |
| 195 | + } |
| 196 | + |
| 197 | + pub fn transfer(&mut self, p: ClientboundTransfer) { |
| 198 | + debug!("Got transfer packet {p:?}"); |
| 199 | + } |
| 200 | + |
| 201 | + pub fn select_known_packs(&mut self, p: ClientboundSelectKnownPacks) { |
| 202 | + debug!("Got select known packs packet {p:?}"); |
| 203 | + |
| 204 | + as_system::<Query<&RawConnection>>(self.ecs, |query| { |
| 205 | + let raw_conn = query.get(self.player).unwrap(); |
| 206 | + |
| 207 | + // resource pack management isn't implemented |
| 208 | + raw_conn |
| 209 | + .write_packet(ServerboundSelectKnownPacks { |
| 210 | + known_packs: vec![], |
| 211 | + }) |
| 212 | + .unwrap(); |
| 213 | + }); |
| 214 | + } |
| 215 | + |
| 216 | + pub fn server_links(&mut self, p: ClientboundServerLinks) { |
| 217 | + debug!("Got server links packet {p:?}"); |
| 218 | + } |
| 219 | + |
| 220 | + pub fn custom_report_details(&mut self, p: ClientboundCustomReportDetails) { |
| 221 | + debug!("Got custom report details packet {p:?}"); |
| 222 | + } |
| 223 | +} |
0 commit comments