11use azalea:: {
22 BlockPos , BotClientExt , SprintDirection , WalkDirection ,
3+ core:: hit_result:: HitResult ,
34 entity:: Position ,
45 interact:: HitResultComponent ,
56 pathfinder:: {
6- ExecutingPath , GotoEvent , Pathfinder , PathfinderClientExt ,
7+ ExecutingPath , Pathfinder , PathfinderClientExt ,
78 goals:: { BlockPosGoal , Goal , InverseGoal , RadiusGoal , ReachBlockPosGoal , XZGoal , YGoal } ,
89 } ,
910 protocol:: packets:: game:: { ServerboundPlayerCommand , s_player_command:: Action } ,
1011 world:: MinecraftEntityId ,
1112} ;
12- use log:: error;
1313use mlua:: { FromLua , Lua , Result , Table , UserDataRef , Value } ;
1414
1515use super :: { Client , Direction , Vec3 } ;
@@ -38,11 +38,13 @@ fn to_goal(lua: &Lua, client: &Client, data: Table, options: &Table, kind: u8) -
3838 } )
3939 }
4040 2 => {
41+ let distance = data. get ( "distance" ) . unwrap_or ( 4.5 ) ;
4142 let pos = Vec3 :: from_lua ( Value :: Table ( data) , lua) ?;
42- Box :: new ( ReachBlockPosGoal {
43- pos : BlockPos :: new ( pos. x as i32 , pos. y as i32 , pos. z as i32 ) ,
44- chunk_storage : client. world ( ) . read ( ) . chunks . clone ( ) ,
45- } )
43+ Box :: new ( ReachBlockPosGoal :: new_with_distance (
44+ BlockPos :: new ( pos. x as i32 , pos. y as i32 , pos. z as i32 ) ,
45+ distance,
46+ client. world ( ) . read ( ) . chunks . clone ( ) ,
47+ ) )
4648 }
4749 3 => Box :: new ( XZGoal {
4850 x : data. get ( "x" ) ?,
@@ -70,11 +72,7 @@ pub fn go_to_reached(_lua: &Lua, client: &Client) -> Result<bool> {
7072 Ok ( client. is_goto_target_reached ( ) )
7173}
7274
73- pub async fn go_to_wait_until_reached (
74- _lua : Lua ,
75- client : UserDataRef < Client > ,
76- ( ) : ( ) ,
77- ) -> Result < ( ) > {
75+ pub async fn wait_until_goal_reached ( _lua : Lua , client : UserDataRef < Client > , ( ) : ( ) ) -> Result < ( ) > {
7876 client. wait_until_goto_target_reached ( ) . await ;
7977 Ok ( ( ) )
8078}
@@ -121,11 +119,7 @@ pub async fn start_go_to(
121119 } else {
122120 client. start_goto ( goal) ;
123121 }
124- while client. get_tick_broadcaster ( ) . recv ( ) . await . is_ok ( ) {
125- if client. ecs . lock ( ) . get :: < GotoEvent > ( client. entity ) . is_none ( ) {
126- break ;
127- }
128- }
122+ let _ = client. get_tick_broadcaster ( ) . recv ( ) . await ;
129123
130124 Ok ( ( ) )
131125}
@@ -148,16 +142,19 @@ pub fn jump(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
148142}
149143
150144pub fn looking_at ( lua : & Lua , client : & Client ) -> Result < Option < Table > > {
151- let result = client. component :: < HitResultComponent > ( ) ;
152- Ok ( if result. miss {
153- None
154- } else {
155- let table = lua. create_table ( ) ?;
156- table. set ( "position" , Vec3 :: from ( result. block_pos ) ) ?;
157- table. set ( "inside" , result. inside ) ?;
158- table. set ( "world_border" , result. world_border ) ?;
159- Some ( table)
160- } )
145+ Ok (
146+ if let HitResult :: Block ( ref result) = * client. component :: < HitResultComponent > ( ) {
147+ let table = lua. create_table ( ) ?;
148+ table. set ( "direction" , Vec3 :: from ( result. direction . normal ( ) ) ) ?;
149+ table. set ( "inside" , result. inside ) ?;
150+ table. set ( "location" , Vec3 :: from ( result. location ) ) ?;
151+ table. set ( "position" , Vec3 :: from ( result. block_pos ) ) ?;
152+ table. set ( "world_border" , result. world_border ) ?;
153+ Some ( table)
154+ } else {
155+ None
156+ } ,
157+ )
161158}
162159
163160pub fn look_at ( _lua : & Lua , client : & Client , position : Vec3 ) -> Result < ( ) > {
@@ -215,17 +212,15 @@ pub fn set_position(_lua: &Lua, client: &Client, new_position: Vec3) -> Result<(
215212}
216213
217214pub fn set_sneaking ( _lua : & Lua , client : & Client , sneaking : bool ) -> Result < ( ) > {
218- if let Err ( error ) = client. write_packet ( ServerboundPlayerCommand {
215+ client. write_packet ( ServerboundPlayerCommand {
219216 id : client. component :: < MinecraftEntityId > ( ) ,
220217 action : if sneaking {
221218 Action :: PressShiftKey
222219 } else {
223220 Action :: ReleaseShiftKey
224221 } ,
225222 data : 0 ,
226- } ) {
227- error ! ( "failed to send PlayerCommand packet: {error:?}" ) ;
228- }
223+ } ) ;
229224 Ok ( ( ) )
230225}
231226
@@ -244,13 +239,11 @@ pub fn stop_pathfinding(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
244239}
245240
246241pub fn stop_sleeping ( _lua : & Lua , client : & Client , ( ) : ( ) ) -> Result < ( ) > {
247- if let Err ( error ) = client. write_packet ( ServerboundPlayerCommand {
242+ client. write_packet ( ServerboundPlayerCommand {
248243 id : client. component :: < MinecraftEntityId > ( ) ,
249244 action : Action :: StopSleeping ,
250245 data : 0 ,
251- } ) {
252- error ! ( "failed to send PlayerCommand packet: {error:?}" ) ;
253- }
246+ } ) ;
254247 Ok ( ( ) )
255248}
256249
0 commit comments