
# -> Temp tag
tag @s add OverlyRealistic.Player.TempPlayer

# -> Operate on downed player parent ent
execute as @e[type=block_display,tag=OverlyRealistic.DownedPlayerParentEnt] \
    if score @s OverlyRealistic.Player.DeathID = @p[tag=OverlyRealistic.Player.TempPlayer,sort=arbitrary] OverlyRealistic.Player.DeathID \
    at @s run \
        function overly_realistic:general/downing/downing_parent_ent_operate

# -> Reset motion
execute if predicate overly_realistic:player/actions/player_is_moving_horizontal \
    at @s run \
        tp @s @s

# -> If player has not gotten their attributes back, then restore them
execute unless entity @s[tag=OverlyRealistic.Player.RestoredModifiersPostDeath] run \
    function overly_realistic:general/downing/attribute_restoration/restoration_start

# -> If another player is near then add to cooldown timer
execute if entity @p[tag=!OverlyRealistic.Player.TempPlayer,distance=..2,predicate=overly_realistic:player/actions/player_is_shifting] run \
    scoreboard players add @s OverlyRealistic.Player.DeathTimer 2
execute if entity @p[tag=!OverlyRealistic.Player.TempPlayer,scores={OverlyRealistic.Player.WisdomStats.Healthcare=2..},distance=..2,predicate=overly_realistic:player/actions/player_is_shifting] run \
    scoreboard players add @s OverlyRealistic.Player.DeathTimer 2

# -> Advancement
advancement grant @p[tag=!OverlyRealistic.Player.TempPlayer,distance=..2,predicate=overly_realistic:player/actions/player_is_shifting] only overly_realistic:advancements/livelyhood/helping_hand

# -> Untag player
tag @s remove OverlyRealistic.Player.TempPlayer

# -> Decrease cooldown timer
scoreboard players remove @s OverlyRealistic.Player.DeathTimer 1

# -> Actionbar display
title @s actionbar [{"bold":false,"color":"#A80000","text":"☠ "},{"bold":true,"text":"You are down! Dying in: "},{"bold":true,"score":{"name":"@s","objective":"OverlyRealistic.Player.DeathTimer"}},{"bold":true,"text":" / 3000"},{"bold":false,"text":" ☠"}]

# -> Function tag
function #overly_realistic:mechanics/player_downed_operate

# -> If timer is insignificant then return
execute if score @s OverlyRealistic.Player.DeathTimer matches 1..2999 run \
    return fail

# -> If timer >= 1000 then save player
execute if score @s OverlyRealistic.Player.DeathTimer matches 3000.. run \
    return run \
        function overly_realistic:general/downing/downing_end_saved

# -> If timer <= 0 then kill off player
execute if score @s OverlyRealistic.Player.DeathTimer matches ..0 run \
    function overly_realistic:general/downing/downing_end_killed
