# Summon a ray at the caster

scoreboard players set @s invulnerable 1

execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"scoutrifle"}}}}] run scoreboard players remove @s[scores={magScout=1..}] magScout 1

execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"scoutrifle"}}}}] run summon area_effect_cloud ~ ~ ~ {Duration: 1, Tags: ["ray","raysniper","basicgunray"]}

execute as @s[scores={carrotshoot=1..,sniperCD=..0},nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"scoutrifle"}}}}] at @s positioned ^ ^ ^ run scoreboard players set @s sniperCD 25

execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"scoutrifle"}}}}] at @s run playsound minecraft:weapon.sniperrifle player @a ~ ~ ~


# Position the ray at the caster's eyes and copy their facing
execute anchored eyes rotated as @s run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~ ~



execute store result score @s recoil run random value 1..4
tp @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"scoutrifle"}}}},scores={recoil=1}] ^ ^ ^ ~-2 ~-5
tp @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"scoutrifle"}}}},scores={recoil=2}] ^ ^ ^ ~3 ~-6
tp @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"scoutrifle"}}}},scores={recoil=3}] ^ ^ ^ ~-5 ~-7
tp @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"scoutrifle"}}}},scores={recoil=4}] ^ ^ ^ ~1 ~-8
scoreboard players reset @s recoil
# Process the ray
execute as @e[tag=ray,limit=1,sort=nearest] run function ray_demo:ray/process

