2

I'm trying to run a command when a player crosses z=0. I currently have the following command in a powered repeating command block:

/replaceitem entity @a[z=0] slot.hotbar.4 written_book 1 0 {Written book data}

I also tried:

/replaceitem entity @a[x=!1000,y=!1000,z=0] slot.hotbar.4 written_book 1 0 {Written book data}

And even:

/replaceitem entity @a[x=1000,y=1000,z=0] slot.hotbar.4 written_book 1 0 {Written book data}

However, in all three circumstances, the command gives the book to a player no matter their position. What am I doing wrong? The wiki says:

[x=X,y=Y,z=Z] — Selects targets based on distance to that exact position.

Do I need all three? If so, can I use ! negators for the x and y values? It's my first time using the position target selectors, so forgive me if I'm missing something obvious.

1 Answer 1

4

The x, y, and z parameters only specify origin, and will still search the entire world for a target closest to that origin (which will always be found). You must use an area limiter such as r for a radius or dx/dy/dz for a square.

However, any of the coordinate origin values not specified will default to the value of the command executor. In this case, by not specifying x or y, they will default to the command block's coordinates. By adding an area limiter to your first command, it would only find players at the specified Z coordinate, but those players must have the same X and Y coordinate as the command block.

The solution is to use /execute to change the origin to that of the targets, and then modify the origin and include an area limiter. You can either modify the origin in the /execute syntax directly, or in the nested selector:

/execute @a ~ ~ 0 /replaceitem entity @a[dx=0,c=1] slot.hotbar.4 written_book 1 0 {Written book data}

Or:

/execute @a ~ ~ ~ /replaceitem entity @a[z=0,dx=0,c=1] slot.hotbar.4 written_book 1 0 {Written book data}

The ! operator can only be used with string-based parameters, being team, name, type, tag, and m.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .