How to make a command set the time to day if it is night or night if it is day?
-
My friends and I are coding a sandbox game in Minecraft. Instead of using the default daylight cycle, I want to make a button that causes the time to change. Of course, I could simply make two buttons, one for
/time set noon
and one for/time set midnight
, but I want to make one button that basically changes night to day and day to night.I have no idea where to even start, and although I've been working with commands for a while now, I've never tried anything like this.
Some background information:
I am on Java edition, so please don't suggest any bedrock ideas.
I can use chain command blocks if needed, as I have plenty of space to use.
I can use scoreboards if I must, but I'd much rather use one simple command. However, if scoreboards are necessary, I am willing to!
If any selection tool has to be used, I would much rather use @p than anything else. Please take this into consideration!
Please use only command blocks and minimal redstone. I cannot use daylight sensors as these command blocks will be underground and I can't have any circuits sticking out.
I know these are pretty picky rules, but I would really appreciate it if you could help me with this. Of course, like I mentioned, I could just have two separate buttons, so if there's no way, I am willing to do so.
-
My best guess is that this is not possible in one command since if/else is not currently supported.
However, you could have a small datapack that does all that behind the scenes when you run a single command:
In your command block, you would have the command:
/function minecraft:timeswap
then in a folder called functions, you would have an MCFUNCTION file named timeswap containing your commands:
scoreboard objectives add Time dummy
This makes a scoreboard to use later^
execute store result score thetime Time run time query daytime
Gets the current game time and saves it as the Time score of a player named thetime
scoreboard players set night Time 12542
Saves the value of the first tick players can sleep (aka the start of night) as the score of a player named night
execute if score thetime Time < night Time run time set night
This sets the time to night if the current time is less than the tick night starts. Note, using "time set night" actually sets it to 13000 so you lose about 458 ticks.
execute if score thetime Time > night Time run time set day
This sets the time to day if the current tick is greater than the tick night starts. Note, using "time set day" sets the time to 1000 so you lose 1000 ticks.
If you want either of these times to be exact, you can change day to 0 and night to 12542.