From 83dfec4a05d944253a61dd7c4dd552e0521ced7c Mon Sep 17 00:00:00 2001 From: rfresh2 <89827146+rfresh2@users.noreply.github.com> Date: Thu, 13 Jul 2023 01:37:42 -0700 Subject: [PATCH 01/11] OnlineTime HUD element --- .../gui/hudgui/elements/misc/OnlineTime.kt | 18 +++++++++++++++ .../manager/managers/OnlineTimeManager.kt | 22 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/OnlineTime.kt create mode 100644 src/main/kotlin/com/lambda/client/manager/managers/OnlineTimeManager.kt diff --git a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/OnlineTime.kt b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/OnlineTime.kt new file mode 100644 index 000000000..1280acbaa --- /dev/null +++ b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/OnlineTime.kt @@ -0,0 +1,18 @@ +package com.lambda.client.gui.hudgui.elements.misc + +import com.lambda.client.event.SafeClientEvent +import com.lambda.client.gui.hudgui.LabelHud +import com.lambda.client.manager.managers.OnlineTimeManager + +internal object OnlineTime: LabelHud( + name = "OnlineTime", + category = Category.MISC, + description = "Displays how long you have been online" +) { + override fun SafeClientEvent.updateText() { + OnlineTimeManager.getOnlineTime()?.let { onlineTime -> + displayText.add("Online:", secondaryColor) + displayText.add(onlineTime.toString(), primaryColor) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/lambda/client/manager/managers/OnlineTimeManager.kt b/src/main/kotlin/com/lambda/client/manager/managers/OnlineTimeManager.kt new file mode 100644 index 000000000..9fb34fd68 --- /dev/null +++ b/src/main/kotlin/com/lambda/client/manager/managers/OnlineTimeManager.kt @@ -0,0 +1,22 @@ +package com.lambda.client.manager.managers + +import com.lambda.client.event.events.ConnectionEvent +import com.lambda.client.event.listener.listener +import com.lambda.client.manager.Manager +import java.time.Duration +import java.time.Instant + +object OnlineTimeManager: Manager { + + private var connectTime: Instant = Instant.EPOCH + + init { + listener { + connectTime = Instant.now() + } + } + + fun getOnlineTime(): Duration { + return Duration.between(connectTime, Instant.now()) + } +} \ No newline at end of file From 408a53965d77d26494125cbb03a7380ac391555b Mon Sep 17 00:00:00 2001 From: rfresh2 <89827146+rfresh2@users.noreply.github.com> Date: Thu, 13 Jul 2023 01:40:01 -0700 Subject: [PATCH 02/11] Add cutoff leading 0 setting to time hud --- .../kotlin/com/lambda/client/gui/hudgui/elements/misc/Time.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/Time.kt b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/Time.kt index 3ee127185..b08dec35e 100644 --- a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/Time.kt +++ b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/Time.kt @@ -15,6 +15,7 @@ internal object Time : LabelHud( private val showTime = setting("Show Time", true) private val dateFormat = setting("Date Format", TimeUtils.DateFormat.DDMMYY, { showDate.value }) private val timeFormat = setting("Time Format", TimeUtils.TimeFormat.HHMM, { showTime.value }) + private val cutoffTimeLeadingZero = setting("Cutoff Time Leading Zero", true, { showTime.value }) private val timeUnit = setting("Time Unit", TimeUtils.TimeUnit.H12, { showTime.value }) override fun SafeClientEvent.updateText() { @@ -24,7 +25,8 @@ internal object Time : LabelHud( displayText.addLine("") } if (showTime.value) { - val time = TimeUtils.getTime(timeFormat.value, timeUnit.value) + var time = TimeUtils.getTime(timeFormat.value, timeUnit.value) + if (cutoffTimeLeadingZero.value && time.isNotEmpty() && time[0] == '0') time = time.removeRange(0, 1) time.forEach { displayText.add(it.toString(), if (it.isDigit()) primaryColor else secondaryColor) } } } From 7b2a801a2b9e983e22bb82a006d2b0a5a9335cb6 Mon Sep 17 00:00:00 2001 From: rfresh2 <89827146+rfresh2@users.noreply.github.com> Date: Thu, 13 Jul 2023 01:51:47 -0700 Subject: [PATCH 03/11] Additional formatting settings for Coordinates HUD --- .../gui/hudgui/elements/world/Coordinates.kt | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/world/Coordinates.kt b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/world/Coordinates.kt index 7031a56c1..b6420b313 100644 --- a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/world/Coordinates.kt +++ b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/world/Coordinates.kt @@ -15,7 +15,10 @@ internal object Coordinates : LabelHud( private val showX by setting("Show X", true) private val showY by setting("Show Y", true) private val showZ by setting("Show Z", true) + private val showXYZText by setting("Show XYZ Text", true) private val showNetherOverworld by setting("Show Nether/Overworld", true) + private val printDimensionName by setting("Print Dimension Name", false) + private val showNetherOverworldMultiline by setting("Show Nether/Overworld Multiline", false, { showNetherOverworld }) private val decimalPlaces by setting("Decimal Places", 1, 0..4, 1) private val thousandsSeparator by setting("Thousands Separator", false) @@ -24,42 +27,48 @@ internal object Coordinates : LabelHud( override fun SafeClientEvent.updateText() { val entity = mc.renderViewEntity ?: player - - displayText.add("XYZ", secondaryColor) - displayText.addLine(getFormattedCoords(entity.positionVector)) - + if (showXYZText) { + displayText.add("XYZ", secondaryColor) + } + if (showNetherOverworldMultiline) + displayText.addLine(getFormattedCoords(entity.positionVector)) + else + displayText.add(getFormattedCoords(entity.positionVector)) if (showNetherOverworld) { when (entity.dimension) { -1 -> { // Nether - displayText.add("Overworld", secondaryColor) - displayText.addLine(getFormattedCoords(entity.positionVector * netherToOverworld)) + if (printDimensionName) displayText.add("Nether", secondaryColor) + displayText.add(getFormattedCoords(entity.positionVector * netherToOverworld, true)) } 0 -> { // Overworld - displayText.add("Nether", secondaryColor) - displayText.addLine(getFormattedCoords(entity.positionVector * overworldToNether)) + if (printDimensionName) + displayText.add("Overworld", secondaryColor) + displayText.add(getFormattedCoords(entity.positionVector * overworldToNether, true)) } } } } - private fun getFormattedCoords(pos: Vec3d): TextComponent.TextElement { + private fun getFormattedCoords(pos: Vec3d, brackets: Boolean = false): TextComponent.TextElement { + if (!showX && !showY && !showZ) return TextComponent.TextElement("", primaryColor) val x = roundOrInt(pos.x) val y = roundOrInt(pos.y) val z = roundOrInt(pos.z) return StringBuilder().run { + if (brackets) append("[") if (showX) append(x) if (showY) appendWithComma(y) if (showZ) appendWithComma(z) + if (brackets) append("]") TextComponent.TextElement(toString(), primaryColor) } } private fun roundOrInt(input: Double): String { val separatorFormat = if (thousandsSeparator) "," else "" - return "%$separatorFormat.${decimalPlaces}f".format(input) } - private fun StringBuilder.appendWithComma(string: String) = append(if (length > 0) ", $string" else string) + private fun StringBuilder.appendWithComma(string: String) = append(if (isNotEmpty()) ", $string" else string) } \ No newline at end of file From be63b87a6a22f7f99e07789cf73057587dfed643 Mon Sep 17 00:00:00 2001 From: rfresh2 <89827146+rfresh2@users.noreply.github.com> Date: Thu, 13 Jul 2023 01:55:43 -0700 Subject: [PATCH 04/11] merge Direction and Rotation elements --- .../gui/hudgui/elements/player/Direction.kt | 20 ----------- .../gui/hudgui/elements/player/Rotation.kt | 33 +++++++++++++++---- 2 files changed, 26 insertions(+), 27 deletions(-) delete mode 100644 src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Direction.kt diff --git a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Direction.kt b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Direction.kt deleted file mode 100644 index 4afd48324..000000000 --- a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Direction.kt +++ /dev/null @@ -1,20 +0,0 @@ -package com.lambda.client.gui.hudgui.elements.player - -import com.lambda.client.event.SafeClientEvent -import com.lambda.client.gui.hudgui.LabelHud -import com.lambda.client.util.math.Direction - -internal object Direction : LabelHud( - name = "Direction", - category = Category.PLAYER, - description = "Direction of player facing to" -) { - - override fun SafeClientEvent.updateText() { - val entity = mc.renderViewEntity ?: player - val direction = Direction.fromEntity(entity) - displayText.add(direction.displayName, secondaryColor) - displayText.add("(${direction.displayNameXY})", primaryColor) - } - -} \ No newline at end of file diff --git a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Rotation.kt b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Rotation.kt index 8082a9d49..fa6798550 100644 --- a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Rotation.kt +++ b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Rotation.kt @@ -3,6 +3,7 @@ package com.lambda.client.gui.hudgui.elements.player import com.lambda.client.commons.utils.MathUtils import com.lambda.client.event.SafeClientEvent import com.lambda.client.gui.hudgui.LabelHud +import com.lambda.client.util.math.Direction import com.lambda.client.util.math.RotationUtils internal object Rotation : LabelHud( @@ -10,15 +11,33 @@ internal object Rotation : LabelHud( category = Category.PLAYER, description = "Player rotation" ) { + private val yaw by setting("Yaw", true) + private val pitch by setting("Pitch", true) + private val direction by setting("Direction", true) + private val directionDisplayName by setting("Direction Name", true, visibility = { direction }) + private val directionXZ by setting("Direction XZ", true, visibility = { direction }) override fun SafeClientEvent.updateText() { - val yaw = MathUtils.round(RotationUtils.normalizeAngle(mc.player?.rotationYaw ?: 0.0f), 1) - val pitch = MathUtils.round(mc.player?.rotationPitch ?: 0.0f, 1) - - displayText.add("Yaw", secondaryColor) - displayText.add(yaw.toString(), primaryColor) - displayText.add("Pitch", secondaryColor) - displayText.add(pitch.toString(), primaryColor) + if (yaw) { + val yawVal = MathUtils.round(RotationUtils.normalizeAngle(mc.player?.rotationYaw ?: 0.0f), 1) + displayText.add("Yaw", secondaryColor) + displayText.add(yawVal.toString(), primaryColor) + } + if (pitch) { + val pitchVal = MathUtils.round(mc.player?.rotationPitch ?: 0.0f, 1) + displayText.add("Pitch", secondaryColor) + displayText.add(pitchVal.toString(), primaryColor) + } + if (direction) { + val entity = mc.renderViewEntity ?: player + val direction = Direction.fromEntity(entity) + if (directionDisplayName) { + displayText.add(direction.displayName, secondaryColor) + } + if (directionXZ) { + displayText.add("(${direction.displayNameXY})", primaryColor) + } + } } } \ No newline at end of file From 9ea4c0432d0e4dbef8c669fe6e4853017b1a7448 Mon Sep 17 00:00:00 2001 From: rfresh2 <89827146+rfresh2@users.noreply.github.com> Date: Thu, 13 Jul 2023 01:59:19 -0700 Subject: [PATCH 05/11] Radar rotation setting --- .../com/lambda/client/gui/hudgui/elements/world/Radar.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/world/Radar.kt b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/world/Radar.kt index 08bb4f8b6..f142a04a5 100644 --- a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/world/Radar.kt +++ b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/world/Radar.kt @@ -35,6 +35,7 @@ internal object Radar : HudElement( private val neutral = setting("Neutral Mobs", true) private val hostile = setting("Hostile Mobs", true) private val invisible = setting("Invisible Entities", true) + private val rotation by setting("Rotation", true) override val hudWidth: Float = 130.0f override val hudHeight: Float = 130.0f @@ -56,7 +57,7 @@ internal object Radar : HudElement( glTranslated(radius.toDouble(), radius.toDouble(), 0.0) drawCircleFilled(vertexHelper, radius = radius.toDouble(), color = GuiColors.backGround) drawCircleOutline(vertexHelper, radius = radius.toDouble(), lineWidth = 1.8f, color = primaryColor) - glRotatef(player.rotationYaw + 180, 0f, 0f, -1f) + if (rotation) glRotatef(player.rotationYaw + 180, 0f, 0f, -1f) } private fun SafeClientEvent.drawEntities(vertexHelper: VertexHelper) { From e8500fae0c7b2874afca3369db33e191865d8e33 Mon Sep 17 00:00:00 2001 From: rfresh2 <89827146+rfresh2@users.noreply.github.com> Date: Thu, 13 Jul 2023 02:02:03 -0700 Subject: [PATCH 06/11] Setting to configure label HUD element text shadow --- .../kotlin/com/lambda/client/gui/hudgui/AbstractLabelHud.kt | 5 +++-- .../kotlin/com/lambda/client/module/modules/client/Hud.kt | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/lambda/client/gui/hudgui/AbstractLabelHud.kt b/src/main/kotlin/com/lambda/client/gui/hudgui/AbstractLabelHud.kt index c3659ea21..d6dda1fb9 100644 --- a/src/main/kotlin/com/lambda/client/gui/hudgui/AbstractLabelHud.kt +++ b/src/main/kotlin/com/lambda/client/gui/hudgui/AbstractLabelHud.kt @@ -2,6 +2,7 @@ package com.lambda.client.gui.hudgui import com.lambda.client.commons.interfaces.Nameable import com.lambda.client.event.SafeClientEvent +import com.lambda.client.module.modules.client.Hud import com.lambda.client.module.modules.client.HudEditor import com.lambda.client.setting.configs.AbstractConfig import com.lambda.client.util.graphics.VertexHelper @@ -23,7 +24,6 @@ abstract class AbstractLabelHud( override val hudWidth: Float get() = displayText.getWidth() + 2.0f override val hudHeight: Float get() = displayText.getHeight(2) - protected val displayText = TextComponent(separator) init { @@ -47,7 +47,8 @@ abstract class AbstractLabelHud( displayText.draw( Vec2d(textPosX.toDouble(), textPosY.toDouble()), horizontalAlign = dockingH, - verticalAlign = dockingV + verticalAlign = dockingV, + drawShadow = Hud.textShadow ) } diff --git a/src/main/kotlin/com/lambda/client/module/modules/client/Hud.kt b/src/main/kotlin/com/lambda/client/module/modules/client/Hud.kt index 8887db8ce..260116744 100644 --- a/src/main/kotlin/com/lambda/client/module/modules/client/Hud.kt +++ b/src/main/kotlin/com/lambda/client/module/modules/client/Hud.kt @@ -14,6 +14,7 @@ object Hud : Module( val hudFrame by setting("Hud Frame", false) val primaryColor by setting("Primary Color", ColorHolder(255, 240, 246), false) val secondaryColor by setting("Secondary Color", ColorHolder(108, 0, 43), false) + val textShadow by setting("Text Shadow", true) val chatSnap by setting("Chat Snap", true) val collisionSnapping by setting("Collision Snapping", true) } \ No newline at end of file From 97162ded82e1b60140222a9c183ceeb3dacee89f Mon Sep 17 00:00:00 2001 From: rfresh2 <89827146+rfresh2@users.noreply.github.com> Date: Fri, 14 Jul 2023 17:16:36 -0700 Subject: [PATCH 07/11] coordinates freecam compat --- .../com/lambda/client/gui/hudgui/elements/world/Coordinates.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/world/Coordinates.kt b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/world/Coordinates.kt index b6420b313..3df781af3 100644 --- a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/world/Coordinates.kt +++ b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/world/Coordinates.kt @@ -35,7 +35,7 @@ internal object Coordinates : LabelHud( else displayText.add(getFormattedCoords(entity.positionVector)) if (showNetherOverworld) { - when (entity.dimension) { + when (world.provider.dimension) { -1 -> { // Nether if (printDimensionName) displayText.add("Nether", secondaryColor) displayText.add(getFormattedCoords(entity.positionVector * netherToOverworld, true)) From e3974049480d4992d7c01664389169e018af36ac Mon Sep 17 00:00:00 2001 From: rfresh2 <89827146+rfresh2@users.noreply.github.com> Date: Fri, 14 Jul 2023 17:27:02 -0700 Subject: [PATCH 08/11] re-split direction/rotation elements --- .../gui/hudgui/elements/player/Direction.kt | 28 +++++++++++++++++++ .../gui/hudgui/elements/player/Rotation.kt | 14 ---------- 2 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Direction.kt diff --git a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Direction.kt b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Direction.kt new file mode 100644 index 000000000..1516d8764 --- /dev/null +++ b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Direction.kt @@ -0,0 +1,28 @@ +package com.lambda.client.gui.hudgui.elements.player + +import com.lambda.client.commons.interfaces.DisplayEnum +import com.lambda.client.event.SafeClientEvent +import com.lambda.client.gui.hudgui.LabelHud +import com.lambda.client.util.math.Direction + +internal object Direction : LabelHud( + name = "Direction", + category = Category.PLAYER, + description = "Displays the direction you are facing" +) { + private val directionDisplayMode by setting("Direction Format", DirectionFormat.XZ) + + enum class DirectionFormat(override val displayName: String): DisplayEnum { + NAME("Name"), XZ("XZ"), BOTH("Both") + } + + override fun SafeClientEvent.updateText() { + val entity = mc.renderViewEntity ?: player + val direction = Direction.fromEntity(entity) + if (directionDisplayMode == DirectionFormat.NAME || directionDisplayMode == DirectionFormat.BOTH) + displayText.add(direction.displayName, secondaryColor) + if (directionDisplayMode == DirectionFormat.XZ || directionDisplayMode == DirectionFormat.BOTH) + displayText.add("(${direction.displayNameXY})", primaryColor) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Rotation.kt b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Rotation.kt index fa6798550..0585246f8 100644 --- a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Rotation.kt +++ b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/player/Rotation.kt @@ -3,7 +3,6 @@ package com.lambda.client.gui.hudgui.elements.player import com.lambda.client.commons.utils.MathUtils import com.lambda.client.event.SafeClientEvent import com.lambda.client.gui.hudgui.LabelHud -import com.lambda.client.util.math.Direction import com.lambda.client.util.math.RotationUtils internal object Rotation : LabelHud( @@ -13,9 +12,6 @@ internal object Rotation : LabelHud( ) { private val yaw by setting("Yaw", true) private val pitch by setting("Pitch", true) - private val direction by setting("Direction", true) - private val directionDisplayName by setting("Direction Name", true, visibility = { direction }) - private val directionXZ by setting("Direction XZ", true, visibility = { direction }) override fun SafeClientEvent.updateText() { if (yaw) { @@ -28,16 +24,6 @@ internal object Rotation : LabelHud( displayText.add("Pitch", secondaryColor) displayText.add(pitchVal.toString(), primaryColor) } - if (direction) { - val entity = mc.renderViewEntity ?: player - val direction = Direction.fromEntity(entity) - if (directionDisplayName) { - displayText.add(direction.displayName, secondaryColor) - } - if (directionXZ) { - displayText.add("(${direction.displayNameXY})", primaryColor) - } - } } } \ No newline at end of file From 53d2590873fe80b18a719ae73756f2227db76fc9 Mon Sep 17 00:00:00 2001 From: rfresh2 <89827146+rfresh2@users.noreply.github.com> Date: Fri, 14 Jul 2023 17:50:20 -0700 Subject: [PATCH 09/11] fix HUD element dragging after defaulting settings --- .../com/lambda/client/gui/hudgui/AbstractHudElement.kt | 5 ++++- .../kotlin/com/lambda/client/gui/rgui/WindowComponent.kt | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/lambda/client/gui/hudgui/AbstractHudElement.kt b/src/main/kotlin/com/lambda/client/gui/hudgui/AbstractHudElement.kt index c65b48fe4..0a832ce27 100644 --- a/src/main/kotlin/com/lambda/client/gui/hudgui/AbstractHudElement.kt +++ b/src/main/kotlin/com/lambda/client/gui/hudgui/AbstractHudElement.kt @@ -159,7 +159,10 @@ abstract class AbstractHudElement( default.valueListeners.add { _, it -> if (it) { - settingList.filter { it != visibleSetting && it != default }.forEach { it.resetValue() } + settingList.filter { it != visibleSetting && it != default }.forEach { + it.resetValue() + updatePreDrag(null) + } default.value = false MessageSendHelper.sendChatMessage("$name Set to defaults!") } diff --git a/src/main/kotlin/com/lambda/client/gui/rgui/WindowComponent.kt b/src/main/kotlin/com/lambda/client/gui/rgui/WindowComponent.kt index 951a9f3e9..f2d10d173 100644 --- a/src/main/kotlin/com/lambda/client/gui/rgui/WindowComponent.kt +++ b/src/main/kotlin/com/lambda/client/gui/rgui/WindowComponent.kt @@ -89,7 +89,7 @@ open class WindowComponent( if (mouseState != MouseState.DRAG) updatePreDrag(mousePos.minus(posX, posY)) } - private fun updatePreDrag(mousePos: Vec2f?) { + protected fun updatePreDrag(mousePos: Vec2f?) { mousePos?.let { preDragMousePos = it } preDragPos = Vec2f(posX, posY) preDragSize = Vec2f(width, height) From 30ced5d045f7c43f6f9c76356a6d64c979182a63 Mon Sep 17 00:00:00 2001 From: rfresh2 <89827146+rfresh2@users.noreply.github.com> Date: Fri, 14 Jul 2023 18:11:36 -0700 Subject: [PATCH 10/11] onlinetime formatting --- .../gui/hudgui/elements/misc/OnlineTime.kt | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/OnlineTime.kt b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/OnlineTime.kt index 1280acbaa..df34f41bc 100644 --- a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/OnlineTime.kt +++ b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/OnlineTime.kt @@ -1,8 +1,10 @@ package com.lambda.client.gui.hudgui.elements.misc +import com.lambda.client.commons.utils.grammar import com.lambda.client.event.SafeClientEvent import com.lambda.client.gui.hudgui.LabelHud import com.lambda.client.manager.managers.OnlineTimeManager +import java.time.Duration internal object OnlineTime: LabelHud( name = "OnlineTime", @@ -10,9 +12,23 @@ internal object OnlineTime: LabelHud( description = "Displays how long you have been online" ) { override fun SafeClientEvent.updateText() { - OnlineTimeManager.getOnlineTime()?.let { onlineTime -> - displayText.add("Online:", secondaryColor) - displayText.add(onlineTime.toString(), primaryColor) + val onlineTime = OnlineTimeManager.getOnlineTime() + displayText.add("Online:", secondaryColor) + displayText.add(formatDuration(onlineTime), primaryColor) + } + + // avoiding kotlin.time.Duration.toString() because it shows down to milliseconds + private fun formatDuration(duration: Duration): String { + val secondsInMinute = 60L + val secondsInHour = secondsInMinute * 60L + val seconds = duration.seconds % secondsInMinute + val minutes = duration.seconds / secondsInMinute % secondsInMinute + val hours = duration.seconds / secondsInHour % 24L + return buildString { + if (hours > 0) append(grammar(hours.toInt(), "hour", "hours") + ", ") + if (minutes > 0) append(grammar(minutes.toInt(), "minute", "minutes") + ", ") + append(grammar(seconds.toInt(), "second", "seconds")) } } + } \ No newline at end of file From 9460fbf8ea86e56a28c275ccbbeea33dc0b035f8 Mon Sep 17 00:00:00 2001 From: Constructor Date: Sat, 15 Jul 2023 21:16:45 +0200 Subject: [PATCH 11/11] Using Kotlin std lib --- .../gui/hudgui/elements/misc/OnlineTime.kt | 24 ++++--------------- .../manager/managers/OnlineTimeManager.kt | 10 ++++---- 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/OnlineTime.kt b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/OnlineTime.kt index df34f41bc..16018eaed 100644 --- a/src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/OnlineTime.kt +++ b/src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/OnlineTime.kt @@ -1,10 +1,11 @@ package com.lambda.client.gui.hudgui.elements.misc -import com.lambda.client.commons.utils.grammar import com.lambda.client.event.SafeClientEvent import com.lambda.client.gui.hudgui.LabelHud import com.lambda.client.manager.managers.OnlineTimeManager -import java.time.Duration +import kotlin.math.roundToInt +import kotlin.time.DurationUnit +import kotlin.time.toDuration internal object OnlineTime: LabelHud( name = "OnlineTime", @@ -12,23 +13,8 @@ internal object OnlineTime: LabelHud( description = "Displays how long you have been online" ) { override fun SafeClientEvent.updateText() { - val onlineTime = OnlineTimeManager.getOnlineTime() + val onlineTime = OnlineTimeManager.getOnlineTime().toDouble(DurationUnit.SECONDS).roundToInt() displayText.add("Online:", secondaryColor) - displayText.add(formatDuration(onlineTime), primaryColor) + displayText.add(onlineTime.toDuration(DurationUnit.SECONDS).toString(), primaryColor) } - - // avoiding kotlin.time.Duration.toString() because it shows down to milliseconds - private fun formatDuration(duration: Duration): String { - val secondsInMinute = 60L - val secondsInHour = secondsInMinute * 60L - val seconds = duration.seconds % secondsInMinute - val minutes = duration.seconds / secondsInMinute % secondsInMinute - val hours = duration.seconds / secondsInHour % 24L - return buildString { - if (hours > 0) append(grammar(hours.toInt(), "hour", "hours") + ", ") - if (minutes > 0) append(grammar(minutes.toInt(), "minute", "minutes") + ", ") - append(grammar(seconds.toInt(), "second", "seconds")) - } - } - } \ No newline at end of file diff --git a/src/main/kotlin/com/lambda/client/manager/managers/OnlineTimeManager.kt b/src/main/kotlin/com/lambda/client/manager/managers/OnlineTimeManager.kt index 9fb34fd68..ac5dac590 100644 --- a/src/main/kotlin/com/lambda/client/manager/managers/OnlineTimeManager.kt +++ b/src/main/kotlin/com/lambda/client/manager/managers/OnlineTimeManager.kt @@ -3,20 +3,20 @@ package com.lambda.client.manager.managers import com.lambda.client.event.events.ConnectionEvent import com.lambda.client.event.listener.listener import com.lambda.client.manager.Manager -import java.time.Duration -import java.time.Instant +import kotlin.time.Duration +import kotlin.time.TimeSource object OnlineTimeManager: Manager { - private var connectTime: Instant = Instant.EPOCH + private var connectTime = TimeSource.Monotonic.markNow() init { listener { - connectTime = Instant.now() + connectTime = TimeSource.Monotonic.markNow() } } fun getOnlineTime(): Duration { - return Duration.between(connectTime, Instant.now()) + return connectTime.elapsedNow() } } \ No newline at end of file