Minecraft Commands – How to Testfor a Player Holding an Item and Give Them an Effect in Minecraft

minecraft-commandsminecraft-java-edition

So i'm making a map that gives you a slime ball says REGEN and a sword. I would like to give the player holding the slime regen II for 1 second to heal if he's not fighting using /testfor. But how do I testfor and give to the exact player that holds the slime, as the regen may be given to another player who's not holding the slime.

Best Answer

Use a scoreboard tag

Set up a repeat/chain line of command blocks and run:

/scoreboard players tag @a[tag=holdSlime] remove holdSlime
/scoreboard players tag @a[tag=!holdSlime] add holdSlime {SelectedItem:{id:minecraft:slime,tag:{Display:{Name:"REGEN"}}}}
/effect @a[tag=holdSlime] minecraft:regeneration 1 1