Minecraft – How to run chains after a /testfor command without having each additional command block being conditional to previous steps

minecraft-commandsminecraft-java-edition

In Minecraft Java Edition 1.12 and under, the only way to run a command on a condition is to use /testfor, /testforblock, or /testforblocks to test, and include a conditional chain command block which only executes if the test passes.

When creating a multi-command chain starting with any of the aforementioned commands, all the rest of the command blocks have to be conditional for them only to be executed when the /testfor succeeds. If any of them are not conditional, they will execute even if the /testfor fails.

The problem is, when a command block is conditional, all command blocks before that have had to execute their command and return a non-zero value. This not only includes the original /testfor, but also every other step in the chain.

For example, let's say I have the following commands in order in a repeating command chain:

REPEATING, NONCONDITIONAL: /testfor @a[x=0, y=1, z=0, r=0.4]
CHAIN,     CONDITIONAL:    /setblock 0 0 0 air
CHAIN,     CONDITIONAL:    /say False Floor Trap!! LOL

This means that the message on screen will only be displayed if the testfor succeeds. All is well, until we add the fact that if the /testfor succeeds but the /setblock fails, the /say still won't run.

But if we make it nonconditional, it will always run, even if the /testfor fails.

What is the best way to make a conditional chain command block run if the /testfor passes, but still run if a step in between fails?

Best Answer

  • Functions existed in 1.12 already, so you could make the command block run a function.
  • You could place a redstone block at another repeating command block chain or set the first one to auto:1, but that would only execute the commands there one tick later. Make any command in that chain to remove the redstone block or tag again to make it not loop forever.
  • You can use something similar to 1.13's /execute if syntax in 1.12 already. For example in your example the commands would be these (and this can also be used for NBT checks):
scoreboard players set @a[x=0,y=1,z=0,r=.4] foo 1 {SomeNBT:true}
execute @a[foo_min=1] ~ ~ ~ setblock 0 0 0 air
execute @a[foo_min=1] ~ ~ ~ tellraw @a {"text":"[@] False floor trap! LOL"}
scoreboard players set @a foo 0

As you can see, I changed the /say to a /tellraw, because doing it this way always changes the executor and the execution position away from the command block. That's just a downside of 1.12 and one of the reasons why /execute was overhauled in 1.13.