
# -> If tool has already been complete then return
execute if entity @s[tag=OverlyRealistic.Block.ToolHandle_IsComplete] run \
    return fail

# -> Tag handle
tag @s add OverlyRealistic.Block.ToolHandleInteractedWith

# -> Get tool progress
scoreboard players set #or.tool_handle_progress OverlyRealistic.General.ArbitraryData -1
execute if score @s OverlyRealistic.General.ArbitraryData matches 0.. run \
    scoreboard players operation #or.tool_handle_progress OverlyRealistic.General.ArbitraryData = @s OverlyRealistic.General.ArbitraryData

# -> Reset interaction type
scoreboard players set #or.tool_handle_interaction_type OverlyRealistic.General.ArbitraryData 0

# -> Set item place success
scoreboard players set #or.tool_handle_item_place_success OverlyRealistic.General.ArbitraryData 0

# -> Interaction call
execute on target at @s run \
    function overly_realistic:general/custom_blocks/tool_handle/handle_interaction_player_interacted

# --> Interaction types

# -> Cordage
execute if score #or.tool_handle_interaction_type OverlyRealistic.General.ArbitraryData matches 1 run \
    function overly_realistic:general/custom_blocks/tool_handle/interaction_events/progress_1/interaction_place_cordage

# -> Adhesive
execute if score #or.tool_handle_interaction_type OverlyRealistic.General.ArbitraryData matches 2 run \
    function overly_realistic:general/custom_blocks/tool_handle/interaction_events/progress_2/interaction_place_adhesive

# -> Adhesive (skip)
execute if score #or.tool_handle_interaction_type OverlyRealistic.General.ArbitraryData matches 4 run \
    function overly_realistic:general/custom_blocks/tool_handle/interaction_events/progress_2/interaction_skip_adhesive

# -> Create item
execute if score #or.tool_handle_interaction_type OverlyRealistic.General.ArbitraryData matches 3 run \
    function overly_realistic:general/custom_blocks/tool_handle/interaction_events/progress_3/interaction_create_item


# -> Remove player's item if possible
execute if score #or.tool_handle_item_place_success OverlyRealistic.General.ArbitraryData matches 1 \
    on target run \
        item modify entity @s[gamemode=!creative] weapon.mainhand overly_realistic:multipurpose/remove_one_item

# -> Untag handle
tag @s remove OverlyRealistic.Block.ToolHandleInteractedWith
