# Detect player respawns and move them back into the border.
# Run as each player every tick.

execute unless data storage bordercraft:state {active:1b} run return 0

# Mark a pending respawn when deathCount increases (this happens at death time).
execute if score @s bc_deaths > @s bc_deaths_last run scoreboard players set @s bc_respawn_pending 1

# Update last seen death count (prevents repeated triggers).
scoreboard players operation @s bc_deaths_last = @s bc_deaths

# Apply respawn safety AFTER the player has actually respawned (time_since_death >= 1).
execute if score @s bc_respawn_pending matches 1.. if score @s bc_time_since_death matches 1.. run function bordercraft:respawn
execute if score @s bc_respawn_pending matches 1.. if score @s bc_time_since_death matches 1.. run scoreboard players set @s bc_respawn_pending 0
