#copy the chest up and then destroy it so the items are free
execute at @s run clone ~ ~ ~ ~ ~ ~ ~ 255 ~ replace move
execute at @s run fill ~ 255 ~ ~ 255 ~ air destroy
execute at @s run fill ~ 255 ~ ~ 255 ~ chest
execute positioned ~ 255 ~ run kill @e[nbt={Item:{id:"minecraft:chest"}},distance=..5]
data merge block ~ 255 ~ {Items:[{Slot:0b,id:"minecraft:structure_void",Count:1b},{Slot:1b,id:"minecraft:structure_void",Count:1b},{Slot:2b,id:"minecraft:structure_void",Count:1b},{Slot:3b,id:"minecraft:structure_void",Count:1b},{Slot:4b,id:"minecraft:structure_void",Count:1b},{Slot:5b,id:"minecraft:structure_void",Count:1b},{Slot:6b,id:"minecraft:structure_void",Count:1b},{Slot:7b,id:"minecraft:structure_void",Count:1b},{Slot:8b,id:"minecraft:structure_void",Count:1b},{Slot:9b,id:"minecraft:structure_void",Count:1b},{Slot:10b,id:"minecraft:structure_void",Count:1b},{Slot:11b,id:"minecraft:structure_void",Count:1b},{Slot:12b,id:"minecraft:structure_void",Count:1b},{Slot:13b,id:"minecraft:structure_void",Count:1b},{Slot:14b,id:"minecraft:structure_void",Count:1b},{Slot:15b,id:"minecraft:structure_void",Count:1b},{Slot:16b,id:"minecraft:structure_void",Count:1b},{Slot:17b,id:"minecraft:structure_void",Count:1b},{Slot:18b,id:"minecraft:structure_void",Count:1b},{Slot:19b,id:"minecraft:structure_void",Count:1b},{Slot:20b,id:"minecraft:structure_void",Count:1b},{Slot:21b,id:"minecraft:structure_void",Count:1b},{Slot:22b,id:"minecraft:structure_void",Count:1b},{Slot:23b,id:"minecraft:structure_void",Count:1b},{Slot:24b,id:"minecraft:structure_void",Count:1b},{Slot:25b,id:"minecraft:structure_void",Count:1b},{Slot:26b,id:"minecraft:structure_void",Count:1b}]}
execute positioned ~ 255 ~ run execute as @e[type=item,distance=..5] run scoreboard players add @s KyuDummy 0
execute positioned ~ 255 ~ run execute as @e[type=item,distance=..5] run scoreboard players add @s KyuDummy2 0


#tag certain items as sacrificable
execute at @s positioned ~ 255 ~ run execute as @e[type=item,distance=..5] run function zzzzdonotuse:structures/enchantedcavern/chest/tagassacrifice

#apply the reagant items
execute at @s positioned ~ 255 ~ run execute if entity @e[type=item,distance=..5,tag=ItemIsSaved,tag=!ItemIsSavedAndReagant] run execute as @e[type=item,distance=..5,tag=!ItemIsSaved,tag=!KyuReagant] run function zzzzdonotuse:structures/enchantedcavern/chest/setreagants
execute at @s positioned ~ 255 ~ run execute if entity @e[type=item,distance=..5,tag=ItemIsSaved,tag=!ItemIsSavedAndReagant] run execute as @e[type=item,distance=..5,tag=ItemIsSaved,tag=ItemIsSavedAndReagant,tag=!KyuReagant] run function zzzzdonotuse:structures/enchantedcavern/chest/setreagants
## for all the reagant items from above(iron ingot, gold_ingot, etc), add their own KyuDummy score to all of the save-able item's KyuDummy score, and do the same for KyuDummy2, which is enchantment chance
execute positioned ~ 255 ~ run execute as @e[distance=..10,type=item,tag=KyuReagant] run scoreboard players operation @e[distance=..10,type=item,tag=ItemIsSaved,tag=!KyuReagant] KyuDummy += @s KyuDummy
execute positioned ~ 255 ~ run execute as @e[distance=..10,type=item,tag=KyuReagant] run scoreboard players operation @e[distance=..10,type=item,tag=ItemIsSaved,tag=!KyuReagant] KyuDummy2 += @s KyuDummy2
execute positioned ~ 255 ~ run kill @e[distance=..10,type=item,tag=KyuReagant,tag=!ItemIsSaved]

