## Remove one item used in the crafting
$item modify block ~ ~ ~ container.$(slot) {function:"minecraft:set_count",count:-1,add:1b}

## Check which slot the item can go in
# 99999 will drop the item
scoreboard players set @s vb.dummy1 99999
# Check all 9 slots
execute unless items block ~ ~ ~ container.0 * run scoreboard players set @s vb.dummy1 0
execute unless items block ~ ~ ~ container.1 * run scoreboard players set @s vb.dummy1 1
execute unless items block ~ ~ ~ container.2 * run scoreboard players set @s vb.dummy1 2
execute unless items block ~ ~ ~ container.3 * run scoreboard players set @s vb.dummy1 3
execute unless items block ~ ~ ~ container.4 * run scoreboard players set @s vb.dummy1 4
execute unless items block ~ ~ ~ container.5 * run scoreboard players set @s vb.dummy1 5
execute unless items block ~ ~ ~ container.6 * run scoreboard players set @s vb.dummy1 6
execute unless items block ~ ~ ~ container.7 * run scoreboard players set @s vb.dummy1 7
execute unless items block ~ ~ ~ container.8 * run scoreboard players set @s vb.dummy1 8
# Check the slot we're crafting in
$execute unless items block ~ ~ ~ container.$(slot) * run scoreboard players set @s vb.dummy1 $(slot)

## Insert the item
$execute if score @s vb.dummy1 matches 99999 run loot spawn ~ ~0.8 ~ loot vb:gameplay/esky/$(item)
$execute if score @s vb.dummy1 matches 0 run loot replace block ~ ~ ~ container.0 loot vb:gameplay/esky/$(item)
$execute if score @s vb.dummy1 matches 1 run loot replace block ~ ~ ~ container.1 loot vb:gameplay/esky/$(item)
$execute if score @s vb.dummy1 matches 2 run loot replace block ~ ~ ~ container.2 loot vb:gameplay/esky/$(item)
$execute if score @s vb.dummy1 matches 3 run loot replace block ~ ~ ~ container.3 loot vb:gameplay/esky/$(item)
$execute if score @s vb.dummy1 matches 4 run loot replace block ~ ~ ~ container.4 loot vb:gameplay/esky/$(item)
$execute if score @s vb.dummy1 matches 5 run loot replace block ~ ~ ~ container.5 loot vb:gameplay/esky/$(item)
$execute if score @s vb.dummy1 matches 6 run loot replace block ~ ~ ~ container.6 loot vb:gameplay/esky/$(item)
$execute if score @s vb.dummy1 matches 7 run loot replace block ~ ~ ~ container.7 loot vb:gameplay/esky/$(item)
$execute if score @s vb.dummy1 matches 8 run loot replace block ~ ~ ~ container.8 loot vb:gameplay/esky/$(item)
