Minecraft – How to make a short timer using only command blocks

minecraft-commandsminecraft-java-edition

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?

Best Answer

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