Minecraft – Would you be able to add an NBT tag to an already ‘spawned’ mob

minecraft-commandsminecraft-java-edition

After a mob is spawned, or summoned, would it be possible to give it more NBT tags? For example, could I have something such as /nbt @e NoAI:1 (I know that's not a real command) to freeze every mob in the game? Or /nbt @e[type=villager] Invisible:1 to give all villagers the 'Invisible' NBT tag?

Best Answer

Yes, this would be done with the /data command. The format is

"/data get/merge/modify/remove block/entity (selector) {nbt}".

What you'd want is

"/data merge entity (selector) {nbt}".

Here's an example for you:

"/data merge entity @e[type=villager,limit=1] {Invulnerable:1b}"

The data command only works on one entity at a time, so all selectors must have a "limit=1" in it- but no fear, there's a way around it. We just make the entities execute the command to themselves.

"/execute as (selector) run data merge entity @s {nbt}".

Example:

"/execute as @e[type=villager] run data merge entity @s {Invulnerable:1b}"

This would make ALL villagers invulnerable.