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?
Minecraft – command block command to see if there are any mobs in a player’s area
minecraft-commandsminecraft-java-edition
Related Solutions
You want to do this with scoreboards. My solution won't prevent getting diamond swords, but it will prevent crafting them and using them, i.e. a player can have a diamond sword in their inventory, as long as they never use it. How it gets there without crafting it, though, is pretty much impossible without cheats.
First, you need to set up 2 scoreboard objectives, one for crafting, and one for using:
/scoreboard objectives add craftedDiamondSword stat.craftItem.276
/scoreboard objectives add usedDiamondSword stat.useItem.276
Next, you need to set up two nearly identical chains of command blocks, one for each scoreboard objective. This will run on a clock like you already have. I'll only give the commands for using, so just replace that in the second chain with the crafting objective.
First, clear those players inventories:
clear @a[score_usedDiamondSword_min=1] 276
Next, tell the players that used a diamond sword to not use it, and reset their score:
tell @a[score_usedDiamondSword_min=1] Don't use diamond swords!
scoreboard players set @a[score_usedDiamondSword_min=1] usedDiamondSword 0
This last command block doesn't really need the [score_usedDiamondSword_min=1]
but it's useful.
I got this to work in a single line of command blocks with redstone over top for the uses chain, but it wouldn't work for the crafting. The solution is to pipe the result from the clear command block into the tell and set scoreboard command blocks with comparators. Here's a pic of the two setups:
Use whatever clock you want.
Additionally, if you never, ever want players to have a diamond sword, even in their inventory, and even if they don't use it, you can still clear from @a
, and then just say
to not use diamond swords if one is found.
Update for added 1.8 functionality:
The 1.8 snapshots have added the option to match players against the Inventory
NBT tag when setting or incrementing a scoreboard objective. This means we can set up a dummy scoreboard objective to track whenever someone has a Diamond Sword in their inventory, regardless if they've used it or crafted it. First, set up the scoreboard objective:
/scoreboard objectives add hasDiamondSword dummy
Next, we need to run two command blocks on the same clock; one to set players scores, and another to check those player's scores and clear the diamond swords:
scoreboard players add @a hasDiamondSword 1 {Inventory:[{id:"minecraft:diamond_sword"}]}
clear @a[score_hasDiamondSword_min=1] minecraft:diamond_sword
The second command is fairly similar to the old commands, except instead of using the old ID number, I'm using the name instead. The first is a bit more complicated, so let's walk through it. Basically it says add to all players on their hasDiamondSword
objective the value 1 if that player has an inventory and that inventory contains a diamond sword. So, for anyone that has a diamond sword in their inventory, their hasDiamondSword
objective will be incremented.
Finally, like in the old version, we need to tell
the players to not use diamond swords, and to reset their hasDiamondSword
objective:
tell @a[score_hasDiamondSword_min=1] Don't use diamond swords!
scoreboard players set @a[score_hasDiamondSword_min=1] hasDiamondSword 0
I stacked these two command blocks (with the tell
on bottom) and fed them with a comparator from the clear
command block. This is what it all looks like in the end:
What you want is the stats
command, with the SuccessCount
stat.
The stats
command
The command syntax for what you want to do is
stats block <x> <y> <z> set <stat> <selector> <objective>
<x> <y> <z>
are the coordinates of the block running the command you want to get the result data for.
<stat>
is one of AffectedBlocks
, AffectedEntities
, AffectedItems
, QueryResult
or SuccessCount
, depending on what you want to do (check the wiki for more information). SuccessCount
is typically the same as output redstone power, but isn't capped to 15 (IIRC, it does vary for some commands).
<selector>
and <objective>
specify a target entity and a scoreboard objective in which to store the result. I suggest using either @a
(useful for executing directly on the players if a condition is met) or a named armor stand as the selector.
Example
We have a command block running a testfor
command (at coordinates 1 2 3), and we want to run another command if at least 4 entities (e.g. players) qualify*. We create a (dummy) scoreboard objective called "SCObjective" and summon in an (invisible, marker) armor stand called "SCDummy". We create another command block, running
stats block 1 2 3 set SuccessCount @e[type=ArmorStand,name=SCDummy] SCObjective
Afterwards, we can use
execute @e[type=ArmorStand,name=SCDummy,score_SCObjective_min=4] ~ ~ ~ <other command>
to run our command based on the result of the testfor.
* This is one of the few cases where testfor
is useful and can not easily be replaced by execute
.
There is an alternate version of the command for use with entities:
stats entity <selector2> set <stat> <selector> <objective>
This will return a <stat>
for a command run by <selector2>
(including when running execute
off that entity).
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:
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.