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 selectors work on some very strange principles.
@r used to be the only way to select players, while now they are just a shorthand.
Using the count selector, we can select up to
n entities in order of range. Because of this,
@p is equivalent to
@a[c=1]. We can use the same principle to target the nearest entity using
In the future, I recommend checking the wiki before posting a question here.
Executing commands at the player
execute command executes commands on an entity.
@p selector selects a single player.
name= argument only selects entities with a specific name.
execute @p[name=<your username here>] ~ ~ ~ <command> will run
<command> at your location.
testfor the nearest entity.
@e selector finds entities of any type.
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.
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.
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
/execute @p[name=] ~ ~ ~ testfor @e[c=1,rm=0]
As far as using
testfor goes, I'd recommend against it. The scoreboard is almost always more useful, since you can use things like
tag to mark the entity you found.