Skip to content

Commit 71cd3f0

Browse files
committed
make packet an Arc in PacketEvent
1 parent e39de79 commit 71cd3f0

File tree

5 files changed

+30
-31
lines changed

5 files changed

+30
-31
lines changed

azalea-client/src/events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ fn packet_listener(query: Query<&LocalPlayerEvents>, mut events: EventReader<Pac
165165
.get(event.entity)
166166
.expect("Non-local entities shouldn't be able to receive add player events");
167167
local_player_events
168-
.send(Event::Packet(Arc::new(event.packet.clone())))
168+
.send(Event::Packet(event.packet.clone()))
169169
.unwrap();
170170
}
171171
}

azalea-client/src/local_player.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ pub struct PlayerAbilities {
6868
/// Used for the fov
6969
pub walking_speed: f32,
7070
}
71-
impl From<ClientboundPlayerAbilitiesPacket> for PlayerAbilities {
72-
fn from(packet: ClientboundPlayerAbilitiesPacket) -> Self {
71+
impl From<&ClientboundPlayerAbilitiesPacket> for PlayerAbilities {
72+
fn from(packet: &ClientboundPlayerAbilitiesPacket) -> Self {
7373
Self {
7474
invulnerable: packet.flags.invulnerable,
7575
flying: packet.flags.flying,

azalea-client/src/packet_handling/game.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub struct PacketEvent {
7474
/// The client entity that received the packet.
7575
pub entity: Entity,
7676
/// The packet that was actually received.
77-
pub packet: ClientboundGamePacket,
77+
pub packet: Arc<ClientboundGamePacket>,
7878
}
7979

8080
/// A player joined the game (or more specifically, was added to the tab
@@ -163,13 +163,10 @@ pub fn send_packet_events(
163163
continue;
164164
}
165165
};
166-
if let ClientboundGamePacket::LevelChunkWithLight(_) = packet {
167-
} else {
168-
packet_events.send(PacketEvent {
169-
entity: player_entity,
170-
packet,
171-
});
172-
}
166+
packet_events.send(PacketEvent {
167+
entity: player_entity,
168+
packet: Arc::new(packet),
169+
});
173170
}
174171
// clear the packets right after we read them
175172
packets.clear();
@@ -190,7 +187,9 @@ pub fn process_packet_events(ecs: &mut World) {
190187
events_owned.push((*player_entity, packet.clone()));
191188
}
192189
for (player_entity, packet) in events_owned {
193-
match packet {
190+
let packet_clone = packet.clone();
191+
let packet_ref = packet_clone.as_ref();
192+
match packet_ref {
194193
ClientboundGamePacket::Login(p) => {
195194
debug!("Got login packet");
196195

@@ -756,6 +755,8 @@ pub fn process_packet_events(ecs: &mut World) {
756755
};
757756
let entity_kind = *entity_kind_query.get(entity).unwrap();
758757

758+
let packed_items = p.packed_items.clone().to_vec();
759+
759760
// we use RelativeEntityUpdate because it makes sure changes aren't made
760761
// multiple times
761762
commands.entity(entity).add(RelativeEntityUpdate {
@@ -766,11 +767,9 @@ pub fn process_packet_events(ecs: &mut World) {
766767
let mut commands_system_state = SystemState::<Commands>::new(world);
767768
let mut commands = commands_system_state.get_mut(world);
768769
let mut entity_comands = commands.entity(entity_id);
769-
if let Err(e) = apply_metadata(
770-
&mut entity_comands,
771-
*entity_kind,
772-
(*p.packed_items).clone(),
773-
) {
770+
if let Err(e) =
771+
apply_metadata(&mut entity_comands, *entity_kind, packed_items)
772+
{
774773
warn!("{e}");
775774
}
776775
});
@@ -803,18 +802,18 @@ pub fn process_packet_events(ecs: &mut World) {
803802

804803
// this is to make sure the same entity velocity update doesn't get sent
805804
// multiple times when in swarms
805+
806+
let knockback = KnockbackType::Set(Vec3 {
807+
x: p.xa as f64 / 8000.,
808+
y: p.ya as f64 / 8000.,
809+
z: p.za as f64 / 8000.,
810+
});
811+
806812
commands.entity(entity).add(RelativeEntityUpdate {
807813
partial_world: instance_holder.partial_instance.clone(),
808814
update: Box::new(move |entity_mut| {
809815
entity_mut.world_scope(|world| {
810-
world.send_event(KnockbackEvent {
811-
entity,
812-
knockback: KnockbackType::Set(Vec3 {
813-
x: p.xa as f64 / 8000.,
814-
y: p.ya as f64 / 8000.,
815-
z: p.za as f64 / 8000.,
816-
}),
817-
})
816+
world.send_event(KnockbackEvent { entity, knockback })
818817
});
819818
}),
820819
});
@@ -1226,7 +1225,7 @@ pub fn process_packet_events(ecs: &mut World) {
12261225
entity: player_entity,
12271226
window_id: p.container_id,
12281227
menu_type: p.menu_type,
1229-
title: p.title,
1228+
title: p.title.to_owned(),
12301229
})
12311230
}
12321231
ClientboundGamePacket::OpenSignEditor(_) => {}
@@ -1281,10 +1280,10 @@ pub fn process_packet_events(ecs: &mut World) {
12811280

12821281
resource_pack_events.send(ResourcePackEvent {
12831282
entity: player_entity,
1284-
url: p.url,
1285-
hash: p.hash,
1283+
url: p.url.to_owned(),
1284+
hash: p.hash.to_owned(),
12861285
required: p.required,
1287-
prompt: p.prompt,
1286+
prompt: p.prompt.to_owned(),
12881287
});
12891288

12901289
system_state.apply(ecs);

azalea/examples/testbot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async fn main() -> anyhow::Result<()> {
4747

4848
let mut accounts = Vec::new();
4949

50-
for i in 0..200 {
50+
for i in 0..3 {
5151
accounts.push(Account::offline(&format!("bot{i}")));
5252
}
5353

azalea/src/container.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pub struct WaitingForInventoryOpen;
180180

181181
fn handle_menu_opened_event(mut commands: Commands, mut events: EventReader<PacketEvent>) {
182182
for event in events.read() {
183-
if let ClientboundGamePacket::ContainerSetContent { .. } = event.packet {
183+
if let ClientboundGamePacket::ContainerSetContent { .. } = event.packet.as_ref() {
184184
commands
185185
.entity(event.entity)
186186
.remove::<WaitingForInventoryOpen>();

0 commit comments

Comments
 (0)