1

Using only MC command blocks (I am using 1.9) how can I create a short timer?

It needs to be command block-only because I am trying to make a 1-block command; I want to make it night time and give the player a night-vision effect for ten seconds, but after the ten seconds I want to revert it back to day-time. I haven't yet figured out how to achieve this final part, of setting it back to day after ten seconds.

So far I have got the command:

summon FallingSand ~ ~1 ~ {
  Block: command_block,
  Time: 1,
  TileEntityData: {
    Command: /time set 18000
  },
  Passengers: [
    {
      id: FallingSand,
      Block: chain_command_block,
      Time: 1,
      TileEntityData: {
        Command: /effect @a 16 10 1 true
      },
      Passengers: [
        {
          id: FallingSand,
          Block: redstone_block,
          Time: 1
        }
      ]
    }
  ]
}

Sorry for the hierarchical mess, but basically, the above code spawns this structure:

/effect @a 16 10 1 true /time set 18000

Which does exactly what I want it to do: the top command block, just underneath the redstone, gives night vision for 10 seconds, and the bottom command block sets the time to midnight (18000.)


How can I add a bit to this setup to make it wait roughly ten seconds before setting the time back to day?

2

You can make a scoreboard:

/scoreboard objectives add time dummy

Create a entity which counts for you:

/summon ArmorStand ~ ~1 ~ {CustomName:"timeCounter",CustomNameVisible:1}

Make it it invisible and a marker if you want:

/summon ArmorStand ~ ~1 ~ {CustomName:"timeCounter",CustomNameVisible:0,Marker:1b,Invisible:1,Invulnerable:1,NoBasePlate:1,NoGravity:1}

Then a repeating command block which increases this scoreboard every tick (20 ticks = 1 second):

/scoreboard players add @e[CustomName:"timeCounter"] time 1

Put your commands into an execute:

/execute @e[score_time_min=200] ~ ~ ~ effect @a 16 10 1 true

/execute @e[score_time_min=200] ~ ~ ~ time set day

Change the 200 to whatever seconds you want * 20:

Sorry, I first understood your question wrong, just remove the reset.

