How can I test for there being 0 entities? As in, do something when there are no iron golems in a certain radius?

How can I test for there being 0 entities? As in, do something when there are no iron golems in a certain radius? - Aluminum Cans Passing Through the Assembly Line

I am hoping to make a command that will do something when the /testfor command brings up 0 of a specified entity. For example:

/testfor @e[x,y,z,r,type=Zombie,count=0]



Best Answer

Do the following: enter image description here Make the command block that is lowest testfor @e[r=[radius], type=VillagerGolem] (Replace [radius] with your test radius (Also, remove the []))

And make the upper one whatever you would like to do when there is no Iron Golem in that radius.




Pictures about "How can I test for there being 0 entities? As in, do something when there are no iron golems in a certain radius?"

How can I test for there being 0 entities? As in, do something when there are no iron golems in a certain radius? - Cans of Beer in the Production Line
How can I test for there being 0 entities? As in, do something when there are no iron golems in a certain radius? - Gray Steel Flower and Garden Bucket
How can I test for there being 0 entities? As in, do something when there are no iron golems in a certain radius? - Tin vessels and metal bucket with milk placed near bike leaned on shabby rusty wall



How do you detect if an entity is holding an item?

The @e target selector is used to target all entities in the game. This includes both players and mobs in the world. Notice that in this last example, we have used [type=enderman] to target only enderman. This value is called a target selector argument and allows us to filter the target.

How do you target specific mobs in Minecraft?

When in-game, game rules can be changed using the /gamerule command, using the syntax /gamerule <rule> [<value>] , where <rule> is the name of the game rule and <value> is an allowed value, which depends on the type of the game rule.

How do you set Gamerules in Minecraft?

NBT tags are used in game commands such as: /summon, /data, /give, /fill, /setblock. The NBT tag is always surrounded in {} such as {NoAI:1}. If there is more than one NBT tag used in a command, the NBT tags are separated by a comma such as {NoAI:1, IsBaby:1}. Each entity (or mob) has its own unique set of NBT tags.



Minecraft Iron Farm: Survival Friendly TINY Iron Golem Farm with 5+ stacks p/hr (Avomance 2020)




More answers regarding how can I test for there being 0 entities? As in, do something when there are no iron golems in a certain radius?

Answer 2

Use a scoreboard counter that have 1 added to it per entity that matches the requirements

Setup:

/scoreboard objectives add Counter dummy
/summon ArmorStand <X> <Y> <Z> {CustomName:Counter}

Clock (20 Hz):

/scoreboard players set @e[type=ArmorStand,name=Counter] Counter 0
/execute @e[type=VillagerGolem,x=<X>,y=<Y>,z=<Z>,r=<Range>] ~ ~ ~ scoreboard players add @e[type=ArmorStand,name=Counter] Counter 1
/execute @e[type=ArmorStand,name=Counter,score_counter=0] ~ ~ ~ <Desiered Command>

Technical Stuff:

Setup:

First a scoreboard objective called Counter is created. Then a armorstand called Counter is summoned.

Loop:

The score is first reset, then for every Iron Golem in range 1 is added to the ArmorStand's score. Lastly the execute command will only run on any armor stand called Counter with a score of 0.

If you change the score_counter of the last command, you can change how many Iron Golems you want before the command is ran.

Answer 3

enter image description hereUsing 2 command blocks, 1 comparator, 1 building block, 1 redstone torch. You can accomplish this thus:

  • Command Block 1: testfor @e[r=radius, type=VillagerGolem]
  • Command Block 2: your command

You will still need to put a clock on it. Mine is underneath the golem :)

As a side note, you don't want to use the c=0 because that is not saying find if there are 0 but rather it's saying when you match, match 0. So in your case, that has no real effect.

Answer 4

Make an if else statement. For example. Start by creating a scoreboard.

/scoreboard objectives add IfElse dummy

With this scoreboard you will detect if something succedeed or not. You will be using the score 0 for when something didnt happend(A testfor failed.) and the score 1 if something suceeded.

Laying down a clock, were you put your first command block to repeating, on a piece of redstone. Now for a score to be carried you need a Armorstand. So summon an armorstand that has the name "IfElse1" or something.

Now, for the first block in your command block line you want to set the amorstand(s) scores to 0, so that every time the clock finishes testing for scores/change them, they will be put back to normal. (Score 0)

/scoreboard players set @e[name=IfElse1] IfElse 0
                    (Armorstand)and(Scoreboard)

If your going to be using sereval IfElse statements in your contraption i suggest giving it a tag, so that you can target and reset all your IfElse(x) armorstands easily only using one command block. So something like this:

/scoreboard players set @e[tag=IfElseAll] IfElse 0

So you could put this as your first command instead.

Next is were you wanna testfor the opposite of what you actually want to find out and get an output from, to know if something issnt true, you first need to find out what kind of output you will get from doing a check. So something like this

/testfor @e[type=Player,r=20] 

(Could add a region tag, to check within a box, or a tag, or anything that could be given to the player to count them as beeing in game.

Example:

tag:

/testfor @e[type=Player,tag=InGame]  

score:

/testfor @e[type=Player,score_GameStatus_min=1,score_GameStatus=1]

Next you want to have a conditional command block. This commandblock will get an output if the test was successfull, or not be activated at all if it wassnt. Meaning this is were you would put your output from there beeing players in the game. But were trying to find the opposite, so you put the following.

/scoreboard players set @e[name=IfElse1] IfElse 1

This will change the armorstands default score(0). To 1. Indicating that there is players in the game. But if that is not the case. The score will remain(0). (If you would like an output from there beeing players in the game. you may put more conditional command blocks. But remember, if one fails the chain does not activate all the way.)

Now after your testing for players, you know know if theres players or not in your game, determined by the state of the score "No players(0)" "Players(1)". Now to get an output from there not beeing players in the game, do another testfor command in the same chain "that is not conditional".

/testfor @e[name=IfElse1,score_IfElse_min=0,score_IfElse=0]

(Checking for a miniumum of 0, and a maximum of 0 score for the armorstand "IfElse1" in the scoreboard "IfElse".)

Now make another conditional command block chain, with whatever outputs you might want. For there beeing no players in the game.

And you should be all set.

No comparators, or redstone needed.

Answer 5

This is trivial in 1.13:

/execute unless entity @e[...] run ...

Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: cottonbro, cottonbro, Clem Onojeghuo, Plato Terentev