This may be a duplicate, but I need help! I am creating a realistic zombie apocalypse map in minecraft. I want it to detect when the time is 0 and say what day it is. So help me! Tell me what commands and what to do! Please! Thanks to everyone in advance!
-
A daylight sensor should help you.– user114997Nov 1, 2015 at 20:36
-
Aren't daylight sensors used for powering redstone stuff (lamps)?– sensiwooNov 1, 2015 at 21:17
-
@sensiwoo Yes, but think about that for a moment. Because Daylight Sensors are used to power red stone circuits, they can also be used to activate command blocks.– user114997Nov 1, 2015 at 23:11
2 Answers
You can use CommandStats and the /time query daytime
command to get a return value for the current time of day in ticks, which will be perfectly accurate when determining the time of day (even if the time is changed via beds or the /time
command). Note that there is a major difference in the returned value between 1.8 and the current 1.9 snapshot (15w44b).
The value returned in 1.8 is equivalent to the number of ticks that have passed, equal to the number of game days that passed (including skipped nights via beds or any other time-manipulation that isn't /time set ...
). This value allows you to directly detect the number of days passed before the mechanism has been created. To get the current time of day, a modulo operation of 24000 must be applied.
In 1.9, that returned value automatically has a modulo 24000 applied. While that lets us skip a step, we can no longer detect the number of days that have passed before setting up the mechanism, and requires us to manually set a score in those cases.
1.8
Prerequisites:
Objective to hold the value.
/scoreboard objectives add DayTime dummy
Fake player to hold the score value. CommandStats requires a target to be tracked prior to updating their score.
/scoreboard players set Time DayTime 0
Fake player named "24000", used for the necessary modulo/division operation, setting their score to 24000.
/scoreboard players set 24000 DayTime 24000
Detection:
Place down a command block and insert the following command into it:
/time query daytime
Then, stand on top of the command block and run the following command a single time yourself:
/stats block ~ ~-1 ~ set QueryResult Time DayTime
When the command block activates, it will run its command and then say the "DayTime" score of "Time" equal to the current 'daytime' value. To determine the current time of day, you use a modulo operation of 24000 on the returned value.
/scoreboard players operation Time DayTime %= 24000 DayTime
For example, testing if the current time is between 3000 and 9000 ticks regardless of what day it is:
/scoreboard players test Time DayTime 3000 9000
The end result is the current time in ticks.
If you want to find the number of days passed instead, you divide the "DayTime" value by 24000 instead of modulo (you can use a second player to keep a record of the original DayTime if needed):
/scoreboard players operation Time DayTime /= 24000 DayTime
And the following would detect if at least 3 days have passed:
/scoreboard players test Time DayTime 3 *
1.9
Prerequisites:
Objective to hold the value.
/scoreboard objectives add DayTime dummy
Fake player to hold the score value. CommandStats requires a target to be tracked prior to updating their score.
/scoreboard players set Time DayTime 0
Fake player named "Days" to detect number of days passed after the mechanism has been set up.
/scoreboard players set Days DayTime 0
Detection:
Place down a command block and insert the following command into it:
/time query daytime
Then, stand on top of the command block and run the following command a single time yourself:
/stats block ~ ~-1 ~ set QueryResult Time DayTime
When the command block activates, it will run its command and then say the "DayTime" score of "Time" equal to the current 'daytime' value. Since the modulo 24000 was already applied before receiving the value, you do not need to perform the operation yourself.
For example, testing if the current time is between 3000 and 9000 ticks regardless of what day it is:
/scoreboard players test Time DayTime 3000 9000
If you want to track the number of days, you'll need to detect when DayTime is 0 and add 1 to "Days". Note that this isn't particularly accurate, which is another issue concerning 1.9 using modulo before providing the value.
/scoreboard players test Time DayTime 0 0
/scoreboard players add Days DayTime 1
And the following would detect 3 days having passed:
/scoreboard players test Days DayTime 3 *
-
Doesn't
daytime
return time within a day, ranging from 0 to 24000? If I recall correctly, there's agametime
option that returns total time since game started, and that's waht you can divide by 24000 to find out day number.– Orc JMRNov 2, 2015 at 6:33 -
1@OrcJMR
gametime
returns the ticks played, but does not adjust at all if the player sleeps or the/time
command is used. As such,gametime
cannot be used to accurately determine the number of day/night cycles passed in-game. In 1.8,daytime
returns the number of ticks the game was played, adjusted for sleeping and the/time
command. In 1.9, it's currently only between 0 and 24000, andgametime
remains unchanged from its previous function. Nov 2, 2015 at 11:40
You can achieve that goal by using the scoreboard. You will need to track time yourself as it passes, because there is currently no way of telling any redstone machine as far as I'm aware.^1
That said, the machine needs when to start counting. Therefore, you will need to set the time once as soon as you start your clock described below.
On to the counting:
1. Create a scoreboard objective of type dummy and give it whatever name you like. /scoreboard objectives add <name> dummy
2. Every tick, increase the value of a fictional player by one. Scoreboards allow you to track anyone and anything, so you can use time
, for example, regardless of whether that player is on your server or exists at all on Mojang's servers.
/scoreboard players add <scoreboardname> time 1
Executing a command every tick is achieved by any 20Hz clock. In the new snapshots, you can even use the new "repeating-type" command block.
Whenever this score reaches 24000 (one in-game day), you would detect that with a testfor command and then for example execute a /say
command. To track indefinitely many days, you can add 1 to another fictional player, say days
and then subtract 24000 from the time
player, using the scoreboard operation command, or set their value directly to zero.
(I'd like to include more examples, but haven't got anymore time, so I hope this does for now. I'll edit this answer later.)
^1 Of course, there is the sun and there's the moon, and additionally the texture of the clock item changes, but that can not be detected by anything automated within the game itself.
EDIT: might be a duplicate of How to test for time of day with command block in minecraft
-
1@Anonymus: Why the downvote? Could you explain how I maybe can improve my answer?– MarcoNov 2, 2015 at 8:31