diff --git a/main/time.lua b/main/time.lua new file mode 100644 index 0000000..853b759 --- /dev/null +++ b/main/time.lua @@ -0,0 +1,53 @@ +-- Define constants +local tick_per_second = 20 +local tick_per_minute = tick_per_second * 60 +local tick_per_hour = tick_per_minute * 60 + +-- Initialize objectives and constants +commands.exec("scoreboard objectives add time_t custom:play_time") +commands.exec("scoreboard objectives add time_h dummy") +commands.exec("scoreboard objectives add time_m dummy") +commands.exec("scoreboard objectives add time_s dummy") +commands.exec("scoreboard objectives add time_formatted dummy") +commands.exec("scoreboard objectives add time_display dummy") + +-- Display on the sidebar +commands.exec("scoreboard objectives setdisplay sidebar time_display") + +-- Main loop to update the time continuously +while true do + -- Calculate seconds + commands.exec("execute as @a run scoreboard players operation @s time_s = @s time_t") + commands.exec("execute as @a run scoreboard players operation @s time_s %= " .. tick_per_minute .. " const") + commands.exec("execute as @a run scoreboard players operation @s time_s /= " .. tick_per_second .. " const") + + -- Calculate minutes + commands.exec("execute as @a run scoreboard players operation @s time_m = @s time_t") + commands.exec("execute as @a run scoreboard players operation @s time_m /= " .. tick_per_minute .. " const") + commands.exec("execute as @a run scoreboard players operation @s time_m %= 60 const") + + -- Calculate hours + commands.exec("execute as @a run scoreboard players operation @s time_h = @s time_t") + commands.exec("execute as @a run scoreboard players operation @s time_h /= " .. tick_per_hour .. " const") + + -- Combine into HHMMSS format + commands.exec("execute as @a run scoreboard players set @s time_formatted 0") + commands.exec("execute as @a run scoreboard players operation @s time_formatted += @s time_h") + commands.exec("execute as @a run scoreboard players operation @s time_formatted *= 100 const") + commands.exec("execute as @a run scoreboard players operation @s time_formatted += @s time_m") + commands.exec("execute as @a run scoreboard players operation @s time_formatted *= 100 const") + commands.exec("execute as @a run scoreboard players operation @s time_formatted += @s time_s") + + -- Play a sound if the hour has changed + commands.exec('execute as @a unless score @s time_h = @s prev_time_h run playsound minecraft:entity.experience_orb.pickup master @s ~ ~ ~') + commands.exec('execute as @a unless score @s time_h = @s prev_time_h run scoreboard players operation @s prev_time_h = @s time_h') + + -- Copy the formatted time to the display objective + commands.exec("execute as @a run scoreboard players operation @s time_display = @s time_formatted") + + -- Display on the actionbar + commands.exec('execute as @a run title @s actionbar [{"text":"Time: "},{"score":{"name":"@s","objective":"time_h"},"color":"gold"},{"text":":"},{"score":{"name":"@s","objective":"time_m"},"color":"yellow"},{"text":":"},{"score":{"name":"@s","objective":"time_s"},"color":"green"}]') + + -- Delay to prevent excessive updates + os.sleep(0.1) +end