
# -> Reset adv trigger
advancement revoke @s only overly_realistic:items/use_shears

# -> Add to shear time
scoreboard players add @s OverlyRealistic.Player.UseItem_Shears 1

# -> If shear time < max then return
execute unless score @s OverlyRealistic.Player.UseItem_Shears matches 20.. run \
    return fail

# -> If player is not looking at sheep then return and do non_ent operations
execute unless predicate overly_realistic:player/misc/looking_at_sheep run \
    return run \
        function overly_realistic:players/items/shears/non_entity_interactions/player_use_shears_non_ent

# -> If player is using iron shears then set value
scoreboard players set #or.player_using_iron_shears OverlyRealistic.General.ArbitraryData 0
execute unless items entity @s weapon.mainhand *[custom_data~{IsShears:1b}] \
    if items entity @s weapon.offhand *[custom_data~{IsShears:1b,or_shears_type:"iron"}] run \
        scoreboard players set #or.player_using_iron_shears OverlyRealistic.General.ArbitraryData 1
execute if items entity @s weapon.mainhand *[custom_data~{IsShears:1b,or_shears_type:"iron"}] run \
    scoreboard players set #or.player_using_iron_shears OverlyRealistic.General.ArbitraryData 1

# -> Reset shear time
scoreboard players reset @s OverlyRealistic.Player.UseItem_Shears

# -> Reset vars
scoreboard players set #or.Raycast.RayRange OverlyRealistic.General.ArbitraryData 0

# -> Reset sheep exist success
scoreboard players set #or.sheep_exist_success OverlyRealistic.General.ArbitraryData 0

# -> Get player ent interaction range
execute store result score #or.Raycast.RayRangeMax OverlyRealistic.General.ArbitraryData run \
    attribute @s entity_interaction_range get 10

# -> Raycast
execute anchored eyes positioned ^ ^ ^ anchored feet run \
    function overly_realistic:players/items/shears/sheep_shearing/shears_raycast_travel

# -> If sheep exist succes is false then return
execute unless score #or.sheep_exist_success OverlyRealistic.General.ArbitraryData matches 1 run \
    return fail

# -> Sound
playsound item.shears.snip player @a ~ ~ ~ 0.5 1

# -> Get shearing percentage
scoreboard players operation #or.shearing_percentage OverlyRealistic.General.ArbitraryData = #or.sheep_shear_time_global OverlyRealistic.General.ArbitraryData
scoreboard players operation #or.shearing_percentage OverlyRealistic.General.ArbitraryData *= #or.static_num_100 OverlyRealistic.General.ArbitraryData
scoreboard players operation #or.shearing_percentage OverlyRealistic.General.ArbitraryData /= #or.sheep_shear_time_max_global OverlyRealistic.General.ArbitraryData

# -> Display shearing percentage
title @s title ""
title @s times 0 40 40
title @s subtitle [{text:"✂",color:"#949494",shadow_color:-12303292},{text:" "},{score:{name:"#or.shearing_percentage",objective:"OverlyRealistic.General.ArbitraryData"},color:"#ffffff",shadow_color:-10395295},{text:"%",color:"#ffffff",shadow_color:-10395295}]

execute if score #or.sheep_shear_success OverlyRealistic.General.ArbitraryData matches 1 run \
    title @s subtitle [{text:"✂",color:"#949494",shadow_color:-12303292},{text:" "},{text:"100",color:"#ffffff",shadow_color:-10395295},{text:"%",color:"#ffffff",shadow_color:-10395295}]

# -> Add wisdom experience
function overly_realistic:players/wisdom/husbandry/add_to_skill_xp_c_shear_sheep

# -> Damage shears (offhand)
execute unless items entity @s weapon.mainhand *[custom_data~{IsShears:1b}] \
    if items entity @s weapon.offhand *[custom_data~{IsShears:1b}] run \
        return run \
            function overly_realistic:players/items/shears/damage_shears_from_offhand

# -> Damage shears (mainhand)
item replace entity 00000001-0000-002b-0000-00010000002b contents from entity @s weapon.mainhand
scoreboard players set #or.shears_damage_from_shearing OverlyRealistic.General.ArbitraryData 0
execute store result score #or.shears_damage_from_shearing OverlyRealistic.General.ArbitraryData run \
    data get entity 00000001-0000-002b-0000-00010000002b item.components."minecraft:damage"
execute if score #or.shears_damage_from_shearing OverlyRealistic.General.ArbitraryData matches 238.. run \
    return run \
        item replace entity @s weapon.mainhand with air
execute if score #or.player_using_iron_shears OverlyRealistic.General.ArbitraryData matches 0 \
    if score #or.shears_damage_from_shearing OverlyRealistic.General.ArbitraryData matches 138.. run \
        return run \
            item replace entity @s weapon.mainhand with air
scoreboard players add #or.shears_damage_from_shearing OverlyRealistic.General.ArbitraryData 1
execute store result entity 00000001-0000-002b-0000-00010000002b item.components."minecraft:damage" int 1 run \
    scoreboard players get #or.shears_damage_from_shearing OverlyRealistic.General.ArbitraryData
item replace entity @s weapon.mainhand from entity 00000001-0000-002b-0000-00010000002b contents
