# Summon a ray at the caster


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

execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"glock"}}}}] run summon area_effect_cloud ~ ~ ~ {Duration: 1, Tags: ["ray","rayglock","basicgunray"]}
execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"mp5"}}}}] run summon area_effect_cloud ~ ~ ~ {Duration: 1, Tags: ["ray","raymp5","basicgunray"]}
execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"bison"}}}}] run summon area_effect_cloud ~ ~ ~ {Duration: 1, Tags: ["ray","raybison","energyray"]}

execute as @s[scores={carrotshoot=1..,glockCD=..0},nbt={SelectedItem:{count:1,components:{"minecraft:custom_data":{weapon:"glock"}}}}] at @s positioned ^ ^ ^ run scoreboard players set @s glockCD 7
execute as @s[scores={carrotshoot=1..,mp5CD=..0},nbt={SelectedItem:{count:1,components:{"minecraft:custom_data":{weapon:"mp5"}}}}] at @s positioned ^ ^ ^ run scoreboard players set @s mp5CD 3


execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"glock"}}}}] at @s run playsound minecraft:weapon.9mm player @a ~ ~ ~
execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"mp5"}}}}] at @s run playsound minecraft:weapon.mp5 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:"mp5"}}}},scores={recoil=1}] ^ ^ ^ ~1 ~-3
tp @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"mp5"}}}},scores={recoil=2}] ^ ^ ^ ~-1 ~-2
tp @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"mp5"}}}},scores={recoil=3}] ^ ^ ^ ~1 ~-3
tp @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{weapon:"mp5"}}}},scores={recoil=4}] ^ ^ ^ ~-1 ~-2
scoreboard players reset @s recoil
# Process the ray
execute as @e[tag=ray,limit=1,sort=nearest] run function ray_demo:ray/process

