Testfor item not working correctly

No one seems to have answered this question from what I've seen, but here's the question. I have this command: testfor @e[type=Item,x=-847,z=156]. What happens is this will trigger for every item laying out, not just the ones whose coordinates -847, y, 156. Can someone please help me on this or tell my what I'm doing wrong?


Solution 1:

The x, y, and z parameters will normally default to the sender's coordinates when not specified (the sender in this case being a command block). By specifying these parameters, all you're doing is changing what the origin is, but nothing more than that. The command will select all item entities, but sorting based on distance starting from the modified origin (where the unspecified y parameter becomes the command block's Y coordinate).

You must use /execute to change the origin to that of the intended targets (while being able to modify that new origin with /execute's syntax), and you must also include a selection limiter such as r or dx/dy/dz to prevent selecting all around the world.

For example, the following will kill any item entities at the X and Z coordinate, replacing the Y coordinate with the item entity's Y value:

/execute @e[type=Item] -847 ~ 156 /kill @e[type=Item,c=1,dx=0]