Debug module

Debug: Minecraft 26.3 cheat module, full source for every implementation (1 client: LiquidBounce (Kotlin)).

1 implementationRENDER

Clients with this module

Full module source: 1 implementation

Every implementation below is the complete module file, verbatim. Expand a client to read its code.

LiquidBounce (Kotlin)RENDERsrc/main/kotlin/net/ccbluex/liquidbounce/features/module/modules/render/ModuleDebug.ktKotlin · 389 lines · 12.6 KB

Only of interest to developers.

SettingsParametersGeometryExpiresTicksToPredict
ModuleDebug.ktKotlin · Minecraft 26.2 · 389 lines · 12.6 KB · complete file, verbatimOriginal ↗
1
/*
2
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
3
 *
4
 * Copyright (c) 2015 - 2026 CCBlueX
5
 *
6
 * LiquidBounce is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * LiquidBounce is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
18
 */
19
package net.ccbluex.liquidbounce.features.module.modules.render
20
21
import it.unimi.dsi.fastutil.objects.Object2ObjectRBTreeMap
22
import net.ccbluex.fastutil.forEachFloat
23
import net.ccbluex.fastutil.step
24
import net.ccbluex.liquidbounce.config.types.CurveValue.Axis.Companion.axis
25
import net.ccbluex.liquidbounce.config.types.group.ToggleableValueGroup
26
import net.ccbluex.liquidbounce.event.events.GameTickEvent
27
import net.ccbluex.liquidbounce.event.events.MovementInputEvent
28
import net.ccbluex.liquidbounce.event.events.OverlayRenderEvent
29
import net.ccbluex.liquidbounce.event.events.WorldRenderEvent
30
import net.ccbluex.liquidbounce.event.handler
31
import net.ccbluex.liquidbounce.features.misc.DebuggedOwner
32
import net.ccbluex.liquidbounce.features.module.ClientModule
33
import net.ccbluex.liquidbounce.features.module.ModuleCategories
34
import net.ccbluex.liquidbounce.render.FontManager
35
import net.ccbluex.liquidbounce.render.WorldRenderEnvironment
36
import net.ccbluex.liquidbounce.render.drawBox
37
import net.ccbluex.liquidbounce.render.drawLine
38
import net.ccbluex.liquidbounce.render.drawLineStrip
39
import net.ccbluex.liquidbounce.render.drawQuad
40
import net.ccbluex.liquidbounce.render.drawTriangle
41
import net.ccbluex.liquidbounce.render.engine.type.Color4b
42
import net.ccbluex.liquidbounce.render.renderEnvironment
43
import net.ccbluex.liquidbounce.render.utils.MutableVertexList
44
import net.ccbluex.liquidbounce.render.withPositionRelativeToCamera
45
import net.ccbluex.liquidbounce.utils.text.asPlainText
46
import net.ccbluex.liquidbounce.utils.text.textOf
47
import net.ccbluex.liquidbounce.utils.math.vector2f
48
import net.ccbluex.liquidbounce.utils.entity.PlayerSimulationCache
49
import net.ccbluex.liquidbounce.utils.kotlin.EventPriorityConvention.FIRST_PRIORITY
50
import net.ccbluex.liquidbounce.utils.math.geometry.Line
51
import net.ccbluex.liquidbounce.utils.math.toVec3f
52
import net.minecraft.ChatFormatting
53
import net.minecraft.network.chat.Component
54
import net.minecraft.world.phys.AABB
55
import net.minecraft.world.phys.Vec3
56
57
/**
58
 * Debug module
59
 *
60
 * Only of interest to developers.
61
 */
