Skip to content

Commit 6d947d0

Browse files
authored
Add speed Hud element (#164)
# Pull Request Guidelines ### This is a template, modify before submitting your PR ### Description Adds a HUD element that displays the current players speed. Can configure to measure in different speed units. Ensure that your title is concise and has all the necessary information
1 parent d3f2642 commit 6d947d0

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.util
19+
20+
enum class SpeedUnit(override val displayName: String, val conversionFromBlocksPerTick: Float, val unitName: String) : NamedEnum {
21+
BlocksPerTick("Blocks Per Tick",1.0F, "bpt"),
22+
BlocksPerSecond("Blocks Per Second", 0.05F, "bps"),
23+
MetersPerSecond("Meters Per Second", 0.05F, "ms"),
24+
KilometersPerHour("Kilometers Per Hour", 0.277778F * 0.05F, "kmh"),
25+
MilesPerHour("Miles Per Hour", 0.44704F * 0.05F, "mph"),
26+
Boeing787AtTakeoffSpeed("Boeing 787 Takeoff Speed", 84.9F * 0.05F, "Boeing 787s");
27+
28+
/**
29+
* Converts the given speed in blocks per tick to the unit of this SpeedUnit.
30+
* @param speedInBlocksPerTick
31+
* @return
32+
*/
33+
fun convertFromMinecraft(speedInBlocksPerTick: Double): Double {
34+
return speedInBlocksPerTick / conversionFromBlocksPerTick
35+
}
36+
}

0 commit comments

Comments
 (0)