0

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?

1 Answer 1

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

1
  • Very interesting. I thought for sure that the redstone block method was the only way! But that's clever, some ways that I never thought of. Thanks for the answer!
    – One 2 Many
    Commented Jun 4, 2020 at 15:15

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.