TransWikia.com

How do I detect if there are any mobs in a specific area and execute a command when they are no longer present in that area?

Arqade Asked by Marker9999 on April 3, 2021

I’m working on a map, which has a castle in it. The castle has different levels (floors), and when a floor is cleared (when there are no mobs left on that floor), I want to make a button appear to open some iron doors, for the players to be able to go up to next floor.

3 Answers

If you really wanted to you could just do a really long command that checks for each potential mob type in the floor.

execute unless entity @e[type=zombie,x=14,y=12,z=21,dx=20,dy=8,dz=13] unless entity @e[type=creeper,x=14,y=12,z=21,dx=20,dy=8,dz=13] unless entity @e[type=spider,x=14,y=12,z=21,dx=20,dy=8,dz=13] unless entity @e[type=skeleton,x=14,y=12,z=21,dx=20,dy=8,dz=13] unless entity @e[type=enderman,x=14,y=12,z=21,dx=20,dy=8,dz=13] unless entity @e[type=silverfish,x=14,y=12,z=21,dx=20,dy=8,dz=13] unless entity @e[type=endermite,x=14,y=12,z=21,dx=20,dy=8,dz=13] run setblock 30 14 26 acacia_button[facing=south]

Correct answer by Airtoum on April 3, 2021

Try this, hopefully you can understand it:

  • 1 means a repeating command block
  • 2 means a chain command block
  • c means conditional
  • a means always active
  • Arrows indicated direction of blocks (1> for example)
  • dummy is the holder of the objectives
  • mobs_in_floor tracks the mobs in the floor
  • mob_entered tracks whether a mob has entered the floor

Command blocks: 1a>2ac>

Command for 1a:

execute if score dummy mobs_in_floor matches 0 run execute if score dummy mobs_entered matches 1.. run scoreboard players set dummy mobs_entered 0

Command for 2ac is the command you want to execute.

Answered by halp on April 3, 2021

This method uses a scoreboard to track the mobs on a floor, and executes a command when the scoreboard is 0.

Important: This method works only in versions 1.13+

Preparation

First off, you need a scoreboard with a dummy criteria. You can do that with the following command.

scoreboard objectives add floor.mobs_left dummy

Next, we need to assign names for the floors, to hold values in the scoreboard. The names are going to be fake players, otherwise it will interfere with the players playing the map. To name these fake players, add characters that are not allowed in normal names, I put "%" in front of the names. Remember these names, because we will need them later. Next, place a Repeating Command Block set to Always Active facing a Chain Command Block set to Always Active.

After Preparation

Now we get to the commands part. First off, in the Repeating Command Block you just placed down, you need a command that tests for entities, then stores it in the appropriate fake player value in our scoreboard. You can do that with the following command.

execute if entity @e[<args>] store result score <fake player> floor.mobs_left

Depending on how your mobs are spawned, you can choose one of the following ways to detect them:

  • Tags: If your mobs are spawned with commands, you can tag them with a recognizable tag to detect later. You can spawn them with the following command:

    summon wither_skeleton ~ ~ ~ {Tags:["floor1"]}
    

    To detect it, replace <args> with tag=floor1 in the above command.

  • Rectangular prismic box:

Continue reading here

After you have finished setting up the previous command, in the Chain Command Block, add the command you want to execute if the scoreboard is 0. You can do that with the following command.

execute if score <fake player> floor.mobs_left matches 0 run <your command>

Remember to repeat the command process for each floor

Summary

This method grabs the amount of entities in an area or with a specific tag, and stores it in a scoreboard. Then executes any command if there is no entities matching the criteria.

Answered by randomuser922 on April 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP