@@ -15,6 +15,7 @@ use hyperion::{
1515 Compose , ConnectionId , agnostic,
1616 packets:: { BossBarAction , BossBarS2c } ,
1717 } ,
18+ runtime:: AsyncRuntime ,
1819 simulation:: {
1920 PacketState , Pitch , Player , Position , Velocity , Xp , Yaw ,
2021 blocks:: Blocks ,
@@ -43,7 +44,7 @@ use hyperion_utils::{EntityExt, LifetimeHandle};
4344use tracing:: info_span;
4445use valence_protocol:: packets:: play:: player_position_look_s2c:: PlayerPositionLookFlags ;
4546
46- use super :: spawn:: { avoid_blocks, is_valid_spawn_block} ;
47+ use super :: spawn:: { avoid_blocks, find_spawn_position , is_valid_spawn_block} ;
4748
4849#[ derive( Component ) ]
4950pub struct AttackModule ;
@@ -535,26 +536,33 @@ impl Module for AttackModule {
535536 pos_vec. push ( * candidate_pos) ;
536537 } ) ;
537538
538- let random_index = fastrand:: usize ( ..pos_vec. len ( ) ) ;
539+ let respawn_pos = if let Some ( random_mate) = fastrand:: choice ( pos_vec) {
540+ // Spawn the player near a teammate
541+ get_respawn_pos ( query. world , & random_mate)
542+ } else {
543+ // There are no other teammates, so spawn the player in a random location
544+ query. world . get :: < & AsyncRuntime > ( |runtime| {
545+ query. world . get :: < & mut Blocks > ( |blocks| {
546+ find_spawn_position ( blocks, runtime, & avoid_blocks ( ) )
547+ . as_dvec3 ( )
548+ } )
549+ } )
550+ } ;
551+
552+ * position = Position :: from ( respawn_pos. as_vec3 ( ) ) ;
553+
554+ let pkt_teleport = play:: PlayerPositionLookS2c {
555+ position : respawn_pos,
556+ yaw : * * yaw,
557+ pitch : * * pitch,
558+ flags : PlayerPositionLookFlags :: default ( ) ,
559+ teleport_id : VarInt ( fastrand:: i32 ( ..) ) ,
560+ } ;
539561
540- if let Some ( random_mate) = pos_vec. get ( random_index) {
541- let respawn_pos = get_respawn_pos ( query. world , random_mate) ;
542-
543- * position = Position :: from ( respawn_pos. as_vec3 ( ) ) ;
544-
545- let pkt_teleport = play:: PlayerPositionLookS2c {
546- position : respawn_pos,
547- yaw : * * yaw,
548- pitch : * * pitch,
549- flags : PlayerPositionLookFlags :: default ( ) ,
550- teleport_id : VarInt ( fastrand:: i32 ( ..) ) ,
551- } ;
552-
553- query
554- . compose
555- . unicast ( & pkt_teleport, * connection, query. system )
556- . unwrap ( ) ;
557- }
562+ query
563+ . compose
564+ . unicast ( & pkt_teleport, * connection, query. system )
565+ . unwrap ( ) ;
558566 } ,
559567 ) ;
560568
0 commit comments