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.
4 Answers
/execute as @e[sort=nearest] run <'whatever'>
for 1.13
Minecraft selectors work on some very strange principles. @a
, @p
, and @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 @e[c=1]
.
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
/execute @p[name=] ~ ~ ~ testfor @e[c=1,rm=0]
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 like tag
to mark the entity you found.
Try this (It will only test for mobs and not players nearby):
/execute @a ~ ~ ~ testfor @e[type=!Player]
This is what I think you mean in the question, but I don't know. !Player is case sensitive, so be careful.
-
-
Ok so you know how when u type in the chst /testfor @p it will tell you the closest player, i just want it to test for the closest entity. And a command to put in the command block so the command block can run the command for the player Jan 22, 2017 at 22:55
-
ok now that this works, is it possible to do, lets say, "save" that entity, so it teleports me to the nearest entity then saves that entity Feb 14, 2017 at 0:13
-
also , is it possible for me to do let say, if the closest entity is a pig say hi, and if the closest entity is a wither, do that, and so on. Is this possible? Feb 14, 2017 at 0:21