
# -> Reset adv trigger
advancement revoke @s only overly_realistic:player/player_die

# -> Reset var
scoreboard players set @s OverlyRealistic.Player.Deaths 0

# -> Dismount the player if possible
execute if predicate overly_realistic:player/actions/player_riding_possible_entity run \
    ride @s[tag=!OverlyRealistic.Player.IsDowned] dismount

# -> Reset the RestoredModifiersPostDeath tag
tag @s remove OverlyRealistic.Player.RestoredModifiersPostDeath

# -> If player died from being downed then return
execute if score @s OverlyRealistic.Player.HasDiedPostDowning matches 1 run \
    return run \
        function overly_realistic:general/downing/downing_end_killed_post

# -> If the 'Restorable Deaths' setting is enabled and the player has HasDiedPostDowning = 1 then return and operate accordingly
execute if score #or.setting.restorable_deaths OverlyRealistic.Settings.ArbitrarySetting matches 1 \
    if score @s OverlyRealistic.Player.HasDiedPostDowning matches 1 run \
        return run \
            function overly_realistic:main/debug/death_inventories/player_die_final_death

# -> Cache player data
execute unless score #or.setting.restorable_deaths OverlyRealistic.Settings.ArbitrarySetting matches 1 run \
    data modify storage overly_realistic:player_general dead_player_data set from entity @s

# -> If the 'Restorable Deaths' setting is enabled then store player data
execute if score #or.setting.restorable_deaths OverlyRealistic.Settings.ArbitrarySetting matches 1 run \
    function overly_realistic:main/debug/death_inventories/data_store

# -> Reset HasDiedPostDowning
scoreboard players reset @s OverlyRealistic.Player.HasDiedPostDowning

# -> Save attribute data
item replace block -29999985 2 -29999985 container.0 from entity @s armor.body
data modify block -29999985 2 -29999985 Items[{Slot:0b}].components.minecraft:custom_data.attribute_modifiers_on_death set from storage overly_realistic:player_general dead_player_data.attributes
item replace entity @s armor.body from block -29999985 2 -29999985 container.0

# -> If 'normal respawning' is enabled, then return and operate accordingly
execute if score #or.setting.do_normal_respawning OverlyRealistic.Settings.ArbitrarySetting matches 1 run \
    return run \
        function overly_realistic:players/operations/player_die_normal_respawn

# -> Set spawn
spawnpoint @s ~ ~ ~

# -> Debug MSG
#tellraw @a {entity:"@s",nbt:"attributes"}

# -> If player hasnt been downed before, and if settings allow it, then down them
execute unless entity @s[tag=OverlyRealistic.Player.HasBeenDownedBefore] \
    unless score #or.setting.do_normal_respawning OverlyRealistic.Settings.ArbitrarySetting matches 1 run \
        return run \
            function overly_realistic:general/downing/downing_start

# -> Remove physical traumas
function overly_realistic:main/debug/stats/remove_physical_trauma

# -> Reset onDeath
function overly_realistic:main/debug/stats/reset_ondeath_stats

# -> Drop inventory
execute if score #or.setting.player_corpses OverlyRealistic.Settings.ArbitrarySetting matches 0 run \
    function overly_realistic:general/downing/death_item_drop/item_drop_start

# -> Create corpse
execute if score #or.setting.player_corpses OverlyRealistic.Settings.ArbitrarySetting matches 1 run \
    function overly_realistic:general/downing/player_corpse/create_corpse/player_create_corpse

# -> Decrease nearby players' mental health
scoreboard players set #or.central_value_set_mental_health OverlyRealistic.General.ArbitraryData 35
execute as @a[distance=..16] \
    unless score @s OverlyRealistic.Player.WisdomStats.Combat matches 2.. run \
        function overly_realistic:players/mental_health/decrease_mental_health

# -> Decrease attacker's mental health
scoreboard players set #or.central_value_set_mental_health OverlyRealistic.General.ArbitraryData 5
execute on attacker \
    unless score @s OverlyRealistic.Player.WisdomStats.Combat matches 3.. run \
        function overly_realistic:players/mental_health/decrease_mental_health

# -> Remove possible tag
tag @s remove OverlyRealistic.Player.LeftWhileDowned

# -> Set as ghost
function overly_realistic:players/custom_data/spawn_as_ghost

# -> Tag player
tag @s add OverlyRealistic.Player.DeathPost

# -> Delay death screen
schedule function overly_realistic:main/debug/death_inventories/player_die_post_delay 3t
