# Summon a ray at the caster


scoreboard players set @s invulnerable 1

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

execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"duskshotgun"}}}}] run summon area_effect_cloud ~ ~ ~ {Duration: 1, Tags: ["Aray","ray1","raydusk","basicgunray"]}
execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"duskshotgun"}}}}] run summon area_effect_cloud ~ ~ ~ {Duration: 1, Tags: ["Aray","ray2","raydusk","basicgunray"]}

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

execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"duskshotgun"}}}}] at @s run playsound minecraft:weapon.duskshotgun player @a ~ ~ ~
# Position the ray at the caster's eyes and copy their facing

execute anchored eyes rotated as @s run tp @e[tag=ray1,limit=1,sort=nearest] ^ ^ ^1 ~2.25 ~
execute anchored eyes rotated as @s run tp @e[tag=ray2,limit=1,sort=nearest] ^ ^ ^1 ~-2.25 ~

execute store result score @s recoil run random value 1..4
tp @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"duskshotgun"}}}},scores={recoil=1}] ^ ^ ^ ~0.6 ~-8
tp @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"duskshotgun"}}}},scores={recoil=2}] ^ ^ ^ ~-0.7 ~-9
tp @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"duskshotgun"}}}},scores={recoil=3}] ^ ^ ^ ~0.7 ~-8
tp @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"duskshotgun"}}}},scores={recoil=4}] ^ ^ ^ ~-0.6 ~-9

scoreboard players reset @s recoil
# Process the ray
execute as @e[tag=Aray,limit=2,sort=nearest] run function ray_demo:ray/process
