6

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]

2
  • You got it, you have to use x=n, y=n etc but that should do it. r=radius (circular)
    – ydobonebi
    Commented Aug 2, 2015 at 0:50
  • if you are using a command block and it's the center point, you don't have to give xyz, for example you could do testfor @e[r=10000, type=zombie] and then detect the signal from the commandblock, negate it (test for NOT entites) and send that signal to what ever you want it to do....
    – ydobonebi
    Commented Aug 2, 2015 at 1:30

5 Answers 5

3

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.

1
  • Damn, I was just about to post this picture and all (except I did mine in the new ender city :) ) BTW< you don't need the redstone dust after the comparator or after the torch...
    – ydobonebi
    Commented Aug 2, 2015 at 1:57
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.

2
  • 1
    Variant solution to count the Golems: Use /testfor @e[type=VillagerGolem,x=<X>,y=<Y>,z=<Z>,r=<Range>] and /stats block X Y Z set SuccessCount @e[type=ArmorStand,name=Counter] Counter where X Y Z are the coordinates of the first command block.
    – MrLemon
    Commented Aug 2, 2015 at 10:11
  • Yeah, that was the other solution to this using stats.
    – Moddl
    Commented Aug 2, 2015 at 12:40
1

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.

2
  • Thanks for the help! Just one question, what does it mean to negate a command block? Commented Aug 2, 2015 at 5:03
  • @AidenHerrod it outputs only on a true test. You want a false test. So test for true, then you not gate (the block with redstone torch in this case) to get the desired effect.
    – ydobonebi
    Commented Aug 2, 2015 at 6:22
1

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.

1

This is trivial in 1.13:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.