Skip to content

Commit a73f3e0

Browse files
authored
fix: respawning without teammates (#828)
This fixes a panic that would've occured from fastrand due to having an empty range due to there being no teammates.
1 parent efb0f19 commit a73f3e0

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

events/tag/src/module/attack.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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};
4344
use tracing::info_span;
4445
use 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)]
4950
pub 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

events/tag/src/module/spawn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl Module for SpawnModule {
8282
}
8383
}
8484

85-
fn find_spawn_position(
85+
pub fn find_spawn_position(
8686
blocks: &mut Blocks,
8787
runtime: &AsyncRuntime,
8888
avoid_blocks: &RoaringBitmap,

0 commit comments

Comments
 (0)