So I am making a map and I need to be able to test for the CLOSEST entity to the player, is this possible with /testfor yet? /execute @e[type=entity name here] ~ ~ ~ /testfor @p will test for the player closest to an entity, instead of the closest entity to the player. So how do I achieve this? Is it even possible? It's kinda like how if you have Steve and Alex next to a command block with the command /testfor @p will find Steve if Steve is closest and same with Alex, just entities instead of people and relative to me, not a command bock.
Minecraft – How to test for the entity closest to the player
minecraft-commandsminecraft-java-edition
Related Topic
- Minecraft – How to test for another player in a radius
- Minecraft – My command for minecraft executing a command at an entity won’t work
- Minecraft – /tp Relative Coordinates
- Minecraft – How to execute a command relative to an entity in 1.15
- Minecraft – Teleporting an entity relative to itself when the entity type is unknown
- Minecraft – How to test for a nonexistant NBT tag
Best Answer
In the future, I recommend checking the wiki before posting a question here.
This is entirely possible using the
execute
command and target selector variables and arguments.Executing commands at the player
The
execute
command executes commands on an entity. The@p
selector selects a single player.The
name=
argument only selects entities with a specific name.So
execute @p[name=<your username here>] ~ ~ ~ <command>
will run<command>
at your location.testfor
the nearest entity.The
@e
selector finds entities of any type.The
c=
argument selects a certain count. If this number is positive, it will select these in order from closest to furthest.c=1
will select the nearest entity.The
rm=
argument selects only entities outside of a radius. If this is set to 0, it will select any entity that is not the executing entity.So
testfor @e[c=1,rm=0]
would find the nearest entity to the executor of the command.Putting it together
So, the first command will execute the second command at the player, meaning your finished command is
Closing comments/notes
As far as using
testfor
goes, I'd recommend against it. The scoreboard is almost always more useful, since you can use things liketag
to mark the entity you found.