Minecraft – How to detect the death of a mob

minecraft-commandsminecraft-java-edition

I'm making an adventure map and I'm trying to detect the death of a mob with a command block so it will activate redstone, but I don't know how to do this.

I am using a custom mob and when it dies, dialogue activates and it teleports the player to a specific area (as an example to a level selecter).

I need to know how to do it because if not the person could just wait for the timer out and then get teleported.

So, how can detect a custom mob death, with a command block so it will activate redstone and the player can continue on with the map?

I'm using Minecraft version 1.13.1

Best Answer

One way to do this involves summoning mobs with armor stands riding them. When the armor stand can no longer detect the mob (because it is dead), then it is tagged. You can do whatever you want by executing all armor stands with that tag and then kill.

I'll use a zombie with an armor stand named "deathdetect" for this example. Notice that I've required persistence, which means it cannot despawn. If it despawned it would trigger the armor stand.

summon zombie ~ ~ ~ {PersistenceRequired:1b,Passengers:[{id:"minecraft:armor_stand",Marker:true,CustomName:"\"deathdetect\"",Invisible:true,NoGravity:true,Tags:["alive"]}]}

Now put these commands in a repeating chain. The first one adds all tags to the armor stands, then removes them if there is a zombie around. If there is no zombie around, they keep the tag. I'll use the tag "trigger" for this example.

tag @e[type=armor_stand,name=deathdetect] add trigger
execute as @e[type=zombie,nbt={Passengers:[{id:"minecraft:armor_stand",CustomName:"\"deathdetect\""}]}] at @s anchored eyes if entity @e[type=armor_stand,name=deathdetect,distance=..1.5] run tag @e[type=armor_stand,name=deathdetect,distance=..1.5,limit=1] remove trigger

Now add whatever redstone you want to activate by executing the armor stands with the trigger tag, then kill the armor stands.

execute as @e[type=armor_stand,tag=trigger] run say it works!
execute as @e[type=armor_stand,tag=trigger] at @s run setblock 45 6 3 redstone_block
kill @e[type=armor_stand,tag=trigger]

NOTE: If the distance for the zombie detecting machine is too low this machine may have a tendency to add the trigger tag when the zombie is alive. I've already increased the distance to 1.5, but if it's still bugging you can increase it to 1.51, 1.52, etc.