I want to execute a command to an armor stand if the nearest pig has a specific tag.

The commands that I have tried so far...

execute as @e[type=armor_stand] at @s if entity @e[type=pig,limit=1,sort=nearest,tag=carrot] run <command>

does not work because it checks if "carrot"-pig exists.

Having a pig with no tag "carrot" between the armor stand and the "carrot"-pig will just result to test passed.

execute as @e[type=armor_stand] at @s as @e[type=pig,limit=1,sort=nearest] if entity @s[tag=carrot] run <command>

might have worked but it changed the selector.

  • What's wrong with the last command exactly?
    – pppery
    Commented Jul 23, 2020 at 15:23
  • I need to execute it as the armor stand, not the pig.
    – Robomike
    Commented Jul 23, 2020 at 15:52

1 Answer 1


Use the as parameter to switch executor to the armour stand at the end.

  at @e[type=armor_stand,limit=1,sort=nearest]  # Move the location to the armour stand
  as @e[type=pig,limit=1,sort=nearest]          # NEAREST PIG NOW HAS CONTROL
  if entity @s[tag=carrot]                      # If I have the tag carrot, continue to the next step, otherwise stop.
  as @e[type=armor_stand,limit=1,sort=nearest]  # ARMOUR STAND NOW HAS CONTROL
say The nearest pig to me has the carrot tag!

The execution position before the last as is still at the armour stand, so the selector always finds the armour stand that started, except if there are multiple at the exact same coordinates.

  • Using the as parameter at the end worked perfectly but there are some flaws in your command. I already edited the command and your answer to perfect the solution to my problem. All I have to do was remove limit=1 at the first as and add sort=nearest at the end. Thanks.
    – Robomike
    Commented Jul 24, 2020 at 4:09
  • @Robomike Why does limit=1 not work for you? I thought there was only one armour stand you were dealing with. But if there are more, removing limit=1 is certainly needed.
    – One 2 Many
    Commented Jul 24, 2020 at 4:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .