How It Works
The datapack consists of two functions, load
and tick
, which are executed when the datapack is loaded and every tick respectively.
The general idea is to increment the playtime of every player by 1 tick every tick and then check if the playtime has reached a certain value to increment the seconds, minutes, hours, and days scoreboards accordingly.
Layout
-
-
-
-
- load.json
- tick.json
-
-
-
-
- load.mcfunction
- tick.mcfunction
-
-
- pack.mcmeta
- pack.png
On Load
Once the datapack gets loaded, it creates multiple internal scoreboards to keep track of the player’s playtime in ticks, seconds, minutes, hours, and days.
scoreboard objectives add playtime_ticks dummy "Playtime (Ticks)"
scoreboard objectives add playtime_seconds dummy "Playtime (Seconds)"
scoreboard objectives add playtime_minutes dummy "Playtime (Minutes)"
scoreboard objectives add playtime_hours dummy "Playtime (Hours)"
scoreboard objectives add playtime_days dummy "Playtime (Days)"
On Tick
It then increments the playtime_ticks
scoreboard by 1 for every player every tick.
scoreboard players add @a playtime_ticks 1
The datapack then checks for every player if the playtime_ticks
scoreboard has reached 20 as any Minecraft server runs at 20 ticks per second if no lag is present.
Once it detects that the scoreboard has reached 20 increments the playtime_seconds
scoreboard by 1.
The same process is repeated for the playtime_seconds
, playtime_minutes
, and playtime_hours
scoreboards, when they reach 60, 60, and 24 respectively.
execute as @a if score @s playtime_ticks matches 20.. run scoreboard players add @s playtime_seconds 1
execute as @a if score @s playtime_seconds matches 60.. run scoreboard players add @s playtime_minutes 1
execute as @a if score @s playtime_minutes matches 60.. run scoreboard players add @s playtime_hours 1
execute as @a if score @s playtime_hours matches 24.. run scoreboard players add @s playtime_days 1
For every scoreboard entry that has reached its target value, the datapack subtracts the target value from the total.
This allows for “overloading” the scoreboards, meaning that the playtime_ticks
scoreboard can be set to values higher than 20 which in turn allows preserving playtime that has been accumulated before the datapack was installed.
execute as @a if score @s playtime_ticks matches 20.. run scoreboard players remove @s playtime_ticks 20
execute as @a if score @s playtime_seconds matches 60.. run scoreboard players remove @s playtime_seconds 60
execute as @a if score @s playtime_minutes matches 60.. run scoreboard players remove @s playtime_minutes 60
execute as @a if score @s playtime_hours matches 24.. run scoreboard players remove @s playtime_hours 24
It then displays the playtime in the player’s actionbar if they have the playtime
trigger enabled.
## Actionbar
scoreboard players enable @a playtime
execute as @a[scores={playtime=1}] run title @s actionbar ["",{"text":"[","bold":true,"color":"#B517AB"},{"score":{"name":"@s","objective":"playtime_days"},"bold":true,"color":"#C71585"},{"text":"d | ","bold":true,"color":"#D02090"},{"score":{"name":"@s","objective":"playtime_hours"},"bold":true,"color":"#D87093"},{"text":"h | ","bold":true,"color":"#DB7093"},{"score":{"name":"@s","objective":"playtime_minutes"},"bold":true,"color":"#D87093"},{"text":"m | ","bold":true,"color":"#D02090"},{"score":{"name":"@s","objective":"playtime_seconds"},"bold":true,"color":"#C71585"},{"text":"s]","bold":true,"color":"#B517AB"}]
execute as @a[scores={playtime=2..}] run scoreboard players set @s playtime 0