Context:
In order to trigger a chat message "from" NPCs - say villagers - when approaching them, I am using an always-active repeat-command block (A), a redstone-signal-sensitive impulse-command block (B) and a redstone comparator between them, pointing from A to B.
Since I want to let the NPCs run free, I can't use absolute coordinates, so what I'm currently doing is:
A: execute at @e[name=Gary] if entity @p[distance=..2]
B: execute at @e[name=Gary] run tellraw @a[distance=..2] "Hey!"
This results in the chat message "Hey!" to appear once for every player with distance 2 blocks or less to an entity named Gary as soon as a player is detected in that space.
Question:
Since it is odd that Gary talks to people in front of him as well as to people behind him, is it possible to alter the commands in a way that the center of the area in which players are detected (and in which players have to be to get the message) is not at exactly Gary's position, but 1 block in front of him?
Deliberations:
If Gary always stood in the same spot, it would be easy by using absolute coordinates like this:
A: execute if entity @p[x=x_,y=y_,z=z_,distance=..2] B: tellraw @a[x=x_,y=y_,z=z_,distance=..2] "Hey!"
x_, y_ and z_ being the desired coordinates 1 block in front of Gary.
Since version 1.13 it is possible to use caret notation for coordinates in some commands making them relative coordinates considering the command's executor's viewing direction, but it seems that target selector arguments only support absolute coordinates in Java edition.
execute positioned
, then you don't even needxyz
in the selector anymore. Also, your two commands don't need to be two. Just make one, it does the same. Not sure what your comparator is supposed to do, but if you want to tell the player only once, just give him a tag and make the speaking command only target players without the tag. – Fabian Röling Jan 15 '19 at 8:15