@@ -25,7 +25,7 @@ use std::{
2525 time:: Duration ,
2626} ;
2727
28- use color_eyre:: eyre:: { bail, Result } ;
28+ use color_eyre:: eyre:: { bail, Context , Result } ;
2929use rand:: Rng ;
3030use serde_json:: json;
3131use thiserror:: Error ;
@@ -51,13 +51,14 @@ use crate::{
5151 Frame , Packet , ProtocolState ,
5252 } ,
5353 server:: window:: { Window , WindowType } ,
54+ world:: Container ,
5455 CrawlState ,
5556} ;
5657
58+ use super :: { entity:: Entity , io:: NetIo } ;
5759#[ cfg( feature = "encryption" ) ]
5860use crate :: protocol:: { datatypes:: Bytes , packets:: login:: PluginRequestC } ;
5961use crate :: protocol:: { PacketDirection , PACKETS } ;
60- use super :: { entity:: Entity , io:: NetIo } ;
6162
6263#[ derive( Debug ) ]
6364pub struct Player {
@@ -148,7 +149,7 @@ impl SharedPlayer {
148149 ) ;
149150
150151 match self . begin_play ( ) . await {
151- Ok ( ( ) ) => debug ! ( "Play loop for {} done ." , self . id( ) ) ,
152+ Ok ( ( ) ) => debug ! ( "Spawned play loop for player {} ." , self . id( ) ) ,
152153 Err ( why) => error ! ( "Failed to play player {}! {why}" , self . id( ) ) ,
153154 }
154155 }
@@ -214,9 +215,12 @@ impl SharedPlayer {
214215 self . 0 . io . tx ( & res) . await ?;
215216 let ping: PingS = self . 0 . io . rx :: < PingS > ( ) . await ?. decode ( ) ?;
216217
217- self . 0 . io . tx ( & PingC {
218- payload : ping. payload
219- } ) . await ?;
218+ self . 0
219+ . io
220+ . tx ( & PingC {
221+ payload : ping. payload ,
222+ } )
223+ . await ?;
220224
221225 Ok ( ( ) )
222226 }
@@ -532,7 +536,10 @@ impl SharedPlayer {
532536 let resource = PACKETS
533537 . get_resource_id ( packet_state, PacketDirection :: Serverbound , frame. id )
534538 . unwrap_or_else ( || {
535- panic ! ( "{} cannot map to any resource in state {:?}" , frame. id, packet_state)
539+ panic ! (
540+ "{} cannot map to any resource in state {:?}" ,
541+ frame. id, packet_state
542+ )
536543 } )
537544 . as_str ( ) ;
538545
@@ -569,7 +576,7 @@ impl SharedPlayer {
569576 }
570577
571578 id => {
572- debug ! (
579+ trace ! (
573580 "Got packet with id {id} from player {}, ignoring" ,
574581 self . 0 . id
575582 ) ;
@@ -583,49 +590,56 @@ impl SharedPlayer {
583590 let crawlstate = self . 0 . crawlstate . clone ( ) ;
584591 let server = crawlstate. get_server ( ) . await ;
585592
586- let x = packet. location . x as i32 ;
587- let y = packet. location . y as i32 ;
588- let z = packet. location . z as i32 ;
593+ let x = packet. location . x ;
594+ let y = packet. location . y ;
595+ let z = packet. location . z ;
589596
590597 debug ! ( "Player {} clicked at {}, {}, {}" , self . id( ) , x, y, z) ;
591598
592599 match server. get_container ( x, y, z) {
593600 None => ( ) ,
594- Some ( container) => {
595- let id = {
596- let mut next_window_id = self . 0 . next_window_id . lock ( ) . await ;
597- let id = * next_window_id;
598- * next_window_id = next_window_id. wrapping_add ( 1 ) ;
599- if * next_window_id == 0 {
600- * next_window_id = 1 ;
601- }
602- id
603- } ;
604-
605- let window = Window {
606- id,
607- kind : WindowType :: Generic9x3 ,
608- title : "Hi" . into ( ) ,
609- } ;
610-
611- self . 0 . io . tx ( & OpenScreenC :: from ( & window) ) . await ?;
612-
613- self . 0
614- . io
615- . tx ( & SetContainerContentC {
616- window_id : id,
617- // FIXME: track this correctly
618- state_id : 0 ,
619- slot_data : container. 0 ,
620- carried_item : Slot :: default ( ) ,
621- } )
622- . await ?;
623-
624- {
625- let mut sw = self . 0 . window . write ( ) . await ;
626- * sw = Some ( window) ;
627- }
601+ Some ( container) => self
602+ . open_container ( container)
603+ . await
604+ . wrap_err_with ( || format ! ( "failed to open container at {x}, {y}, {z}" ) ) ?,
605+ }
606+
607+ Ok ( ( ) )
608+ }
609+
610+ async fn open_container ( & self , container : Container ) -> Result < ( ) > {
611+ let id = {
612+ let mut next_window_id = self . 0 . next_window_id . lock ( ) . await ;
613+ let id = * next_window_id;
614+ * next_window_id = next_window_id. wrapping_add ( 1 ) ;
615+ if * next_window_id == 0 {
616+ * next_window_id = 1 ;
628617 }
618+ id
619+ } ;
620+
621+ let window = Window {
622+ id,
623+ kind : WindowType :: Generic9x3 ,
624+ title : "Hi" . into ( ) ,
625+ } ;
626+
627+ self . 0 . io . tx ( & OpenScreenC :: from ( & window) ) . await ?;
628+
629+ self . 0
630+ . io
631+ . tx ( & SetContainerContentC {
632+ window_id : id,
633+ // FIXME: track this correctly
634+ state_id : 0 ,
635+ slot_data : container. 0 ,
636+ carried_item : Slot :: default ( ) ,
637+ } )
638+ . await ?;
639+
640+ {
641+ let mut sw = self . 0 . window . write ( ) . await ;
642+ * sw = Some ( window) ;
629643 }
630644
631645 Ok ( ( ) )
0 commit comments