How we use the /testfor command to detect if there's a block missing (or a new block) in an area with only one command block and comparator?
3 Answers
If you want to detect blocks, then /testforblocks is what you're looking for. /testfor is used to find entities, such as players, mobs or items. /testforblocks is used as follows:
testforblocks <x1> <y1> <z1> <x2> <y2> <z2> <x> <y> <z> [mode]
from Minecraft Wiki:
Arguments:
x1 y1 z1
andx2 y2 z2
:
Specifies two opposing corners of the region to use as the pattern to test for (the "source region"). x1,
z1, x2,
andz2
must all be between -30,000,000 and 30,000,000 (inclusive, without the commas), and y1 and y2 must be between 0 and 255 (inclusive). May use tilde notation to specify coordinates relative to the command's position. The number of blocks in the source region must not exceed 524,288.
x y z
Specifies the lower northwestern corner (the corner with the most-negative values) of the region to be checked (the "destination region"). x and z must both be between -30,000,000 and 30,000,000 (inclusive, without the commas), and y must be between 0 and 255 (inclusive). May use tilde notation to specify coordinates relative to the command's position. Source and destination regions may overlap.
mode
(optional)Specifies how to match blocks. Must be one of:
all
— every block in the source and destination regions must match exactly.masked
— air blocks in the source region will match any block in the destination region. If not specified, defaults to all.
To use testforblocks, you will need an area on your map to compare against - this will need to be in a loaded chunk, so I would put it underground and out of sight somewhere.
-
I think that we can also put it in the "mother chunk", or if I said wrong it is the chunk where the player spawn. This chunk will always be active, even if the player is far away from it. By doing this, we can make sure that the command will always be run.– KimatuyCommented Apr 1, 2016 at 12:33
-
Sorry about this little detail, but I just tried the command. I don't understand what's the x y z are for because there is already the x1 y2 z3 x2 y2 z2. Thank you for reply.– KimatuyCommented Apr 6, 2016 at 0:12
-
You can answer this question here: gaming.stackexchange.com/questions/261627/…– KimatuyCommented Apr 6, 2016 at 18:13
Testfor is a command used to detect entities, which can be a player, a mob, item frame, item, etc. If using testfor you can hook another command block up to it, or use the execute command, which however, does not support data tags.
The command given in the accepted answer, testforblocks
, was removed in version 1.13. The same functionality is now provided by the execute
command.
Specifically, what would previously require
testforblocks <source_start_x> <source_start_y> <source_start_z> <source_end_x> <source_end_y> <source_end_z> <destination_x> <destination_y> <destination_z>
now instead requires
execute (if|unless) blocks <source_start_x> <source_start_y> <source_start_z> <source_end_x> <source_end_y> <source_end_z> <destination_x> <destination_y> <destination_z> (all|masked) run <command>
Note that the coordinates are used exactly the same as before:
- The six source coordinates select the start and end positions of a volume of blocks to be compared (the 'source volume').
- Note that the coordinates are unordered corner coordinates, not a lower coordinate and a size.
- The three destination coordinates indicate the lowest northwest corner (i.e. lowest three coordinates) of the volume of blocks (the 'destination volume') to which the 'source volume' will be compared. The size of the 'destination volume' will be the same as the size of the 'source volume'.
The big advantages of the execute
function compared to the old testforblocks
function is that the execute
function can be used to directly run a specific command as opposed to merely giving out a success/failure signal and that the blocks
criterion can be combined with any of the other execute
criteria, both of which can result in significantly more compact command block chains.
As before though, you will need to be mindful of where you keep your compared block areas. If the player is not supposed to see the area you are comparing against or there is a risk of the player interfering with the area (e.g. breaking blocks), then you will want to keep that area well hidden.
testfor
! It's already one if the most useless commands (especially for novice command blockers), and more to the point, it was never designed to do what you're asking.testfor
in that instance. It's often hard to tell iftestfor
really is the best option, so you might as well stick to the other options.