
# -> Debug
#tellraw @a {score:{name:"@s",objective:"OverlyRealistic.Player.UsingHuiTime_New"}}

# -> If player hasn't been holding down the HUI for long enough then return
execute unless score @s OverlyRealistic.Player.UsingHuiTime_New matches 11.. run \
    return run \
        scoreboard players set @s OverlyRealistic.Player.UsingHuiTime_New 0

# -> Decrease stamina
scoreboard players remove @s OverlyRealistic.Player.Stats.Stamina 5

# -> Store spear item
execute if score #or.player_use_hui_hand OverlyRealistic.General.ArbitraryData matches 1 run \
    item replace entity 00000001-0000-002b-0000-00010000002b contents from entity @s weapon.mainhand
execute if score #or.player_use_hui_hand OverlyRealistic.General.ArbitraryData matches 2 run \
    item replace entity 00000001-0000-002b-0000-00010000002b contents from entity @s weapon.offhand

# -> Remove spear from player
execute if score #or.player_use_hui_hand OverlyRealistic.General.ArbitraryData matches 1 run \
    item replace entity @s[gamemode=!creative] weapon.mainhand with air
execute if score #or.player_use_hui_hand OverlyRealistic.General.ArbitraryData matches 2 run \
    item replace entity @s[gamemode=!creative] weapon.offhand with air

# -> Sound
playsound entity.arrow.shoot player @a ~ ~ ~ 0.6 1.3
playsound minecraft:entity.breeze.jump player @a ~ ~ ~ 0.15 1.8

# -> Remove HUI time new
scoreboard players set @s OverlyRealistic.Player.UsingHuiTime_New 0

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

# -> If theres an entity right in front of the player then return and damage it directly
execute anchored eyes \
    positioned ^ ^ ^ \
    anchored eyes \
    positioned ^ ^ ^2 \
    positioned ~-1 ~-1 ~-1 \
    if entity @n[type=!#overly_realistic:spear_cant_damage,tag=!OverlyRealistic.Player.TempPlayerThrewSpear,dx=1,dy=1,dz=1] \
    positioned ~1 ~1 ~1 run \
        return run \
            function overly_realistic:players/misc/spear/spear_damage_instant/spear_deal_insta_damage

# -> Create ID if player does not have one already
execute unless score @s OverlyRealistic.Player.UsingSpearID matches -2147483648..2147483647 \
    store result score @s OverlyRealistic.Player.UsingSpearID run \
        scoreboard players add #or.global OverlyRealistic.Player.UsingSpearID 1

# -> Create spear entity ( cant use exec summon here because it would be visible for a couple of ms :pensive: )
execute anchored eyes \
    positioned ^ ^ ^ \
    anchored feet run \
        summon snowball ^ ^ ^ {Tags:["OverlyRealistic.Item.TempSpearEnt"],Item:{id:"minecraft:diamond",count:1,components:{"minecraft:item_model":"overlyrealistic:misc/empty"}},Passengers:[{id:"item_display",Tags:["OverlyRealistic.TempEntSpear","OverlyRealistic.Item.SpearDisplayEnt","OverlyRealistic.Entity.IsProjectileDisplayEntity","OverlyRealistic.Entity.IsProjectile"],teleport_duration:1,item: {count: 1, id: "minecraft:stick"}, transformation: {left_rotation: [0.6424319f, 0.27467933f, 0.66374505f, 0.26697394f], right_rotation: [0.0f, 0.0f, 0.0f, 1.0f], scale: [1.0000002f, 1.0000002f, 1.0000001f], translation: [0.0f, 0.0f, 0.0f]}}]}
execute as @n[type=snowball,tag=OverlyRealistic.Item.TempSpearEnt,distance=..10] at @s rotated as @p[tag=OverlyRealistic.Player.TempPlayerThrewSpear,distance=..10] run \
    function overly_realistic:players/misc/spear/spear_create_entity

# -> Activate spear loop
schedule function overly_realistic:general/spears/continuous/spear_thrown_loop 1t append

# -> Create spear entity ( cant use exec summon here because it would be visible for a couple of ms :pensive: )
#execute anchored eyes \
    positioned ^ ^ ^ \
    anchored feet run \
        summon armor_stand ^ ^ ^ {DisabledSlots:4144959,Silent:1b,Small:1b,Invisible:1b,PersistenceRequired:1b,Tags:["OverlyRealistic.Item.SpearBaseEnt","OverlyRealistic.Item.TempSpearEnt"],attributes:[{id:"minecraft:gravity",base:0.06},{id:"minecraft:scale",base:0.0625}]}
#execute as @n[type=armor_stand,tag=OverlyRealistic.Item.TempSpearEnt,distance=..10] at @s run \
    function overly_realistic:players/misc/spear/spear_create_entity

#summon minecraft:item_display ~ ~ ~ {item: {components: {"minecraft:consumable": {animation: "trident", consume_seconds: 99999.0f, has_consume_particles: 0b, sound: "minecraft:intentionally_empty"}, "minecraft:custom_data": {IsSpear: 1b, SpearType: "wooden_spear", or_edited_item: 1b, or_is_tool: 1b, or_no_edit: 1b, or_tool_type: "spear", or_tool_type_specific: "wooden_spear"}, "minecraft:food": {can_always_eat: 1b, nutrition: 0, saturation: 0.0f}, "minecraft:item_model": "overlyrealistic:items/spears/wooden_spear", "minecraft:item_name": {color: "white", fallback: "Wooden Spear", translate: "item.minecraft.wooden_spear"}, "minecraft:use_effects": {can_sprint: 1b}}, count: 1, id: "minecraft:wooden_spear"}, teleport_duration: 1, transformation: {left_rotation: [0.6424319f, 0.27467933f, 0.66374505f, 0.26697394f], right_rotation: [0.0f, 0.0f, 0.0f, 1.0f], scale: [1.0000002f, 1.0000002f, 1.0000001f], translation: [0.0f, 0.0f, 0.0f]}}


# -> Untag player
tag @s remove OverlyRealistic.Player.TempPlayerThrewSpear