Potential method for assigning multiple commands to one condition

๐ŸŽ™๏ธ tryashtar ยท 3 points ยท Posted at 04:39:49 on March 1, 2017 ยท (Permalink)


Hello, my friends. In the world of command blocks, it's really easy to make one command run if the one before it succeeded -- just use a conditional!

The issue comes when you want one condition to activate multiple commands. You can't just use a chain of conditionals, because each one would have to succeed for subsequent ones to run.

One of the most popular methods is to activate another chain, like in this image: https://i.imgur.com/QCDYzrC.png[1]

If the condition succeeds, it will run the IF commands, and either way it will run the AFTER commands when it's done. This is a simple method, but it has a few issues. Firstly, it relies on activation order, making it very hard to nest. Also, both the IF and AFTER chains run a tick later than the earlier portion, which can cause issues.

I have come up with an alternative method, which I believe is better in a lot of ways. Here's an example setup: https://i.imgur.com/AM3m101.png[2]

If the condition succeeds, set the SuccessCount of that block to 1 (so it's checkable later), then run the first IF command. Subsequent IF commands are branched to conditionals that re-check the original block to see if it succeeded. Any AFTER commands can be hooked on to the end like normal, and ELSE blocks are also trivial to add.

The main advantages to this setup are that it all runs in one tick with one chain, and is very easily nestable. The disadvantages are that it takes two blocks for every IF command, and that the /testforblock commands run every tick. However, I believe such an easy check is negligible on server strain, especially when compared to resolving selectors that other methods might have.

Please let me know your thoughts and suggestions, and thanks for reading!


[1]


[2]

pca006132 ยท 1 points ยท Posted at 04:54:48 on March 1, 2017 ยท (Permalink)*

Principle:

Use marker entities to locate the command blocks, and turn on/off the chain command blocks when the condition is met.

This seems to be a bit confusing, but actually it is quite easy to do so. As there are rules for that.

Problem is that this may cause lag if there are a lot of code blocks in the commands.

The main advantage is that this can be used in OOC, we don't have to worry about the coodinate. (My OOC generator handles the summon for me :P). Also, the length of the command will not be too large.

pseudocode:

if (there are arrows)
{
    if (there are items)
    {
        if (there are players)
        {
            say there are arrows,items and players
        }
        else
        {
            say there are arrows and items
        }       
    }
    else if (there are players)
    {
        say there're arrows and players
    }
    else
    {
        say there are arrows
    }
}
else if (there are items)
{
    if (there are players)
    {
        say there are items and players
    }
    else
    {
        say there are items
    }
}
else if (there are players)
{
    say there are players
}

Commands

// means comment mark:(name) means that summon a marker entity at the location of the next command block

//reset the command blocks states
execute @e[type=ArmorStand,name=1] ~ ~ ~ blockdata ~ ~ ~ {auto:1b}
execute @e[type=ArmorStand,name=2] ~ ~ ~ blockdata ~ ~ ~ {auto:0b}
execute @e[type=ArmorStand,name=3] ~ ~ ~ blockdata ~ ~ ~ {auto:0b}

testfor @e[type=Arrow]
//turn on the if block, and turn off the else block... commands later on is just something like that, i think you can understand that so i don't write comments now... so tired :P
cond:execute @e[type=ArmorStand,name=1] ~ ~ ~ blockdata ~ ~ ~ {auto:0b}
cond:execute @e[type=ArmorStand,name=2] ~ ~ ~ blockdata ~ ~ ~ {auto:1b}

    mark:2
    cond:testfor @e[type=Item]
    cond:execute @e[type=ArmorStand,name=3] ~ ~ ~ blockdata ~ ~ ~ {auto:1b}
    cond:execute @e[type=ArmorStand,name=2] ~ ~ ~ blockdata ~ ~ ~ {auto:0b}

        mark:3
        cond:testfor @a
        cond:say there are arrows,items and players
        cond:execute @e[type=ArmorStand,name=3] ~ ~ ~ blockdata ~ ~ ~ {auto:0b}

        mark:3
        say there are arrows and items

    mark:2
    testfor @a
    cond:say there're arrows and players
    cond:execute @e[type=ArmorStand,name=2] ~ ~ ~ blockdata ~ ~ ~ {auto:0b}

    mark:2
    say there are arrows


execute @e[type=ArmorStand,name=2] ~ ~ ~ blockdata ~ ~ ~ {auto:0b}
mark:1
testfor @e[type=Item]
cond:execute @e[type=ArmorStand,name=1] ~ ~ ~ blockdata ~ ~ ~ {auto:0b}
cond:execute @e[type=ArmorStand,name=2] ~ ~ ~ blockdata ~ ~ ~ {auto:1b}

    mark:2
    cond:testfor @a
    cond:say there are items and players
    cond:execute @e[type=ArmorStand,name=2] ~ ~ ~ blockdata ~ ~ ~ {auto:0b}

    mark:2
    say there are items

mark:1
testfor @a
cond:say there are players