4

For some reason they thought it would be a good idea for the dx, dy, dz parameters to only ever extend from the command block itself. Is there a way, maybe combining this with execute, to select a volume remotely?

The goal is to see if players are within a rectangular prism region.

3

4 Answers 4

4

The x, y, and z arguments can select the starting corner of the search box:

/testfor @a[x=100,y=60,z=100,dy=10,dx=10,dz=10]

Or, more compactly:

/testfor @a[100,60,100,dx=10,dy=10,dz=10]

3
  • can you provide some evidence of this working? ive tried this before and tried it again after your suggestion and it does not seem to work.
    – indy
    Mar 7, 2015 at 19:36
  • @indy Added a video to the post.
    – SirBenet
    Mar 7, 2015 at 19:55
  • awesome. the bounding volume numbers dont work exactly as i expected them to- had to subtract one from the X and Z i expected to be able to use. thanks!
    – indy
    Mar 9, 2015 at 22:26
1

You are correct, you can use the execute command to do anything remotely. Just spawn an invisible armor stand named Dummy somewhere near the command block (it doesn't matter where). Then run

/execute @e[name=Dummy] <world coordinates here> <command to run here>

Many commands have more compact ways of doing things like this, but this works for any command.

0

It's possible to select a cuboid region but the command block must represent one of the corners.

[dx=DX,dy=DY,dz=DZ]

To use this, place the command block in the corner of the area you are selecting. This needs to be in the corner with the lowest coordinate values, i.e., the bottom, southmost, westmost corner. DX, DY & DZ represent the size of the cuboid- replace them with those values. For example,

[dx=10,dy=5,dz=10]

will target everything in a 10x10 area that is 5 blocks high.

You need to put this in the command that needs target selection. This is done in the same way that you would set an area by point and radius. For example:

kill @e[dx=10,dy=5,dz=10]

0

To give a modern answer to an old question...

As of version 1.13 one can use execute positioned <position> (and execute positioned as <entity>) along with the [dx=<value>,dy=<value>,dz=<value>] target selector arguments to select any volume relative to a specific position or entity.

For example, to list all entities in a 5x5x5 cube around the executing entity one can use:

execute positioned ~-2 ~-2 ~-2 run say @e[dx=5,dy=5,dz=5]

(Note that the executing entity will be included in the list if it is not a command block or the server.)


In the OP's use case, when run from a command block this would detect all entities in a 5x5x5 cube around a command block with the block itself in the centre rather than being the lowest corner of the volume.

By changing the offset one can select any volume at any position in the world relative to the executing entity.

Naturally the old technique for selecting an entity in a fixed volume also works:

say @e[x=0,y=0,z=0,dx=5,dy=5,dz=5]

For commands that don't have arguments that require target selectors, execute if can be used to run any command if an entity exists within the specified volume.

For example, to change the difficulty to peaceful if an entity is found in a 5x5x5 cube around an executing entity:

execute positioned ~-2 ~-2 ~-2 if entity @e[dx=5,dy=5,dz=5] run difficulty peaceful

(Note that the executing entity will trigger the condition if it is not a command block or the server. To avoid this, make the target selector condition more specific, or define it in such a way that it excludes the executing entity.)

You must log in to answer this question.

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