how do i execute a command at an entity with a certain nbt data

I am trying to execute a test command where every single creeper with a walkspeed of 0 spawned in runs a command to summon a zombie directly at the creeper with the NBT data I specified.

Commands I tried:

  • /execute as @e[type=creeper] at @s[nbt={Attributes:generic.movementSpeed,Base:0f}] run summon zombie

  • /execute at @e[type=creeper,nbt={Attributes:generic.movementSpeed,Base:0f}] run summon zombie

  • /execute at @e[type=creeper] if entity @s[nbt={Attributes:generic.movementSpeed,Base:0f}] run summon zombie

Those commands did not work, how do I do this?


Solution 1:

Your NBT data is malformed. You have to provide a list of attributes, even if you only want to specify one, and for each attribute you want to specify you have to give its name and base value.

  • You start constructing your NBT data with your attribute, which is a compound. Compounds are marked as {...}.
  • An attribute needs two elements, its Name and Base.
  • Each element has a value and you construct an element with a value with element:value. So in this case you do Name:"generic.movementSpeed" and Base:0d. You put the generic.movementSpeed in quotation marks, because it is of type String (computer talk for text) and you put the d behind the number because it is of type Double (computer talk for a decimal number).
  • To put several elements (with values) inside a compound you separate them with a comma.

Overall you get:

{Name:"generic.movementSpeed",Base:0d}

Now that you have your attribute you have to consider that Minecraft expects an element called Attributes with a value that is a list of all attributes. You create a list with [...]. Put your attribute compound into it to form a list with one element (which is your attribute compound, which itself has two elements), so you just surround your attribute with [...]. Now put this as the value of the element Attributes:

Attributes:[{Name:"generic.movementSpeed",Base:0d}]

When checking with nbt=... Minecraft always expects a single NBT compound, so add another {...} around this. If you want to check any creeper for this you can do:

\execute as @e[type=creeper,nbt={Attributes:[{Name:"generic.movementSpeed",Base:0d}]}] run ...

Or you do it split up into an as and an at like you did in your first command. The second and third commands you specified would not work as expected, because you never specify an as, thus the command will check the currently executing entity whether it is a creeper with the NBT attribute. And this is normally the player who runs the command.

For doing something (like summoning a zombie) at the position of those creepers replace the run ... with at @s run ....

HOWEVER, note that this will check for creepers that have this movement speed attribute, NOT for creepers that are currently moving with this speed. So to check for any still standing creepers this won't work.