4

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.

  • Great, I went from "I don't know how to do this" to 2 self-answers now – MrLemon May 27 '16 at 17:52
4

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.
    
2

One way is to create an old-fashioned /setblock clock to trigger a new line of command blocks.

We need 4 lines of command blocks for this:

  • Two (or more) for checking conditions
  • One for running our commands
  • One line for the reset

In the following, X Y Z are the coordinates of a block next to the main command line's first command block.

The reset line is just a single repeating1 command block that is always on, running

/setblock X Y Z minecraft:stone

The conditional lines are using a repeat and a chain command block each. The repeating blocks are used to run your conditions. The chain command block is set to conditional and runs:

/setblock X Y Z minecraft:redstone_block

(replace the first command with your condition).

The main line starts with an impulse command block, set to require redstone. The redstone block will be set and replaced every tick by the other command blocks, so there's no need to use a repeating command block here (shouldn't matter, actually). Every other command you want to run is chained from this one.

1 If you want this to only run once, you can replace all repeating command blocks by impulse ones.

  • Note: I was worried about the reset not happening consistently, depending on the placement of the command blocks in the world, but I haven't actually found a configuration in which this doesn't work. – MrLemon May 27 '16 at 17:24
1

You can store the result of your conditionals using a scoreboard tag

Create a single line of command blocks, starting with a repeating (or impulse) command block, followed by chain command blocks. We'll be using the fact that unconditional chain command blocks execute even when conditional command blocks earlier in the line aren't run because their condition wasn't met.

Start the line by untagging the player(s):

/scoreboard players tag @a[tag=conditionTrue] remove conditionTrue

Now, for each condition, place two chain command blocks. The first one is unconditional and runs your condition. Set the second one to conditional and put

/scoreboard players tag @a[tag=!conditionTrue] add conditionTrue

After placing down all your conditions, put one more unconditional chain block, running

/testfor @a[tag=conditionTrue]

followed by your commands, which are all run by conditional chain blocks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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