
# -> If player sharpened a flint flake within the last tick then return
execute if entity @s[tag=OverlyRealistic.Player.HasSharpenedFlintFlake] run \
    return fail

# -> Particle
execute at @s \
    anchored eyes run \
        particle item{item:"flint"} ^ ^-0.25 ^0.75 0.1 0.1 0.1 0.001 1 normal @a

# -> Advancement
advancement grant @s only overly_realistic:advancements/stone_age/quick_knap

# -> If player has unset power then set power to 0
execute unless score @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapBreakPower matches 0.. run \
    scoreboard players set @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapBreakPower 0

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

# -> Sound
playsound minecraft:block.stone.break block @a ~ ~ ~ 0.7 2

# -> Altitude checks
scoreboard players remove @s[y=150,dy=9999] OverlyRealistic.Player.Stats.Stamina 1
scoreboard players remove @s[y=250,dy=9999] OverlyRealistic.Player.Stats.Stamina 1

# -> Reset time
scoreboard players set @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapTime 1

# -> Get old flint's usage value
execute unless score @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapUsage matches 0.. run \
    scoreboard players set @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapUsage 0
scoreboard players operation @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapUsageOld = @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapUsage

# -> Get flint's usage
scoreboard players set @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapUsage 0
execute if items entity @s weapon.mainhand *[custom_data~{sharpened_flint_uses:1b}] run \
    scoreboard players set @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapUsage 1
execute if items entity @s weapon.mainhand *[custom_data~{sharpened_flint_uses:2b}] run \
    scoreboard players set @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapUsage 2
execute if items entity @s weapon.mainhand *[custom_data~{sharpened_flint_uses:3b}] run \
    scoreboard players set @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapUsage 3

# -> If old flint usage != new flint usage then reset knap percentage
execute unless score @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapUsage = @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapUsageOld run \
    scoreboard players set @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapBreakPercent 0

# --> Power usages

# -> power 0
execute if score @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapBreakPower matches ..1 run \
    return run \
        function overly_realistic:players/items/sharpened_flint/knapping/power_usages/power_0

# -> power 1
execute if score @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapBreakPower matches 2 run \
    return run \
        function overly_realistic:players/items/sharpened_flint/knapping/power_usages/power_1

# -> power 2
execute if score @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapBreakPower matches 3 run \
    return run \
        function overly_realistic:players/items/sharpened_flint/knapping/power_usages/power_2

# -> power 3
execute if score @s OverlyRealistic.Player.UseItem_SharpenedFlint_KnapBreakPower matches 4.. run \
    return run \
        function overly_realistic:players/items/sharpened_flint/knapping/power_usages/power_3
