Minecraft – Check if area contains only one entity in Minecraft with a command block

minecraft-commandsminecraft-java-edition

Is it possible to check if an area contains only one entity with command blocks. I've tried /testfor, but it activates the redstone for one person, even if there are more…

Best Answer

/testfor @e will output a signal strength based on how many entities it finds. For example, if 5 entities are found, the signal strength outputted will travel 5 blocks.

To test for exactly one entity, you can do something like this with and add on an AND gate:

enter image description here


Alternatively, you can use another command block with the 'testforblock' command in it, testing for if the first command block has succeeded exactly once. The setup should look something like:

enter image description here

/testforblock X Y Z command_block 1 {SuccessCount:1}

Where X Y Z are the coordinates of the first command block, and the comparator should come off of this second command block.

Related Topic