
# -> Untag ent
tag @s remove OverlyRealistic.Item.TempSpearEnt

# -> Set rotation data based on origin
rotate @s ~ ~

# -> Operate on display
execute on passengers run \
    tag @s remove OverlyRealistic.TempEntSpear
execute rotated as @s \
    on passengers run \
        rotate @s ~ ~
execute on passengers run \
    scoreboard players operation @s OverlyRealistic.Player.UsingSpearID = @p[tag=OverlyRealistic.Player.TempPlayerThrewSpear,distance=..10] OverlyRealistic.Player.UsingSpearID
execute on passengers run \
    item replace entity @s contents from entity 00000001-0000-002b-0000-00010000002b contents


# -> Add motion
execute store result score #or.pos_x_0 OverlyRealistic.General.ArbitraryData run \
    data get entity @s Pos[0] 1000
execute store result score #or.pos_y_0 OverlyRealistic.General.ArbitraryData run \
    data get entity @s Pos[1] 1000
execute store result score #or.pos_z_0 OverlyRealistic.General.ArbitraryData run \
    data get entity @s Pos[2] 1000

execute at @s run \
    tp @s ^ ^ ^1

execute store result score #or.pos_x_1 OverlyRealistic.General.ArbitraryData run \
    data get entity @s Pos[0] 1000
execute store result score #or.pos_y_1 OverlyRealistic.General.ArbitraryData run \
    data get entity @s Pos[1] 1000
execute store result score #or.pos_z_1 OverlyRealistic.General.ArbitraryData run \
    data get entity @s Pos[2] 1000

scoreboard players operation #or.pos_x_1 OverlyRealistic.General.ArbitraryData -= #or.pos_x_0 OverlyRealistic.General.ArbitraryData
scoreboard players operation #or.pos_y_1 OverlyRealistic.General.ArbitraryData -= #or.pos_y_0 OverlyRealistic.General.ArbitraryData
scoreboard players operation #or.pos_z_1 OverlyRealistic.General.ArbitraryData -= #or.pos_z_0 OverlyRealistic.General.ArbitraryData

execute store result entity @s Motion[0] double 0.002 run \
    scoreboard players get #or.pos_x_1 OverlyRealistic.General.ArbitraryData

execute store result entity @s Motion[2] double 0.002 run \
    scoreboard players get #or.pos_z_1 OverlyRealistic.General.ArbitraryData

# -> Debug rotation message
#tellraw @a {score:{name:"#or.pos_y_1",objective:"OverlyRealistic.General.ArbitraryData"}}

# --> Y pos binary tree

# -> Pos 0
execute if score #or.pos_y_1 OverlyRealistic.General.ArbitraryData matches ..-500 run \
    return run \
        function overly_realistic:players/misc/spear/y_pos_binary_tree/pos_type_0

# -> Pos 1
execute if score #or.pos_y_1 OverlyRealistic.General.ArbitraryData matches -499..-330 run \
    return run \
        function overly_realistic:players/misc/spear/y_pos_binary_tree/pos_type_1

# -> Pos 2
execute if score #or.pos_y_1 OverlyRealistic.General.ArbitraryData matches -329..-200 run \
    return run \
        function overly_realistic:players/misc/spear/y_pos_binary_tree/pos_type_2

# -> Pos 3

execute if score #or.pos_y_1 OverlyRealistic.General.ArbitraryData matches -199..399 \
    store result entity @s Motion[1] double 0.0025 run \
        return run \
            scoreboard players get #or.pos_y_1 OverlyRealistic.General.ArbitraryData

execute if score #or.pos_y_1 OverlyRealistic.General.ArbitraryData matches 400.. \
    store result entity @s Motion[1] double 0.002 run \
        return run \
            scoreboard players get #or.pos_y_1 OverlyRealistic.General.ArbitraryData
