
# -> Add tag
tag @s add OverlyRealistic.Entity.PlayerCorpse_HasDroppedInventory

# -> Cache data
data modify storage overly_realistic:player_corpse dead_player_data set from entity @s data
data remove storage overly_realistic:player_corpse dead_player_data.equipment.body

# -> If corpse has no items then return
scoreboard players set #or.player_corpse_inventory_data OverlyRealistic.General.ArbitraryData 0
scoreboard players set #or.player_corpse_equipment_data OverlyRealistic.General.ArbitraryData 0

execute store result score #or.player_corpse_inventory_data OverlyRealistic.General.ArbitraryData \
    if data storage overly_realistic:player_corpse dead_player_data.inventory[{}]
execute store result score #or.player_corpse_equipment_data OverlyRealistic.General.ArbitraryData \
    if data storage overly_realistic:player_corpse dead_player_data.equipment[{}]

execute if score #or.player_corpse_inventory_data OverlyRealistic.General.ArbitraryData matches 0 \
    if score #or.player_corpse_equipment_data OverlyRealistic.General.ArbitraryData matches 0 run \
        return fail


# --> Part I

# -> Set box
data remove block -29999987 1 -29999987 Items
setblock -29999987 1 -29999987 minecraft:yellow_shulker_box

# -> Get player inventory
data modify block -29999987 1 -29999987 Items set from storage overly_realistic:player_corpse dead_player_data.inventory

# -> Create item
summon item ~ ~ ~ {Tags:["OverlyRealistic.Player.PlayerInventoryHolder0"],Item:{id:"minecraft:stick",count:1}}
execute as @n[type=item,tag=OverlyRealistic.Player.PlayerInventoryHolder0,distance=..1] run \
    function overly_realistic:general/downing/death_item_drop/item_create_0

# -> Data removal
#data modify storage overly_realistic:player_corpse temp_player_data_restoration.inventory set from storage overly_realistic:player_corpse dead_player_data.inventory

data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]

data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]
data remove storage overly_realistic:player_corpse dead_player_data.inventory[0]

# -> Debug
#data modify storage test_removal Inventory set from storage overly_realistic:player_general temp_player_data_restoration.Inventory
#tellraw @a {color:"blue",storage:"overly_realistic:player_general",nbt:"restorable_deaths.Inventory"}

# --> Part II

# -> Set box
data remove block -29999987 1 -29999987 Items
setblock -29999987 1 -29999987 minecraft:yellow_shulker_box

# -> Get player inventory
data modify storage overly_realistic:player_corpse dead_player_data.inventory[{Slot:27b}].Slot set value 0b
data modify storage overly_realistic:player_corpse dead_player_data.inventory[{Slot:28b}].Slot set value 1b
data modify storage overly_realistic:player_corpse dead_player_data.inventory[{Slot:29b}].Slot set value 2b
data modify storage overly_realistic:player_corpse dead_player_data.inventory[{Slot:30b}].Slot set value 3b
data modify storage overly_realistic:player_corpse dead_player_data.inventory[{Slot:31b}].Slot set value 4b
data modify storage overly_realistic:player_corpse dead_player_data.inventory[{Slot:32b}].Slot set value 5b
data modify storage overly_realistic:player_corpse dead_player_data.inventory[{Slot:33b}].Slot set value 6b
data modify storage overly_realistic:player_corpse dead_player_data.inventory[{Slot:34b}].Slot set value 7b
data modify storage overly_realistic:player_corpse dead_player_data.inventory[{Slot:35b}].Slot set value 8b

data modify block -29999987 1 -29999987 Items set from storage overly_realistic:player_corpse dead_player_data.inventory

#tellraw @a {color:"red",storage:"overly_realistic:player_general",nbt:"restorable_deaths.Inventory"}

# -> Create item
summon item ~ ~ ~ {Tags:["OverlyRealistic.Player.PlayerInventoryHolder1"],Item:{id:"minecraft:stick",count:1}}
execute as @n[type=item,tag=OverlyRealistic.Player.PlayerInventoryHolder1,distance=..1] run \
    function overly_realistic:general/downing/death_item_drop/item_create_0

# -> Data restore
#data modify storage overly_realistic:player_corpse dead_player_data.inventory set from storage overly_realistic:player_general temp_player_data_restoration.Inventory

# --> Part III

# -> Set box
data remove block -29999987 1 -29999987 Items
setblock -29999987 1 -29999987 minecraft:yellow_shulker_box

# -> Get & restore player equipment
item replace block -29999987 1 -29999987 container.0 from entity @s armor.head
item replace block -29999987 1 -29999987 container.1 from entity @s armor.chest
item replace block -29999987 1 -29999987 container.2 from entity @s armor.legs
item replace block -29999987 1 -29999987 container.3 from entity @s armor.feet

item replace block -29999987 1 -29999987 container.4 from entity @s weapon.offhand

# -> Create item
summon item ~ ~ ~ {Tags:["OverlyRealistic.Player.PlayerInventoryHolder2"],Item:{id:"minecraft:stick",count:1}}
execute as @n[type=item,tag=OverlyRealistic.Player.PlayerInventoryHolder2,distance=..1] run \
    function overly_realistic:general/downing/death_item_drop/item_create_0

# -> Remove additional data
item replace entity @s weapon.mainhand with air
item replace entity @s weapon.offhand with air

item replace entity @s armor.head with air
item replace entity @s armor.chest with air
item replace entity @s armor.legs with air
item replace entity @s armor.feet with air

data remove entity @s data
