Skip spawn of non visible player characters (#100)

* Skip spawn of non visible player characters
* Don't spawn player if player list is `None`

Co-authored-by: Ryan <ryanj00a@gmail.com>
This commit is contained in:
Daniel Huth 2022-10-09 11:12:30 +13:00 committed by GitHub
parent 153cde1a04
commit e13d0ffe7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 9 deletions

View file

@ -188,8 +188,8 @@ pub struct Client<C: Config> {
username: String,
textures: Option<SignedPlayerTextures>,
world: WorldId,
player_list: Option<PlayerListId>,
old_player_list: Option<PlayerListId>,
new_player_list: Option<PlayerListId>,
position: Vec3<f64>,
old_position: Vec3<f64>,
/// Measured in m/s.
@ -271,7 +271,7 @@ impl<C: Config> Client<C> {
textures: ncd.textures,
world: WorldId::default(),
old_player_list: None,
new_player_list: None,
player_list: None,
position: Vec3::default(),
old_position: Vec3::default(),
velocity: Vec3::default(),
@ -334,14 +334,14 @@ impl<C: Config> Client<C> {
/// Gets the player list this client sees.
pub fn player_list(&self) -> Option<&PlayerListId> {
self.new_player_list.as_ref()
self.player_list.as_ref()
}
/// Sets the player list this client sees.
///
/// The previous player list ID is returned.
pub fn set_player_list(&mut self, id: impl Into<Option<PlayerListId>>) -> Option<PlayerListId> {
mem::replace(&mut self.new_player_list, id.into())
mem::replace(&mut self.player_list, id.into())
}
/// Sets if this client sees the world as superflat. Superflat worlds have
@ -1035,7 +1035,7 @@ impl<C: Config> Client<C> {
if self.created_this_tick() {
self.bits.set_spawn(false);
if let Some(id) = &self.new_player_list {
if let Some(id) = &self.player_list {
player_lists
.get(id)
.initial_packets(|p| send_packet(&mut self.send, p));
@ -1118,7 +1118,7 @@ impl<C: Config> Client<C> {
}
// If the player list was changed...
if self.old_player_list != self.new_player_list {
if self.old_player_list != self.player_list {
// Delete existing entries from old player list.
if let Some(id) = &self.old_player_list {
player_lists
@ -1127,14 +1127,14 @@ impl<C: Config> Client<C> {
}
// Get initial packets for new player list.
if let Some(id) = &self.new_player_list {
if let Some(id) = &self.player_list {
player_lists
.get(id)
.initial_packets(|p| send_packet(&mut self.send, p));
}
self.old_player_list = self.new_player_list.clone();
} else if let Some(id) = &self.new_player_list {
self.old_player_list = self.player_list.clone();
} else if let Some(id) = &self.player_list {
// Update current player list.
player_lists
.get(id)
@ -1452,6 +1452,17 @@ impl<C: Config> Client<C> {
let entity = entities
.get(id)
.expect("entity IDs in spatial index should be valid at this point");
// Skip spawning players not in the player list because they would be invisible
// otherwise.
if entity.kind() == EntityKind::Player {
if let Some(list_id) = &self.player_list {
player_lists.get(list_id).entry(entity.uuid())?;
} else {
return None;
}
}
if entity.kind() != EntityKind::Marker
&& entity.uuid() != self.uuid
&& self.loaded_entities.insert(id)

View file

@ -222,6 +222,20 @@ impl<C: Config> PlayerList<C> {
}
}
/// Returns a reference to the entry with the given UUID.
///
/// If the entry does not exist, `None` is returned.
pub fn entry(&self, uuid: Uuid) -> Option<&PlayerListEntry> {
self.entries.get(&uuid)
}
/// Returns a mutable reference to the entry with the given UUID.
///
/// If the entry does not exist, `None` is returned.
pub fn entry_mut(&mut self, uuid: Uuid) -> Option<&mut PlayerListEntry> {
self.entries.get_mut(&uuid)
}
/// Returns an iterator over all entries in an unspecified order.
pub fn entries(&self) -> impl Iterator<Item = (Uuid, &PlayerListEntry)> + '_ {
self.entries.iter().map(|(k, v)| (*k, v))