Skip to content

Commit

Permalink
Fix crashes (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrarier2111 authored Mar 3, 2024
1 parent a4e5d42 commit 9f9d1b9
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<MouseButtons>().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::<MouseButtons>().unwrap();
mouse_buttons.left = true;
}
} else {
self.inventory_context.write().on_click()
}
Expand All @@ -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::<MouseButtons>().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::<MouseButtons>().unwrap();
mouse_buttons.left = false;
}
}
// TODO: Pass events into inventory context when not focused
}
Expand Down Expand Up @@ -1405,19 +1411,25 @@ 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::<MouseButtons>().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::<MouseButtons>().unwrap();
mouse_buttons.right = true;
}
}
// TODO: Pass events into inventory context when not focused
}

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::<MouseButtons>().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::<MouseButtons>().unwrap();
mouse_buttons.right = false;
}
}
// TODO: Pass events into inventory context when not focused
}
Expand Down

0 comments on commit 9f9d1b9

Please sign in to comment.