From c4590a45fcb1ff3b5073a8c1e427fed3cfa816f6 Mon Sep 17 00:00:00 2001 From: Ryan Date: Mon, 4 Jul 2022 16:30:37 -0700 Subject: [PATCH] Fix entity create/delete bugs --- examples/conway.rs | 16 +++++++++------- examples/cow_sphere.rs | 1 + examples/terrain.rs | 11 ++++++----- src/entity.rs | 5 ++++- src/slotmap.rs | 1 - 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/examples/conway.rs b/examples/conway.rs index 854bbf1..d04bfd3 100644 --- a/examples/conway.rs +++ b/examples/conway.rs @@ -9,8 +9,8 @@ use num::Integer; use rayon::iter::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator}; use valence::client::{ClientEvent, ClientId, GameMode}; use valence::config::{Config, ServerListPing}; -use valence::entity::EntityMeta; use valence::entity::meta::Pose; +use valence::entity::EntityMeta; use valence::text::Color; use valence::{ async_trait, ident, Biome, BlockState, Dimension, DimensionId, EntityId, EntityType, Server, @@ -118,12 +118,6 @@ impl Config for Game { server.clients.retain(|client_id, client| { if client.created_tick() == server.shared.current_tick() { - if client.is_disconnected() { - self.player_count.fetch_sub(1, Ordering::SeqCst); - player_entities.remove(&client_id); - return false; - } - if self .player_count .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |count| { @@ -161,6 +155,14 @@ impl Config for Game { client.send_message("Hold the left mouse button to bring blocks to life.".italic()); } + if client.is_disconnected() { + self.player_count.fetch_sub(1, Ordering::SeqCst); + let id = player_entities.remove(&client_id).unwrap(); + server.entities.delete(id); + world.meta.player_list_mut().remove(client.uuid()); + return false; + } + true }); diff --git a/examples/cow_sphere.rs b/examples/cow_sphere.rs index 5a30008..6ca80c8 100644 --- a/examples/cow_sphere.rs +++ b/examples/cow_sphere.rs @@ -108,6 +108,7 @@ impl Config for Game { if client.is_disconnected() { self.player_count.fetch_sub(1, Ordering::SeqCst); + world.meta.player_list_mut().remove(client.uuid()); false } else { true diff --git a/examples/terrain.rs b/examples/terrain.rs index 443beb8..2d9bbb4 100644 --- a/examples/terrain.rs +++ b/examples/terrain.rs @@ -80,11 +80,6 @@ impl Config for Game { let mut chunks_to_unload = HashSet::<_>::from_iter(world.chunks.iter().map(|t| t.0)); server.clients.retain(|_, client| { - if client.is_disconnected() { - self.player_count.fetch_sub(1, Ordering::SeqCst); - return false; - } - if client.created_tick() == server.shared.current_tick() { if self .player_count @@ -118,6 +113,12 @@ impl Config for Game { ); } + if client.is_disconnected() { + self.player_count.fetch_sub(1, Ordering::SeqCst); + world.meta.player_list_mut().remove(client.uuid()); + return false; + } + let dist = client.view_distance(); let p = client.position(); diff --git a/src/entity.rs b/src/entity.rs index 43a20e7..b0e6fff 100644 --- a/src/entity.rs +++ b/src/entity.rs @@ -12,7 +12,7 @@ pub use types::{EntityMeta, EntityType}; use uuid::Uuid; use vek::{Aabb, Vec3}; -pub use crate::protocol::packets::play::s2c::EntityEvent as EntityEvent; +pub use crate::protocol::packets::play::s2c::EntityEvent; use crate::protocol::packets::play::s2c::{ AddEntity, AddExperienceOrb, AddPlayer, S2cPlayPacket, SetEntityMetadata, }; @@ -70,6 +70,9 @@ impl Entities { events: Vec::new(), }); + // TODO check for overflowing version? + self.network_id_to_entity.insert(k.version(), k.index()); + ve.insert(EntityId(k)); Some((EntityId(k), e)) diff --git a/src/slotmap.rs b/src/slotmap.rs index 35f861f..9fd92a1 100644 --- a/src/slotmap.rs +++ b/src/slotmap.rs @@ -56,7 +56,6 @@ impl Key { } } - #[allow(unused)] pub fn index(self) -> u32 { self.index }