Add zeros to minutes and hours too in Clock.kt

This commit is contained in:
Username404 2021-06-18 21:08:00 +02:00
parent 104f1631e5
commit 59370fab4c
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
1 changed files with 11 additions and 8 deletions

View File

@ -3,23 +3,26 @@ package fr.username404.snowygui.gui.feature
import com.mojang.blaze3d.vertex.PoseStack
import fr.username404.snowygui.Snowy.Companion.onEvent
import fr.username404.snowygui.gui.FontUtil
import kotlinx.datetime.Clock as DatetimeClock
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import kotlinx.datetime.Clock as DatetimeClock
@ButtonInfo(Category.MISC)
object Clock: ButtonImpl() {
private val currentTimezone = TimeZone.currentSystemDefault()
override fun toString(): String = DatetimeClock.System.now().toLocalDateTime(currentTimezone).run {
arrayOf(hour, minute, second).map {
(it.takeIf { (it >= 10) } ?: "0$it").toString()
}.reduce { previous, current -> "$previous:$current"}
}
init {
onEvent("HudRender") {
if (toggled) {
with(DatetimeClock.System.now().toLocalDateTime(currentTimezone)) {
FontUtil.drawScaled(it.first() as PoseStack,
"$hour:$minute:${second.let { if (it >= 10) it else "0$it" }}",
5.0, 5.0,
0.85F, color = Colors.GOLD
)
}
FontUtil.drawScaled(it.first() as PoseStack,
this@Clock.toString(),
5.0, 5.0,
0.85F, color = Colors.GOLD
)
}
}
}