Minecraft – How to apply an effect on a villager of a specific profession

minecraft-commandsminecraft-java-edition

How do I get a command block to affect a specific type of villager (also known as profession)?

So far I know:

/effect @e[type=villager] minecraft:{effect here}

To affect all villagers, but I don't know how to change the command to only affect a specific profession. I've tried:

/effect @e[type=villager, Profession:5] minecraft:{effect here}
/effect @e[type=villager; Profession:5] minecraft:{effect here}

And also with profession in brackets and with no separator.

Best Answer

You cannot insert NBT data directly into a selector. You must use a valid selector argument, of which there is none for a villager profession (you can find a list of valid arguments here). Selectors must also not contain spaces.

In order to target based on NBT data, you must use the /scoreboard command to assign a label to the target first:

/scoreboard players tag @e[type=villager,tag=!profession5] add profession5 {Profession:5}

And then you can target the villager with the particular label:

/effect @e[type=villager,tag=profession5] minecraft:speed

Alternatively, you can use a score instead of a tag label:

/scoreboard objectives add Profession dummy

And the value of the score would represent the profession:

/scoreboard players set @e[type=villager] Profession 5 {Profession:5}

This allows you to easily target a range of professions at the same time, unlike tag labels:

/effect @e[type=villager,score_Profession_min=5,score_Profession=5] minecraft:speed
Related Topic