1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

make packet an Arc in PacketEvent

This commit is contained in:
mat 2023-11-18 00:49:44 -06:00
parent e39de79a6b
commit 71cd3f021e
5 changed files with 30 additions and 31 deletions

View file

@ -165,7 +165,7 @@ fn packet_listener(query: Query<&LocalPlayerEvents>, mut events: EventReader<Pac
.get(event.entity)
.expect("Non-local entities shouldn't be able to receive add player events");
local_player_events
.send(Event::Packet(Arc::new(event.packet.clone())))
.send(Event::Packet(event.packet.clone()))
.unwrap();
}
}

View file

@ -68,8 +68,8 @@ pub struct PlayerAbilities {
/// Used for the fov
pub walking_speed: f32,
}
impl From<ClientboundPlayerAbilitiesPacket> for PlayerAbilities {
fn from(packet: ClientboundPlayerAbilitiesPacket) -> Self {
impl From<&ClientboundPlayerAbilitiesPacket> for PlayerAbilities {
fn from(packet: &ClientboundPlayerAbilitiesPacket) -> Self {
Self {
invulnerable: packet.flags.invulnerable,
flying: packet.flags.flying,

View file

@ -74,7 +74,7 @@ pub struct PacketEvent {
/// The client entity that received the packet.
pub entity: Entity,
/// The packet that was actually received.
pub packet: ClientboundGamePacket,
pub packet: Arc<ClientboundGamePacket>,
}
/// A player joined the game (or more specifically, was added to the tab
@ -163,13 +163,10 @@ pub fn send_packet_events(
continue;
}
};
if let ClientboundGamePacket::LevelChunkWithLight(_) = packet {
} else {
packet_events.send(PacketEvent {
entity: player_entity,
packet,
});
}
packet_events.send(PacketEvent {
entity: player_entity,
packet: Arc::new(packet),
});
}
// clear the packets right after we read them
packets.clear();
@ -190,7 +187,9 @@ pub fn process_packet_events(ecs: &mut World) {
events_owned.push((*player_entity, packet.clone()));
}
for (player_entity, packet) in events_owned {
match packet {
let packet_clone = packet.clone();
let packet_ref = packet_clone.as_ref();
match packet_ref {
ClientboundGamePacket::Login(p) => {
debug!("Got login packet");
@ -756,6 +755,8 @@ pub fn process_packet_events(ecs: &mut World) {
};
let entity_kind = *entity_kind_query.get(entity).unwrap();
let packed_items = p.packed_items.clone().to_vec();
// we use RelativeEntityUpdate because it makes sure changes aren't made
// multiple times
commands.entity(entity).add(RelativeEntityUpdate {
@ -766,11 +767,9 @@ pub fn process_packet_events(ecs: &mut World) {
let mut commands_system_state = SystemState::<Commands>::new(world);
let mut commands = commands_system_state.get_mut(world);
let mut entity_comands = commands.entity(entity_id);
if let Err(e) = apply_metadata(
&mut entity_comands,
*entity_kind,
(*p.packed_items).clone(),
) {
if let Err(e) =
apply_metadata(&mut entity_comands, *entity_kind, packed_items)
{
warn!("{e}");
}
});
@ -803,18 +802,18 @@ pub fn process_packet_events(ecs: &mut World) {
// this is to make sure the same entity velocity update doesn't get sent
// multiple times when in swarms
let knockback = KnockbackType::Set(Vec3 {
x: p.xa as f64 / 8000.,
y: p.ya as f64 / 8000.,
z: p.za as f64 / 8000.,
});
commands.entity(entity).add(RelativeEntityUpdate {
partial_world: instance_holder.partial_instance.clone(),
update: Box::new(move |entity_mut| {
entity_mut.world_scope(|world| {
world.send_event(KnockbackEvent {
entity,
knockback: KnockbackType::Set(Vec3 {
x: p.xa as f64 / 8000.,
y: p.ya as f64 / 8000.,
z: p.za as f64 / 8000.,
}),
})
world.send_event(KnockbackEvent { entity, knockback })
});
}),
});
@ -1226,7 +1225,7 @@ pub fn process_packet_events(ecs: &mut World) {
entity: player_entity,
window_id: p.container_id,
menu_type: p.menu_type,
title: p.title,
title: p.title.to_owned(),
})
}
ClientboundGamePacket::OpenSignEditor(_) => {}
@ -1281,10 +1280,10 @@ pub fn process_packet_events(ecs: &mut World) {
resource_pack_events.send(ResourcePackEvent {
entity: player_entity,
url: p.url,
hash: p.hash,
url: p.url.to_owned(),
hash: p.hash.to_owned(),
required: p.required,
prompt: p.prompt,
prompt: p.prompt.to_owned(),
});
system_state.apply(ecs);

View file

@ -47,7 +47,7 @@ async fn main() -> anyhow::Result<()> {
let mut accounts = Vec::new();
for i in 0..200 {
for i in 0..3 {
accounts.push(Account::offline(&format!("bot{i}")));
}

View file

@ -180,7 +180,7 @@ pub struct WaitingForInventoryOpen;
fn handle_menu_opened_event(mut commands: Commands, mut events: EventReader<PacketEvent>) {
for event in events.read() {
if let ClientboundGamePacket::ContainerSetContent { .. } = event.packet {
if let ClientboundGamePacket::ContainerSetContent { .. } = event.packet.as_ref() {
commands
.entity(event.entity)
.remove::<WaitingForInventoryOpen>();