Download the ready and complete datapack to use in this tutorial by clicking here
If you don't know how a function works, click here and check out this tutorial
In this tutorial, we will use right-click mouse detection. If you want to learn more about how this detection works, check out this tutorial by clicking here
Before we start, get the wand that will summon fireballs using the command below
give @p minecraft:stick[food={nutrition:0,saturation:0.0,can_always_eat:true},minecraft:consumable={consume_seconds:1000000},minecraft:custom_data={Fireball_Wand:1b},minecraft:custom_name="Fireball Wand",minecraft:enchantment_glint_override=true]
Create the following scoreboard to be used as the wand cooldown
scoreboard objectives add fireball_cooldown dummy
Run this command to set a zero score in the scoreboard to avoid problems
scoreboard players set @a fireball_cooldown 0
Now it's time to set up the cooldown and run the function commands
Place two separate Repeat command blocks set to Always Active and use the following commands
execute as @a if score @s fireball_cooldown matches 1.. run scoreboard players add @s fireball_cooldown 1
execute as @a if score @s fireball_cooldown matches 40.. run scoreboard players set @s fireball_cooldown 0
The first command will run on all players (adding +1 score individually) every Minecraft tick: 20 ticks = 1 second
In the second command, we detect if the score reached 40, which in this case means a cooldown of 2 seconds
After that, the score will be reset, and therefore the cooldown to use the wand will also reset
When the click happens, the function is called and checks if the cooldown is zero to charge the attack
Here are the commands for the attack charging function
advancement revoke @s from comandos:click_fireball_attack
execute if score @s fireball_cooldown matches 0 run execute anchored eyes positioned ^ ^ ^ summon fireball run function comandos:fireball_attack_position
execute if score @s fireball_cooldown matches 0 run scoreboard players set @s fireball_cooldown 1
Here are the commands for the position detection function to summon the fireball
execute positioned 0.0 0.0 0.0 run tp @s ^ ^ ^0.1
data modify storage test Motion set from entity @s Pos
tp @s ~ ~ ~
data modify entity @s Motion set from storage test Motion