enter image description here

  • Thank you for your comprehensive answer! Because this is, undoubtedly, the easiest (and most intuitive) way of accomplishing what I posted in my question, I upvoted this because it could be helpful to browsing users. However, this is not the perfect answer for me, as I wanted to rely on as few external variables as possible (it is eventually going to be a player server, so I don't want any redundant scoreboards left lying around.) – theonlygusti May 21 '16 at 10:35
  • You could remove the scoreboard after the 10 seconds: /scoreboard objectives remove time So the scoreboard would be there only for 10 seconds – Simon Meusel May 21 '16 at 11:02
1

When I myself was struggling with this problem, I had an epiphany; why not use a chicken timer?

I could spawn a Chicken far above the command block, and because Chickens fall so slowly I could set up a proximity detector to activate the next set of commands.


While experimenting with the idea I set up this demo (which uses the new 1.9 command blocks):

/kill @e[type=Chicken,r=1] /say Timer finished

In the image, there is a repeating command block pointing into a chain command block.

The repeating command block is set to be Unconditional and Always Active, whereas the chain command block is set to be Conditional, but also Always Active.

The repeating command block's /kill @e[type=Chicken,r=2] command will kill any chicken near to it (radius of two), but it also acts as a sort-of testfor command; its output will be successful only if it does manage to kill a chicken.

This means that whenever it can kill a chicken (ie. there is a chicken within two-blocks' distance of it) the chain command block will also execute, because it is set to be Conditional. This means that the chain command block will only execute its command when the repeating command block behind it has been successful, which only happens when there is a chicken within range of the repeating command block's kill selector.

All we need to do now is set the chain command block's command to be something more useful (you want to set the time to day after the command finishes, so therefore the chain command block's command should be /time set day) and use another command block to summon a chicken far above the structure to act as the timer.


Here is the final minified command:

summon FallingSand ~ ~1 ~ {Block:command_block,Time:1,TileEntityData:{Command:/time set 18000},Passengers:[{id:FallingSand,Block:chain_command_block,Time:1,TileEntityData:{Command:/effect @a 16 10 1 true},Passengers:[{id:FallingSand,Block:command_block,Time:1,TileEntityData:{Command:/summon Chicken ~ ~28 ~ {NoAI:1}},Passengers:[{id:FallingSand,Block:redstone_block,Time:1,Passengers:[{id:FallingSand,Block:chain_command_block,Data:8,Time:1,TileEntityData:{Command:/time set day},Passengers:[{id:FallingSand,Block:repeating_command_block,Time:1,TileEntityData:{Command:/kill @e[type=Chicken,r=3]},Passengers:[{id:FallingSand,Block:redstone_block,Time:1}]}]}]}]}]}]}

It creates this structure (the command is placed into the normal bottom command block):

/kill @e[type=Chicken,r=3] /time set day /summon Chicken ~ ~28 ~ /effect @a 16 10 1 true /time set 18000

The sequence of events which follow from activating the bottom command block are (in order):

  1. The structure is generated, with all the necessary commands and redstone blocks in place.
  2. A chicken is summoned 28 blocks above the summoning command block (24 blocks above the structure)
  3. The players are all given night vision for ten seconds, and the time is set to midnight.
  4. The chicken, with no AI, slowly falls down towards the structure.
  5. When it is in range of the repeating command block, which tries to kill chickens within three blocks' distance, it is killed.
  6. This successful command then activates the conditional command block below the repeating command block, which sets the time back to day.

A walkthrough of the command:

summon FallingSand ~ ~1 ~ {            // Summon the first command block
  Block:command_block,                 // using falling sand entities.
  Time:1,                              // 
  TileEntityData:{                     // This command block sets the
    Command:/time set 18000            // time to midnight, and is at the
  },                                   // bottom of the structure.
Passengers:[{                          
    id:FallingSand,                    // The next command block is a chain
    Block:chain_command_block,         // command block so that it can pass
    Time:1,                            // the signal to ones underneath it.
    TileEntityData:{                   // This command block gives the
      Command:/effect @a 16 10 1 true  // players the night vision effect.
    },                                 
Passengers:[{                          
    id:FallingSand,                    // This command block summons
    Block:command_block,               // a chicken with no AI 28
    Time:1,                            // blocks above it.
    TileEntityData:{
      Command:/summon Chicken ~ ~28 ~ {NoAI:1}
    },
Passengers:[{                          // This redstone block activates only
    id:FallingSand,                    // The bottom half of the tower, so
    Block:redstone_block,              // every command block summoned so far
    Time:1,                            
Passengers:[{                          // This next command block sets the time
    id:FallingSand,                    // back to day. It is a chain command block
    Block:chain_command_block,         // and has to be conditional, the Data: 8
    Data:8,                            // attribute makes it a conditional command
    Time:1,                            // block which points downwards. It is
    TileEntityData:{                   // activated only when the repeating block
      Command:/time set day            // above it is successful.
    },
Passengers:[{
    id:FallingSand,                    // This repeating command block tries to
    Block:repeating_command_block,     // kill a chicken within three blocks of
    Time:1,                            // itself.
    TileEntityData:{
      Command:/kill @e[type=Chicken,r=3]
    }, 
Passengers:[{                          // I couldn't figure out how to summon an
    id:FallingSand,                    // "Always Active" repeating command block,
    Block:redstone_block,              // so it needs this final redstone block
    Time:1                             // to run.
}]}]}]}]}]}]}

Important Notes

During my testing, I found 28 blocks above to be the optimal distance to summon the chicken for a roughly ten-second timer, however this can be easily adjusted by changing the

/summon Chicken ~ ~28 ~ {NoAI:1}

command to summon at a greater or lesser height.

Also, note that the Passengers attribute in 1.9 has replaced the Riding attribute of earlier versions. Read about these changes here: 15w41a - Official Minecraft Wiki

Finally, the new 1.9 command blocks use their damage values to decide whether to be conditional or not. This is why I had to set the Data property of the second-highest command block to 8, to ensure it was conditional.

Anyone, please post any questions in the comments if you are still confused.

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.