diff options
| author | mat <git@matdoes.dev> | 2023-09-28 21:57:36 -0500 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2023-09-28 21:57:36 -0500 |
| commit | 0bf8291388f740ed1b854c545eee4a6baa107eef (patch) | |
| tree | 604ddc6da5ca540188231607ebb1094a42c631bb /azalea-client/src/client.rs | |
| parent | 5977f79400e46de6a7af413a51bc1afd8e0dc9f6 (diff) | |
| download | azalea-drasl-0bf8291388f740ed1b854c545eee4a6baa107eef.tar.xz | |
check for entity duplication before spawning
this fixes behavior where in swarms entities in the world might sometimes have a duplicate that gets spawned and despawned immediately
Diffstat (limited to 'azalea-client/src/client.rs')
| -rw-r--r-- | azalea-client/src/client.rs | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index cd191e0f..6438cf06 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -25,7 +25,7 @@ use azalea_buf::McBufWritable; use azalea_chat::FormattedText; use azalea_core::{ResourceLocation, Vec3}; use azalea_entity::{ - indexing::{EntityIdIndex, Loaded}, + indexing::{EntityIdIndex, EntityUuidIndex}, metadata::Health, EntityPlugin, EntityUpdateSet, EyeHeight, LocalEntity, Position, }; @@ -208,8 +208,6 @@ impl Client { resolved_address: &SocketAddr, run_schedule_sender: mpsc::UnboundedSender<()>, ) -> Result<(Self, mpsc::UnboundedReceiver<Event>), JoinError> { - let entity = ecs_lock.lock().spawn(account.to_owned()).id(); - let conn = Connection::new(resolved_address).await?; let (mut conn, game_profile) = Self::handshake(conn, account, address).await?; @@ -236,6 +234,22 @@ impl Client { let mut ecs = ecs_lock.lock(); + // check if an entity with our uuid already exists in the ecs and if so then + // just use that + let entity = { + let entity_uuid_index = ecs.resource::<EntityUuidIndex>(); + if let Some(entity) = entity_uuid_index.get(&game_profile.uuid) { + debug!("Reusing entity {entity:?} for client"); + entity + } else { + let entity = ecs.spawn_empty().id(); + debug!("Created new entity {entity:?} for client"); + // add to the uuid index + let mut entity_uuid_index = ecs.resource_mut::<EntityUuidIndex>(); + entity_uuid_index.insert(game_profile.uuid, entity); + entity + } + }; // we got the ConfigurationConnection, so the client is now connected :) let client = Client::new( game_profile.clone(), @@ -256,6 +270,7 @@ impl Client { received_registries: ReceivedRegistries::default(), local_player_events: LocalPlayerEvents(tx), game_profile: GameProfileComponent(game_profile), + account: account.to_owned(), }, InConfigurationState, )); @@ -578,6 +593,7 @@ pub struct LocalPlayerBundle { pub received_registries: ReceivedRegistries, pub local_player_events: LocalPlayerEvents, pub game_profile: GameProfileComponent, + pub account: Account, } /// A bundle for the components that are present on a local player that is @@ -603,7 +619,6 @@ pub struct JoinedClientBundle { pub attack: attack::AttackBundle, pub _local_entity: LocalEntity, - pub _loaded: Loaded, } /// A marker component for local players that are currently in the |
