Minecraft – command block command to see if there are any mobs in a player’s area

minecraft-commandsminecraft-java-edition

I would like to have a security system to see if there are mobs in my area. I want to set up a command block with an always-active command which is going to send a signal to another command block if there are any mobs in my area. The other command block will send me a warning. Is it possible to do something like that?

Best Answer

Run the following commands in order, replacing X, Y and Z with the values of the X, Y and Z coordinates respectively. Also replace DX, DY and DZ with the size of the area in the X, Y and Z direction respectively:

/scoreboard players add @e[type=!Player] tell 0
/scoreboard players tag @e[type=!Player] add mob
/scoreboard players tag @e[type=ArmorStand] remove mob
/execute @e[tag=mob,x=X,y=Y,z=Z,dy=DY,dx=DX,dz=DZ,score_tell=0] ~ ~ ~ /tellraw @p ["Mob Detected"]
/scoreboard players set @e[tag=mob,x=X,y=Y,z=Z,dy=DY,dx=DX,dz=DZ,score_tell=0] tell 1

Also replace "Mob Detected" with the text you want to show.

How this works is it executes a tellraw command as every mob inside the specified area and then adds a score to the mob so that it doesn't execute the command again.