@@ -15,6 +15,7 @@ use hyperion::{
15
15
Compose , ConnectionId , agnostic,
16
16
packets:: { BossBarAction , BossBarS2c } ,
17
17
} ,
18
+ runtime:: AsyncRuntime ,
18
19
simulation:: {
19
20
PacketState , Pitch , Player , Position , Velocity , Xp , Yaw ,
20
21
blocks:: Blocks ,
@@ -43,7 +44,7 @@ use hyperion_utils::{EntityExt, LifetimeHandle};
43
44
use tracing:: info_span;
44
45
use valence_protocol:: packets:: play:: player_position_look_s2c:: PlayerPositionLookFlags ;
45
46
46
- use super :: spawn:: { avoid_blocks, is_valid_spawn_block} ;
47
+ use super :: spawn:: { avoid_blocks, find_spawn_position , is_valid_spawn_block} ;
47
48
48
49
#[ derive( Component ) ]
49
50
pub struct AttackModule ;
@@ -535,26 +536,33 @@ impl Module for AttackModule {
535
536
pos_vec. push ( * candidate_pos) ;
536
537
} ) ;
537
538
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
+ } ;
539
561
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 ( ) ;
558
566
} ,
559
567
) ;
560
568
0 commit comments