From 9f9d1b92d9e26e3cd5dd521d8e8a3ece32f7a21e Mon Sep 17 00:00:00 2001 From: terrarier2111 <58695553+terrarier2111@users.noreply.github.com> Date: Mon, 4 Mar 2024 00:44:47 +0100 Subject: [PATCH] Fix crashes (#308) --- src/server/mod.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/server/mod.rs b/src/server/mod.rs index 974acc0d..a34b8650 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1336,9 +1336,12 @@ impl Server { pub fn on_left_click(&self, focused: bool) { if focused { let mut entities = self.entities.write(); - let mut player = entities.world.entity_mut(self.player.read().unwrap().1); - let mut mouse_buttons = player.get_mut::().unwrap(); - mouse_buttons.left = true; + // check if the player exists, as it might not be initialized very early on server join + if let Some(player) = self.player.read().as_ref() { + let mut player = entities.world.entity_mut(player.1); + let mut mouse_buttons = player.get_mut::().unwrap(); + mouse_buttons.left = true; + } } else { self.inventory_context.write().on_click() } @@ -1347,9 +1350,12 @@ impl Server { pub fn on_release_left_click(&self, focused: bool) { if focused { let mut entities = self.entities.write(); - let mut player = entities.world.entity_mut(self.player.read().unwrap().1); - let mut mouse_buttons = player.get_mut::().unwrap(); - mouse_buttons.left = false; + // check if the player exists, as it might not be initialized very early on server join + if let Some(player) = self.player.read().as_ref() { + let mut player = entities.world.entity_mut(player.1); + let mut mouse_buttons = player.get_mut::().unwrap(); + mouse_buttons.left = false; + } } // TODO: Pass events into inventory context when not focused } @@ -1405,9 +1411,12 @@ impl Server { } let mut entities = self.entities.write(); - let mut player = entities.world.entity_mut(self.player.read().unwrap().1); - let mut mouse_buttons = player.get_mut::().unwrap(); - mouse_buttons.right = true; + // check if the player exists, as it might not be initialized very early on server join + if let Some(player) = self.player.read().as_ref() { + let mut player = entities.world.entity_mut(player.1); + let mut mouse_buttons = player.get_mut::().unwrap(); + mouse_buttons.right = true; + } } // TODO: Pass events into inventory context when not focused } @@ -1415,9 +1424,12 @@ impl Server { pub fn on_release_right_click(&self, focused: bool) { if focused { let mut entities = self.entities.write(); - let mut player = entities.world.entity_mut(self.player.read().unwrap().1); - let mut mouse_buttons = player.get_mut::().unwrap(); - mouse_buttons.right = false; + // check if the player exists, as it might not be initialized very early on server join + if let Some(player) = self.player.read().as_ref() { + let mut player = entities.world.entity_mut(player.1); + let mut mouse_buttons = player.get_mut::().unwrap(); + mouse_buttons.right = false; + } } // TODO: Pass events into inventory context when not focused }