@@ -21,6 +21,7 @@ use crate::world::world::VIEW_DISTANCE;
2121use crate :: world:: world:: { World , WorldExtension } ;
2222use fstr:: FString ;
2323use glam:: { dvec3, DVec3 , IVec3 , Vec3 } ;
24+ use std:: cell:: UnsafeCell ;
2425use std:: collections:: HashMap ;
2526use std:: f32:: consts:: PI ;
2627use 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 {
0 commit comments