# Summon a ray at the caster


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

execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"spas12"}}}}] run summon area_effect_cloud ~ ~ ~ {Duration: 1, Tags: ["Aray","ray1","rayspas12","basicgunray"]}
execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"spas12"}}}}] run summon area_effect_cloud ~ ~ ~ {Duration: 1, Tags: ["Aray","ray2","rayspas12","basicgunray"]}
execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"spas12"}}}}] run summon area_effect_cloud ~ ~ ~ {Duration: 1, Tags: ["Aray","ray3","rayspas12","basicgunray"]}
execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"spas12"}}}}] run summon area_effect_cloud ~ ~ ~ {Duration: 1, Tags: ["Aray","ray4","rayspas12","basicgunray"]}
execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"spas12"}}}}] at @s run playsound minecraft:weapon_spas12 player @a ~ ~ ~

execute as @s[scores={carrotshoot=1..,spas12CD=..0},nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"spas12"}}}}] run scoreboard players set @s spas12CD 15

# 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 ~3.5 ~
execute anchored eyes rotated as @s run tp @e[tag=ray2,limit=1,sort=nearest] ^ ^ ^1 ~-3.5 ~
execute anchored eyes rotated as @s run tp @e[tag=ray3,limit=1,sort=nearest] ^ ^ ^1 ~11.25 ~
execute anchored eyes rotated as @s run tp @e[tag=ray4,limit=1,sort=nearest] ^ ^ ^1 ~-11.25 ~

execute store result score @s recoil run random value 1..4
tp @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"spas12"}}}},scores={recoil=1}] ^ ^ ^ ~0.35 ~-5
tp @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"spas12"}}}},scores={recoil=2}] ^ ^ ^ ~-0.35 ~-6
tp @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"spas12"}}}},scores={recoil=3}] ^ ^ ^ ~0.25 ~-5
tp @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"spas12"}}}},scores={recoil=4}] ^ ^ ^ ~-0.25 ~-6

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