Skip to content

Commit 7ba8730

Browse files
committed
made OpenContainer not manually take ownership when using it
1 parent 1c28b38 commit 7ba8730

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

server/src/player/packet_handling.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,8 @@ impl ProcessPacket for serverbound::CloseWindow {
145145

146146
impl ProcessPacket for ClickWindow {
147147
fn process<P : PlayerExtension>(&self, player: &mut Player<P>) {
148-
// need to take ownership because of borrow checker
149-
let mut container = std::mem::replace(&mut player.open_container, OpenContainer::None);
148+
let container = unsafe { player.open_container.get().as_mut().unwrap() };
150149
container.click_window(player, self);
151-
player.open_container = container;
152150
}
153151
}
154152

server/src/player/player.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::world::world::VIEW_DISTANCE;
2121
use crate::world::world::{World, WorldExtension};
2222
use fstr::FString;
2323
use glam::{dvec3, DVec3, IVec3, Vec3};
24+
use std::cell::UnsafeCell;
2425
use std::collections::HashMap;
2526
use std::f32::consts::PI;
2627
use std::ptr::NonNull;
@@ -82,8 +83,7 @@ pub struct Player<E : PlayerExtension> {
8283
pub is_sneaking: bool,
8384

8485
pub window_id: i8,
85-
// todo: make certain areas not need to claim ownership
86-
pub open_container: OpenContainer<E>,
86+
pub(super) open_container: UnsafeCell<OpenContainer<E>>,
8787
pub inventory: Inventory<E::Item>,
8888
pub held_slot: u8,
8989

@@ -124,10 +124,9 @@ impl<E : PlayerExtension> Player<E> {
124124
last_position: position,
125125
last_yaw: yaw,
126126
last_pitch: pitch,
127-
128127
is_sneaking: false,
129128

130-
open_container: OpenContainer::None,
129+
open_container: UnsafeCell::new(OpenContainer::None),
131130
window_id: 0,
132131
inventory: Inventory::new(),
133132
held_slot: 0,
@@ -288,16 +287,20 @@ impl<E : PlayerExtension> Player<E> {
288287
}
289288

290289
pub fn open_container(&mut self, mut container: OpenContainer<E>) {
291-
if let OpenContainer::Menu(_) = self.open_container {
290+
if matches!(*self.open_container.get_mut(), OpenContainer::Menu(_)) {
292291
self.write_packet(&clientbound::CloseWindow {
293292
window_id: self.window_id,
294293
})
295-
}
294+
};
296295
self.window_id += 1;
297296
container.open(self);
298-
self.open_container = container;
297+
self.open_container = UnsafeCell::new(container);
299298
}
300-
299+
300+
pub fn get_container(&mut self) -> &mut OpenContainer<E> {
301+
self.open_container.get_mut()
302+
}
303+
301304
pub fn sync_inventory(&mut self) {
302305
let mut items = Vec::new();
303306
for item in self.inventory.items.iter() {
@@ -307,10 +310,8 @@ impl<E : PlayerExtension> Player<E> {
307310
window_id: 0,
308311
items,
309312
});
310-
// take ownership
311-
let mut container = std::mem::replace(&mut self.open_container, OpenContainer::None);
313+
let container = unsafe { self.open_container.get().as_mut().unwrap() };
312314
container.sync_container(self);
313-
self.open_container = container;
314315
}
315316

316317
pub fn rotation_vec(&self) -> Vec3 {

src/dungeon/dungeon.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl Dungeon {
194194

195195
pub fn start_dungeon(world: &mut World<Self>) {
196196
for player in world.players.iter_mut() {
197-
if let OpenContainer::Menu(_) = player.open_container {
197+
if let OpenContainer::Menu(_) = player.get_container() {
198198
player.open_container(OpenContainer::None)
199199
}
200200
player.inventory.set_slot(44, Some(DungeonItem::MagicalMap));

0 commit comments

Comments
 (0)