From 0c245fd8a7d24ed3475ea3eabd4ff61b2295de2b Mon Sep 17 00:00:00 2001 From: Jupeyy Date: Wed, 1 Jan 2025 17:15:06 +0100 Subject: [PATCH] Fix player name change check should ignore own player --- game/vanilla/src/entities/character/player.rs | 5 +++-- game/vanilla/src/state.rs | 20 ++++++++++--------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/game/vanilla/src/entities/character/player.rs b/game/vanilla/src/entities/character/player.rs index 0057236..d8a7901 100644 --- a/game/vanilla/src/entities/character/player.rs +++ b/game/vanilla/src/entities/character/player.rs @@ -196,9 +196,10 @@ pub mod player { pub fn contains_key(&self, id: &PlayerId) -> bool { self.players.get(id).is_some() } - pub fn any_with_name(&self, name: &str) -> bool { + pub fn any_with_name(&self, except_id: Option, name: &str) -> bool { self.players - .values() + .iter() + .filter_map(|(id, p)| (Some(*id) != except_id).then_some(p)) .any(|p| p.player_info.player_info.name.as_str() == name) } diff --git a/game/vanilla/src/state.rs b/game/vanilla/src/state.rs index cb1f67c..f1f9903 100644 --- a/game/vanilla/src/state.rs +++ b/game/vanilla/src/state.rs @@ -1428,15 +1428,20 @@ pub mod state { } } - fn chech_player_info(&self, mut info: NetworkCharacterInfo) -> NetworkCharacterInfo { + fn chech_player_info( + &self, + mut info: NetworkCharacterInfo, + player_id: Option, + ) -> NetworkCharacterInfo { // check if the name is already in use let name_exists = |name: &str| { self.game.stages.values().any(|s| { s.world .characters - .values() + .iter() + .filter_map(|(id, c)| (Some(*id) != player_id).then_some(c)) .any(|c| c.player_info.player_info.name.as_str() == name) - }) || self.game.spectator_players.any_with_name(name) + }) || self.game.spectator_players.any_with_name(player_id, name) }; let mut name = info.name.clone(); if name_exists(name.as_str()) { @@ -1917,7 +1922,7 @@ pub mod state { let player_id = self.id_generator.next_id(); let stage_0_id = self.stage_0_id; - let character_info = self.chech_player_info(client_player_info.info.clone()); + let character_info = self.chech_player_info(client_player_info.info.clone(), None); self.game .stages @@ -2071,7 +2076,7 @@ pub mod state { version: NonZeroU64, ) { let old_info = &mut None; - let new_info = self.chech_player_info(info.clone()); + let new_info = self.chech_player_info(info.clone(), Some(*id)); let mut stage_id = self.stage_0_id; if let Some(player) = self.game.players.player(id) { stage_id = player.stage_id(); @@ -2138,10 +2143,7 @@ pub mod state { old_info .take() .and_then(|(old_name, old_skin, old_skin_info)| { - (old_name != new_info.name - || old_skin != new_info.skin - || old_skin_info != new_info.skin_info) - .then_some((old_name, old_skin, old_skin_info)) + (old_name != new_info.name).then_some((old_name, old_skin, old_skin_info)) }) { let stage = self.game.stages.get(&stage_id).unwrap();