62
63
object ModuleDebug : ClientModule("Debug", ModuleCategories.RENDER) {
64
65
    private val parameters by boolean("Parameters", true).onChanged { _ ->
66
        debugParameters.clear()
67
    }
68
    private val geometry by boolean("Geometry", true).onChanged { _ ->
69
        debuggedGeometry.clear()
70
    }
71
72
    private val expireTime by int("Expires", 5, 1..30, "secs")
73
74
    private val fontRenderer
75
        get() = FontManager.FONT_RENDERER
76
77
    object RenderSimulatedPlayer : ToggleableValueGroup(this, "SimulatedPlayer", false) {
78
79
        private val ticksToPredict by int("TicksToPredict", 20, 5..100)
80
81
        @Suppress("unused")
82
        private val movementInputHandler = handler<MovementInputEvent> { _ ->
83
            PlayerSimulationCache.getSimulationForLocalPlayer().simulateUntil(this.ticksToPredict)
84
        }
85
86
        @Suppress("unused")
87
        private val renderHandler = handler<WorldRenderEvent> { event ->
88
            val cachedPositions = PlayerSimulationCache
89
                .getSimulationForLocalPlayer()
90
                .getSnapshotsBetween(0 until this.ticksToPredict)
91
92
            event.renderEnvironment {
93
                drawLineStrip(
94
                    Color4b.BLUE.argb,
95
                    positions = MutableVertexList(cachedPositions.size)
96
                        .addAllRelativeToCamera(cachedPositions, camera) { it.pos },
97
                )
98
            }
99
        }
100
101
    }
102
103
    object Graph : ToggleableValueGroup(this, "Graph", false) {
104
105
        private val curve = curve(
106
            "Curve", mutableListOf(
107
                0f vector2f 120f,
108
                50f vector2f 60f,
109
                140f vector2f 120f,
110
                180f vector2f 90f
111
            ),
112
            xAxis = "X Axis" axis 0f..180f,
113
            yAxis = "Y Axis" axis 40f..120f
114
        )
115
116
        @Suppress("unused")
117
        private val screenRenderHandler = handler<OverlayRenderEvent> { event ->
118
            val context = event.context
119
120
            with(context) {
121
                var posX = 300
122
                var posY = 500
123
124
                fontRenderer.draw("Graph".asPlainText()) {
125
                    x = posX.toFloat()
126
                    y = posY.toFloat()
127
                    shadow = true
128
                    scale = 0.3f
129
                }
130
131
                curve.xAxis.range.step(0.1f).forEachFloat { x ->
132
                    var y = curve.transform(x)
133
                    this.drawQuad(
134
                        posX + x,
135
                        posY - y,
136
                        posX + x + 1,
137
                        posY - y + 1,
138
                        Color4b.GREEN
139
                    )
140
                }
141
142
                val points = curve.get()
143
                for (point in curve.get()) {
144
                    var x = point[0]
145
                    var y = point[1]
146
147
                    this.drawQuad(
148
                        posX + x - 2,
149
                        posY - y - 2,
150
                        posX + x + 2,
151
                        posY - y + 2,
152
                        Color4b.WHITE
153
                    )
154
                }
155
            }
156
        }
157
158
    }
159
160
    init {
161
        tree(RenderSimulatedPlayer)
162
        tree(Graph)
163
    }
164
165
    @JvmRecord
166
    private data class DebuggedKey(val owner: DebuggedOwner, val name: String)
167
168
    private val KEY_COMPARATOR = compareBy<DebuggedKey> { it.owner.debugOwnerId }
169
        .thenComparing(DebuggedKey::name)
170
171
    @JvmRecord
172
    private data class ParameterCapture(val time: Long = System.currentTimeMillis(), val value: Any?)
173
174
    private val debugParameters = Object2ObjectRBTreeMap<DebuggedKey, ParameterCapture>(KEY_COMPARATOR)
175
176
    private val debuggedGeometry = Object2ObjectRBTreeMap<DebuggedKey, DebuggedGeometry>(KEY_COMPARATOR)
177
178
    @Suppress("unused")
179
    private val renderHandler = handler<WorldRenderEvent> { event ->
180
        if (!geometry) {
181
            return@handler
182
        }
183
184
        event.renderEnvironment {
185
            debuggedGeometry.values.forEach { geometry ->
186
                geometry.render()
187
            }
188
        }
189
    }
190
191
    @Suppress("unused")
192
    private val expireHandler = handler<GameTickEvent>(priority = FIRST_PRIORITY) {
193
        val earliest = System.currentTimeMillis() - expireTime * 1000
194
195
        debugParameters.entries.removeIf { (_, capture) ->
196
            capture.time <= earliest
197
        }
198
    }
199
200
    @Suppress("unused")
201
    private val screenRenderHandler = handler<OverlayRenderEvent> { event ->
202
        val context = event.context
203
204
        if (mc.options.keyPlayerList.isDown || !parameters) {
205
            return@handler
206
        }
207
208
        /**
209
         * Separate the debugged owner from its parameter
210
         * Structure should be like this:
211
         * Owner ->
212
         *   Parameter Name: Parameter Value
213
         *   Parameter Name: Parameter Value
214
         *   Parameter Name: Parameter Value
215
         */
216
        val textList = mutableListOf<Component>()
217
218
        val debuggedOwners = debugParameters.keys.groupBy { it.owner }
219
220
        val currentTime = System.currentTimeMillis()
221
222
        debuggedOwners.forEach { (owner, parameters) ->
223
            textList += owner.debugDisplayName
224
225
            for (debuggedParameter in parameters) {
226
                val parameterName = debuggedParameter.name
227
                val parameterCapture = debugParameters[debuggedParameter] ?: continue
228
                val duration = (currentTime - parameterCapture.time) / 1000
229
                textList += textOf(
230
                    "$parameterName: ".asPlainText(ChatFormatting.WHITE),
231
                    parameterCapture.value.toString().asPlainText(ChatFormatting.GREEN),
232
                    " [${duration}s ago]".asPlainText(ChatFormatting.GRAY),
233
                )
234
            }
235
        }
236
237
        with(event.context) {
238
            val vanillaScale = fontRenderer.scaleToVanillaFont
239
240
            // Draw
241
            fontRenderer.draw("Debugging".asPlainText()) {
242
                x = 120f
243
                y = 22f
244
                shadow = true
245
                scale = vanillaScale * 2
246
            }
247
248
            // Draw text line one by one
249
            textList.forEachIndexed { index, text ->
250
                fontRenderer.draw(text) {
251
                    x = 120f
252
                    y = 40 + ((fontRenderer.height * vanillaScale) * index)
253
                    shadow = true
254
                    scale = vanillaScale
255
                }
256
            }
257
        }
258
    }
259
260
    fun debugGeometry(owner: DebuggedOwner, name: String, geometry: DebuggedGeometry?) {
261
        // Do not take any new debugging while the module is off
262
        if (!running) {
263
            return
264
        }
265
266
        if (geometry != null) {
267
            debuggedGeometry[DebuggedKey(owner, name)] = geometry
268
        } else {
269
            debuggedGeometry.remove(DebuggedKey(owner, name))
270
        }
271
    }
272
273
    inline fun DebuggedOwner.debugGeometry(name: String, lazyGeometry: () -> DebuggedGeometry?) {
274
        if (!running) {
275
            return
276
        }
277
278
        debugGeometry(owner = this, name, lazyGeometry())
279
    }
280
281
    fun debugParameter(owner: DebuggedOwner, name: String, value: Any?) {
282
        if (!running) {
283
            return
284
        }
285
286
        debugParameters[DebuggedKey(owner, name)] = ParameterCapture(value = value)
287
    }
288
289
    inline fun DebuggedOwner.debugParameter(name: String, lazyValue: () -> Any?) {
290
        if (!running) {
291
            return
292
        }
293
294
        debugParameter(owner = this, name, lazyValue())
295
    }
296
297
    fun getArrayEntryColor(idx: Int, length: Int): Color4b {
298
        val hue = idx.toFloat() / length.toFloat()
299
        return Color4b.ofHSB(hue, 1f, 1f, alpha = 32f / 255f)
300
    }
301
302
    fun interface DebuggedGeometry {
303
        context(env: WorldRenderEnvironment)
304
        fun render()
305
    }
306
307
    class DebuggedLine(line: Line, val color: Color4b) : DebuggedGeometry {
308
        val from: Vec3
309
        val to: Vec3
310
311
        init {
312
            val normalizedDirection = line.direction.normalize()
313
314
            this.from = line.position.subtract(normalizedDirection.scale(100.0))
315
            this.to = line.position.add(normalizedDirection.scale(100.0))
316
        }
317
318
        context(env: WorldRenderEnvironment)
319
        override fun render() {
320
            env.withPositionRelativeToCamera {
321
                env.drawLine(
322
                    from,
323
                    to,
324
                    color.argb,
325
                )
326
            }
327
        }
328
    }
329
330
    class DebuggedTriangle(
331
        val p1: Vec3,
332
        val p2: Vec3,
333
        val p3: Vec3,
334
        val color: Color4b,
335
    ) : DebuggedGeometry {
336
        context(env: WorldRenderEnvironment)
337
        override fun render() {
338
            env.withPositionRelativeToCamera {
339
                env.drawTriangle(
340
                    p1 = p1.toVec3f(),
341
                    p2 = p2.toVec3f(),
342
                    p3 = p3.toVec3f(),
343
                    argb = color.argb,
344
                )
345
            }
346
        }
347
    }
348
349
    class DebuggedLineSegment(val from: Vec3, val to: Vec3, val color: Color4b) : DebuggedGeometry {
350
        context(env: WorldRenderEnvironment)
351
        override fun render() {
352
            env.withPositionRelativeToCamera {
353
                env.drawLine(
354
                    from,
355
                    to,
356
                    color.argb,
357
                )
358
            }
359
        }
360
    }
361
362
    open class DebuggedBox(val box: AABB, val color: Color4b) : DebuggedGeometry {
363
        context(env: WorldRenderEnvironment)
364
        override fun render() {
365
            env.drawBox(box.move(env.camera.position().reverse()), color)
366
        }
367
    }
368
369
    class DebuggedPoint(point: Vec3, color: Color4b, size: Double = 0.2) : DebuggedBox(
370
        AABB.ofSize(point, size, size, size),
371
        color
372
    )
373
374
    class DebugCollection(val geometry: Collection<DebuggedGeometry>) : DebuggedGeometry {
375
        context(env: WorldRenderEnvironment)
376
        override fun render() {
377
            this.geometry.forEach { it.render() }
378
        }
379
    }
380
381
    override fun onDisabled() {
382
        // Might clean up some memory if we disable the module
383
        debuggedGeometry.clear()
384
        debugParameters.clear()
385
        super.onDisabled()
386
    }
387
388
}
389