How do I detect players in a certain area using Command Blocks

I'm building this map and I have 4 different ways you can go and what I want is if someone walks through an archway or something the path will open up using Command Blocks to sense if a player has done that but I don't want to use Trip Wires. Can anyone help?


Solution 1:

For starters, you need a fast clock, although it doesn't necessarily need to be a fill clock. I'll use one in this example, though, since it'll simplify some things (although it'll take up way more space than what's actually required).

I'm assuming this is a CTM style adventure map, and so this will be used at a 4-way intersection. Below the very center (this isn't strictly required, but it's easier), I placed a fill clock, like so:

Step 1: fill clock

For reference, the two commands are

fill ~-10 ~-1 ~-10 ~10 ~-1 ~10 redstone_block
fill ~-10 ~1 ~-10 ~10 ~1 ~10 air

That will easily allow you to do a 15x15 intersection. Also, this clock won't start on it's own, but all you need is to set the air block between the two command blocks to a redstone_block. I did this with another command block 2 blocks higher, using the setblock command so that I could use relative coordinates. Additionally, I co-opted the top command block to create the platform for my intersection, just to make life easy, since it needs to be done before starting the clock.

Next, let's get one path detecting players. The first step in that process is using testfor to see if a player is nearby:

testfor @a[r=4]

I'm using @a[r=4] to test if a player is within 4 blocks (@p would also work) because ~ notation doesn't work in the target selectors; otherwise I'd have to know where the lower north-west corner of the box is, plus what length I want each side to be. Admittedly, the second part of that is pretty easy.

Out of that command block is a comparator that feeds into another command block that, for me at least, sets a redstone block to power some redstone lamps:

setblock ~ ~6 ~ redstone_block

This is where you would put the signal input for your door. I stuck a torch on the side of that command block so I could turn the lamps off as I left the area, again using a generic command:

fill ~-3 ~6 ~-3 ~3 ~6 ~3 air

This of course will fill a large slab with air, so it's hard to put anything up there, but as my creation is just a demo, I didn't care much. In your case, this may not even be necessary, as your door could auto-close, or you may just want to leave it open forever. In any case, those three command blocks look like this:

Step 2: detector

And this is the final product:

Final product, 3/4 view

Final product, view from tunnel

P.S. The only way to stop a fill clock is to break the command block that fills in the redstone blocks. It's a good idea to turn off clocks that aren't being used, and for adventure maps, to just keep a single clock running at the world spawn.

Solution 2:

I have one trick to make this a little easier, so the command blocks are not under the door.

If you would like to detect players in a radius around a certain coordinate, instead of a radius around the the command block, you can use a command like this:

/testfor @a[x=X,y=Y,z=Z,r=RADIUS]

To make this a bit clearer, the X,Y, and Z in the command are the center coordinates of where the command block will test for players. The RADIUS in the command is how close the player needs to be to the coordinate (the X,Y,Z specified above), to trigger the command block.

Here is an example:

/testfor @a[x=15,y=45,z=39,r=3]

Hope this helped!