1+ /*
2+ * Copyright 2025 Lambda
3+ *
4+ * This program is free software: you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation, either version 3 of the License, or
7+ * (at your option) any later version.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16+ */
17+
18+ package com.lambda.module.hud
19+
20+ import com.lambda.event.events.TickEvent
21+ import com.lambda.event.listener.SafeListener.Companion.listen
22+ import com.lambda.gui.dsl.ImGuiBuilder
23+ import com.lambda.module.HudModule
24+ import com.lambda.module.tag.ModuleTag
25+ import com.lambda.threading.runSafe
26+ import com.lambda.util.SpeedUnit
27+ import net.minecraft.util.math.Vec3d
28+
29+ object Speed : HudModule(
30+ name = " Speed" ,
31+ description = " Displays player speed" ,
32+ tag = ModuleTag .HUD
33+ ) {
34+ var speedUnit by setting(" Speed Unit" , SpeedUnit .MetersPerSecond )
35+ var onlyHorizontal by setting(" Horizontal Speed" , false , description = " Only consider horizontal movement for speed calculation" )
36+
37+ var previousPos: Vec3d = Vec3d .ZERO
38+ var currentPos: Vec3d = Vec3d .ZERO
39+ var speed: Double = 0.0
40+
41+ init {
42+ listen<TickEvent .Post > {
43+ previousPos = currentPos
44+ currentPos = player.pos
45+
46+ var vecDelta = player.pos.subtract(previousPos)
47+ if (onlyHorizontal) {
48+ vecDelta = Vec3d (vecDelta.x, 0.0 , vecDelta.z)
49+ }
50+ speed = speedUnit.convertFromMinecraft(vecDelta.length())
51+ }
52+
53+ onEnable {
54+ previousPos = player.pos
55+ currentPos = player.pos
56+ speed = 0.0
57+ }
58+
59+ onDisable { speed = 0.0 }
60+ }
61+
62+ override fun ImGuiBuilder.buildLayout () {
63+ runSafe {
64+ text(" Speed: %.2f %s" .format(speed, speedUnit.unitName))
65+ }
66+ }
67+ }
0 commit comments