#do survival chances for the items tagged as sacrificable
execute at @s positioned ~ 255 ~ run execute as @e[type=item,distance=..5,tag=ItemIsSaved] run function zzzzdonotuse:structures/enchantedcavern/chest/saveitemchance

#disassemble the items into materials and enchanted books, and apply effects for certain items
execute at @s positioned ~ 255 ~ run execute as @e[type=item,distance=..5,tag=ItemSave,tag=!ItemSaveEffects] run function zzzzdonotuse:structures/enchantedcavern/chest/disassembleitems


#run single-use effect items, such as blaze powder and ghast tears
execute at @s[tag=!GA3] positioned ~ 255 ~ run execute as @e[type=item,distance=..5,tag=ItemSave,limit=1,tag=ItemSaveGA] run execute as @s[tag=ItemSaveEffects] run function zzzzdonotuse:structures/enchantedcavern/chest/specialeffects
execute at @s positioned ~ 255 ~ run execute as @e[type=item,distance=..5,tag=ItemSave,limit=1,tag=ItemSaveEgg] run execute as @s[tag=ItemSaveEffects] run function zzzzdonotuse:structures/enchantedcavern/chest/specialeffects
execute at @s positioned ~ 255 ~ run execute as @e[type=item,distance=..5,tag=ItemSave,limit=1,tag=ItemSaveEGA] run execute as @s[tag=ItemSaveEffects] run function zzzzdonotuse:structures/enchantedcavern/chest/specialeffects
execute at @s positioned ~ 255 ~ run execute as @e[type=item,distance=..5,tag=ItemSave,limit=1,tag=ItemSaveGT] run execute as @s[tag=ItemSaveEffects] run function zzzzdonotuse:structures/enchantedcavern/chest/specialeffects
execute at @s positioned ~ 255 ~ run execute as @e[type=item,distance=..5,tag=ItemSave,limit=1,tag=ItemSaveBP] run execute as @s[tag=ItemSaveEffects] run function zzzzdonotuse:structures/enchantedcavern/chest/specialeffects
execute at @s positioned ~ 255 ~ run execute as @e[type=item,distance=..5,tag=ItemSave,limit=1,tag=ItemSaveEmBlock] run execute as @s[tag=ItemSaveEffects] run function zzzzdonotuse:structures/enchantedcavern/chest/specialeffects
execute at @s positioned ~ 255 ~ run execute as @e[type=item,distance=..5,tag=ItemSave,limit=1,tag=ItemSaveDiaBlock] run execute as @s[tag=ItemSaveEffects] run function zzzzdonotuse:structures/enchantedcavern/chest/specialeffects
execute at @s positioned ~ 255 ~ run execute as @e[type=item,distance=..5,tag=ItemSave] run execute as @s[tag=ItemSaveEffects] run tag @s remove ItemSave

#put all the items spawned in the chest
execute at @s positioned ~ 255 ~ run function zzzzdonotuse:structures/enchantedcavern/chest/replacechest


#move the chest back down
execute at @s run clone ~ 255 ~ ~ 255 ~ ~ ~ ~ replace move



#Might need the below again later. Maybe not, who knows?

###diamond and emerald blocks are special exceptions due to having effects as well as being reagants - apply them differently
#execute positioned ~ 255 ~ run execute as @e[nbt={Item:{id:"minecraft:emerald_block"}},limit=1,sort=random,distance=..5] run scoreboard players set @s KyuDummy 450
#execute positioned ~ 255 ~ run execute as @e[nbt={Item:{id:"minecraft:diamond_block"}},limit=1,sort=random,distance=..5] run scoreboard players set @s KyuDummy 2250
#execute positioned ~ 255 ~ run execute as @e[nbt={Item:{id:"minecraft:emerald_block"}},limit=1,sort=random,distance=..5] run scoreboard players set @s KyuDummy2 450