※Before copying and pasting the commands below※
Copying and pasting the commands as-is will not work!!!
If you look closely at the commands, you'll see sections labeled "Item," "Number," and "(Ability Command).
You must manually enter the desired item name, cooldown time, and ability command (effect, summon, etc.) in these sections.
The command to create a scoreboard only needs to be entered once.
The command to set the score to 0 must be entered once for each player.
scoreboard objectives add cool dummy
scoreboard objectives add time dummy
execute as @a[nbt={Inventory:[{Slot:-106b,id:"minecraft:item"}]}] run tag @s add use
execute as @a[nbt={Inventory:[{Slot:-106b,id:"minecraft:item"}]}] run item replace entity @s weapon.offhand with air
execute as @a[tag=use] run item replace entity @s weapon.mainhand with item
execute as @a[tag=use] if score @s cool matches 0 run (ability command)
execute as @a[tag=use] run scoreboard players set @s cool number
execute as @a[tag=use] run tag @s remove use
execute as @a[scores={cool=1..}] run scoreboard players add @s time 1
execute as @a[scores={time=20}] run scoreboard players remove @s cool 1
execute as @a[scores={time=20}] run scoreboard players reset @s time
execute as @a[scores={cool=0}] run scoreboard players reset @s time
scoreboard players set @a cool 0
Below is a bonus command that displays the cooldown in the action bar.
You can use it by inserting it into a repeat command block.
(When 1 ability)
execute as @a run title @s actionbar [{"text":"Cooldown:","color":"black"},{"score":{"name":"@s","objective":"cool"},"color":"black"}]
(When 2 abilities)
execute as @a run title @s actionbar [{"text":"Cooldown 1:","color":"black"},{"score":{"name":"@s","objective":"cool"},"color":"black"},{"text":" "},{"text":"Cooldown 2:","color":"black"},{"score":{"name":"@s","objective":"cool2"},"color":"black"}]