Minecraft – How to join two lines of conditional chain command blocks without resorting to redstone

minecraft-commandsminecraft-java-edition

After reading this question and the answer by Simon Meusel I started to wonder how one would go and join two lines of conditional command blocks.

As an example, let's say that I want to run a series of commands at dawn (daytime 0-1000) and dusk (12000-13000). I use the /stats command to store the value of /time query daytime in a dummy objective (daytime) on a dummy player (#DAYTIME).

I set up two command blocks to test if the daytime is correct:

/scoreboard players test #DAYTIME daytime 0 1000
/scoreboard players test #DAYTIME daytime 12000 13000

I can use conditional chain command blocks to run whatever I'd like from here. But if there's a lot of commands, I'd rather not place every command block twice. Is there a way to join two conditional command block lines, i.e. create a logical OR using command blocks, without resorting to comparators, redstone dust, or the laggy like? Summoning redstone blocks is okay, as long as it is properly reset every tick.

Best Answer

You can use CommandStats to simulate an OR gate while reducing the amount of required command blocks (since you'd essentially be running two commands for the price of one).

Prerequisites

Objective to hold the CommandStats result.

/scoreboard objectives add Condition dummy

Single armor stand to summon, which could also be used for other chains that needs it (as its score is to be reset at the beginning of the chains). This will be the entity subject to CommandStats.

/summon ArmorStand ~ ~ ~ {Tags:["conditional"]}

Mechanism

Example mechanism (you can swap out the impulse for a repeating block):

Impulse/repeating > chain > chain > chain > conditional chain

Command blocks 2 and 3 will be the initial conditional blocks, where if either are successful you would run more commands. Place a temporary command block above each of them, place the following command inside it, and activate it (you would otherwise just run the command yourself, but the character limit was exceeded):

/stats block ~ ~-1 ~ set SuccessCount @e[type=ArmorStand,tag=conditional,score_Condition=0] Condition

Now if either of those two commands run, the armor stand will have their "Condition" score set equal to the number of successful iterations of the command.

However, it will only target the armor stand if it has a "Condition" score of 0. This means that if either of them are successful, the score will be 1 and will remain at 1 even if the next command block is unsuccessful.

Commands

The following are the commands for the example mechanism above.

  1. Set the armor stand's "Condition" score to 0. This is needed so it can be targeted by this chain again.

    /scoreboard players set @e[type=ArmorStand,tag=conditional] Condition 0
    
  2. One of the conditionals to check. The success of this command will trigger its stored CommandStats.

    /scoreboard players test #DAYTIME daytime 0 1000
    
  3. The second conditional to check.

    /scoreboard players test #DAYTIME daytime 12000 13000
    
  4. After the conditionals have finished, the armor stand will have a score of 1 if either of them were successful. You can then detect this armor stand with a score of 1.

    /testfor @e[type=ArmorStand,tag=conditional,score_Condition_min=1,c=1]
    
  5. Command(s) to run if either of the conditionals were successful.

    /say The time is either 0-1000 or 12000-13000.