Backtrack module

Backtrack: Minecraft 26.3 cheat module, full source for every implementation (2 clients: Combatant, LiquidBounce (Kotlin)).

2 implementationsCOMBAT

Full module source: 2 implementations

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

CombatantCOMBATsrc/main/java/combatant/client/features/module/modules/combat/Backtrack.javaJava · 411 lines · 15.6 KB
SettingsbacktrackMinRangebacktrackMaxRangebacktrackMinDelaybacktrackMaxDelaybacktrackMinNextDelaybacktrackMaxNextDelaybacktrackTrackingBufferbacktrackChancebacktrackPauseOnHurtTimebacktrackHurtTimebacktrackTargetModebacktrackLastAttackTimebacktrackRenderbacktrackFillColorbacktrackLineColorbacktrackLineWidth
Backtrack.javaJava · Minecraft 26.2 · 411 lines · 15.6 KB · complete file, verbatimOriginal ↗
1
/*
2
 * This file is part of the Combatant Client distribution.
3
 * Combatant modifications copyright (c) 2026 pivosos2007.
4
 *
5
 * Portions of this file are based on LiquidBounce
6
 * (https://github.com/CCBlueX/LiquidBounce).
7
 * Copyright (c) 2015-2026 CCBlueX.
8
 *
9
 * LiquidBounce portions are licensed under GPLv3-or-later.
10
 * Combatant modifications are licensed under GPLv3.
11
 * See THIRD_PARTY_NOTICES.md for details.
12
 */
13
14
package combatant.client.features.module.modules.combat;
15
16
import net.minecraft.world.entity.Entity;
17
import net.minecraft.world.phys.AABB;
18
import net.minecraft.world.phys.Vec3;
19
import combatant.client.config.common.CommonSettingSchemas;
20
import combatant.client.config.values.BooleanValue;
21
import combatant.client.config.values.EnumValue;
22
import combatant.client.config.values.NumberValue;
23
import combatant.client.config.values.RGBAColorValue;
24
import combatant.client.features.module.Module;
25
import combatant.client.features.module.ModuleCategory;
26
import combatant.client.features.module.ModuleInfo;
27
import combatant.client.features.module.WorldPhase;
28
import combatant.client.render.engine.RenderState;
29
import combatant.client.render.engine.pipeline.CombatantRenderPipelines;
30
import combatant.client.render.engine.renderer.Renderer3D;
31
import combatant.client.render.engine.uniform.MeshBuilder;
32
import combatant.client.util.network.BacktrackController;
33
34
/**
35
 * Backtrack module front-end for the shared BlinkManager-backed incoming packet controller.
36
 */
37
//todo Description
38
@ModuleInfo(
39
        id = "backtrack",
40
        displayName = "Backtrack",
41
        category = ModuleCategory.COMBAT
42
)
43
public final class Backtrack extends Module {
44
45
    private final NumberValue<Float> minRange = numCommon(
46
            "backtrackMinRange",
47
            "range_min",
48
            CommonSettingSchemas.COMBAT_BACKTRACK_RANGE_MIN,
49
            0.0f,
50
            0.0f,
51
            10.0f
52
    );
53
54
    private final NumberValue<Float> maxRange = numCommon(
55
            "backtrackMaxRange",
56
            "range_max",
57
            CommonSettingSchemas.COMBAT_BACKTRACK_RANGE_MAX,
58
            3.0f,
59
            0.0f,
60
            10.0f
61
    );
62
63
    private final NumberValue<Integer> minDelay = numCommon(
64
            "backtrackMinDelay",
65
            "delay_min",
66
            CommonSettingSchemas.COMBAT_BACKTRACK_DELAY_MIN,
67
            100,
68
            0,
69
            1000
70
    );
71
    private final NumberValue<Integer> maxDelay = numCommon(
72
            "backtrackMaxDelay",
73
            "delay_max",
74
            CommonSettingSchemas.COMBAT_BACKTRACK_DELAY_MAX,
75
            150,
76
            0,
77
            1000
78
    );
79
80
    private final NumberValue<Integer> minNextBacktrackDelay = numCommon(
81
            "backtrackMinNextDelay",
82
            "next_delay_min",
83
            CommonSettingSchemas.COMBAT_BACKTRACK_NEXT_DELAY_MIN,
84
            0,
85
            0,
86
            2000
87
    );
88
89
    private final NumberValue<Integer> maxNextBacktrackDelay = numCommon(
90
            "backtrackMaxNextDelay",
91
            "next_delay_max",
92
            CommonSettingSchemas.COMBAT_BACKTRACK_NEXT_DELAY_MAX,
93
            10,
94
            0,
95
            2000
96
    );
97
98
    private final NumberValue<Integer> trackingBuffer = numCommon(
99
            "backtrackTrackingBuffer",
100
            "tracking_buffer",
101
            CommonSettingSchemas.COMBAT_BACKTRACK_TRACKING_BUFFER,
102
            500,
103
            0,
104
            2000
105
    );
106
107
    private final NumberValue<Float> chance = numCommon(
108
            "backtrackChance",
109
            "chance",
110
            CommonSettingSchemas.COMBAT_BACKTRACK_CHANCE,
111
            100.0f,
112
            0.0f,
113
            100.0f
114
    );
115
116
    private final BooleanValue pauseOnHurtTime = boolCommon(
117
            "backtrackPauseOnHurtTime",
118
            "pause_on_hurt_time",
119
            CommonSettingSchemas.COMBAT_BACKTRACK_PAUSE_ON_HURT_TIME,
120
            false
121
    );
122
123
    private final NumberValue<Integer> hurtTime = visibleWhen(numCommon(
124
            "backtrackHurtTime",
125
            "hurt_time",
126
            CommonSettingSchemas.COMBAT_BACKTRACK_HURT_TIME,
127
            3,
128
            0,
129
            10
130
    ), pauseOnHurtTime::get);
131
132
    private final EnumValue<BacktrackController.TargetMode> targetMode = enumCommon(
133
            "backtrackTargetMode",
134
            "target_mode",
135
            CommonSettingSchemas.COMBAT_BACKTRACK_TARGET_MODE,
136
            BacktrackController.TargetMode.ATTACK,
137
            BacktrackController.TargetMode.class
138
    );
139
140
    private final NumberValue<Integer> lastAttackTime = visibleWhen(numCommon(
141
            "backtrackLastAttackTime",
142
            "last_attack_time",
143
            CommonSettingSchemas.COMBAT_BACKTRACK_LAST_ATTACK_TIME,
144
            1000,
145
            0,
146
            5000
147
    ), () -> targetMode.get() == BacktrackController.TargetMode.ATTACK);
148
149
    private final BooleanValue render = boolCommon(
150
            "backtrackRender",
151
            "render",
152
            CommonSettingSchemas.COMBAT_BACKTRACK_RENDER,
153
            true
154
    );
155
156
    private final RGBAColorValue fillColor = visibleWhen(color(
157
            "backtrackFillColor",
158
            "fill_color",
159
            "#552D8CFF"
160
    ), render::get);
161
162
    private final RGBAColorValue lineColor = visibleWhen(color(
163
            "backtrackLineColor",
164
            "line_color",
165
            "#FF9BE4FF"
166
    ), render::get);
167
168
    private final NumberValue<Float> lineWidth = visibleWhen(numCommon(
169
            "backtrackLineWidth",
170
            "line_width",
171
            CommonSettingSchemas.COMBAT_BACKTRACK_LINE_WIDTH,
172
            1.5f,
173
            0.5f,
174
            6.0f
175
    ), render::get);
176
177
    private BacktrackController.Config appliedConfig;
178
179
    private static void addBodyModel(MeshBuilder tris, MeshBuilder lines, Entity entity, Vec3 feet, int fillArgb, int lineArgb) {
180
        AABB currentBox = entity.getBoundingBox();
181
        double width = clamp(Math.max(currentBox.getXsize(), currentBox.getZsize()), 0.35, 1.35);
182
        double height = clamp(currentBox.getYsize(), 0.75, 3.2);
183
        float yaw = entity.getYRot();
184
185
        double depth = width * 0.32;
186
        double legHeight = height * 0.43;
187
        double torsoHeight = height * 0.34;
188
        double headSize = Math.min(width * 0.58, height * 0.24);
189
190
        double legWidth = width * 0.20;
191
        double legDepth = depth * 0.85;
192
        double legOffset = width * 0.14;
193
        double torsoWidth = width * 0.58;
194
        double armWidth = width * 0.17;
195
        double armOffset = torsoWidth * 0.5 + armWidth * 0.6;
196
197
        addBodyPart(tris, lines, feet, -legOffset, legHeight * 0.5, 0.0, legWidth, legHeight, legDepth, yaw, fillArgb, lineArgb);
198
        addBodyPart(tris, lines, feet, legOffset, legHeight * 0.5, 0.0, legWidth, legHeight, legDepth, yaw, fillArgb, lineArgb);
199
        addBodyPart(tris, lines, feet, 0.0, legHeight + torsoHeight * 0.5, 0.0, torsoWidth, torsoHeight, depth, yaw, fillArgb, lineArgb);
200
        addBodyPart(tris, lines, feet, -armOffset, legHeight + torsoHeight * 0.5, 0.0, armWidth, torsoHeight * 0.95, depth * 0.9, yaw, fillArgb, lineArgb);
201
        addBodyPart(tris, lines, feet, armOffset, legHeight + torsoHeight * 0.5, 0.0, armWidth, torsoHeight * 0.95, depth * 0.9, yaw, fillArgb, lineArgb);
202
        addBodyPart(tris, lines, feet, 0.0, height - headSize * 0.5, 0.0, headSize, headSize, headSize, yaw, fillArgb, lineArgb);
203
    }
204
205
    private static void addBodyPart(MeshBuilder tris,
206
                                    MeshBuilder lines,
207
                                    Vec3 origin,
208
                                    double centerX,
209
                                    double centerY,
210
                                    double centerZ,
211
                                    double sizeX,
212
                                    double sizeY,
213
                                    double sizeZ,
214
                                    float yaw,
215
                                    int fillArgb,
216
                                    int lineArgb) {
217
        Vec3[] corners = orientedBoxCorners(origin, centerX, centerY, centerZ, sizeX, sizeY, sizeZ, yaw);
218
219
        quad(tris, corners[0], corners[1], corners[2], corners[3], fillArgb);
220
        quad(tris, corners[4], corners[7], corners[6], corners[5], fillArgb);
221
        quad(tris, corners[0], corners[4], corners[5], corners[1], fillArgb);
222
        quad(tris, corners[3], corners[2], corners[6], corners[7], fillArgb);
223
        quad(tris, corners[1], corners[5], corners[6], corners[2], fillArgb);
224
        quad(tris, corners[0], corners[3], corners[7], corners[4], fillArgb);
225
226
        line(lines, corners[0], corners[1], lineArgb);
227
        line(lines, corners[1], corners[2], lineArgb);
228
        line(lines, corners[2], corners[3], lineArgb);
229
        line(lines, corners[3], corners[0], lineArgb);
230
        line(lines, corners[4], corners[5], lineArgb);
231
        line(lines, corners[5], corners[6], lineArgb);
232
        line(lines, corners[6], corners[7], lineArgb);
233
        line(lines, corners[7], corners[4], lineArgb);
234
        line(lines, corners[0], corners[4], lineArgb);
235
        line(lines, corners[1], corners[5], lineArgb);
236
        line(lines, corners[2], corners[6], lineArgb);
237
        line(lines, corners[3], corners[7], lineArgb);
238
    }
239
240
    private static Vec3[] orientedBoxCorners(Vec3 origin,
241
                                             double centerX,
242
                                             double centerY,
243
                                             double centerZ,
244
                                             double sizeX,
245
                                             double sizeY,
246
                                             double sizeZ,
247
                                             float yaw) {
248
        double halfX = sizeX * 0.5;
249
        double halfY = sizeY * 0.5;
250
        double halfZ = sizeZ * 0.5;
251
252
        return new Vec3[]{
253
                transformBodyVertex(origin, centerX - halfX, centerY - halfY, centerZ - halfZ, yaw),
254
                transformBodyVertex(origin, centerX + halfX, centerY - halfY, centerZ - halfZ, yaw),
255
                transformBodyVertex(origin, centerX + halfX, centerY - halfY, centerZ + halfZ, yaw),
256
                transformBodyVertex(origin, centerX - halfX, centerY - halfY, centerZ + halfZ, yaw),
257
                transformBodyVertex(origin, centerX - halfX, centerY + halfY, centerZ - halfZ, yaw),
258
                transformBodyVertex(origin, centerX + halfX, centerY + halfY, centerZ - halfZ, yaw),
259
                transformBodyVertex(origin, centerX + halfX, centerY + halfY, centerZ + halfZ, yaw),
260
                transformBodyVertex(origin, centerX - halfX, centerY + halfY, centerZ + halfZ, yaw)
261
        };
262
    }
263
264
    private static Vec3 transformBodyVertex(Vec3 origin, double localX, double localY, double localZ, float yaw) {
265
        double radians = Math.toRadians(-yaw);
266
        double sin = Math.sin(radians);
267
        double cos = Math.cos(radians);
268
        double x = localX * cos - localZ * sin;
269
        double z = localX * sin + localZ * cos;
270
        return new Vec3(origin.x + x, origin.y + localY, origin.z + z);
271
    }
272
273
    private static double clamp(double value, double min, double max) {
274
        return Math.max(min, Math.min(max, value));
275
    }
276
277
    private static void quad(MeshBuilder mesh,
278
                             double x1, double y1, double z1,
279
                             double x2, double y2, double z2,
280
                             double x3, double y3, double z3,
281
                             double x4, double y4, double z4,
282
                             int argb) {
283
        mesh.ensureQuadCapacity();
284
        putVertex(mesh, x1, y1, z1, argb);
285
        int i1 = mesh.next();
286
        putVertex(mesh, x2, y2, z2, argb);
287
        int i2 = mesh.next();
288
        putVertex(mesh, x3, y3, z3, argb);
289
        int i3 = mesh.next();
290
        putVertex(mesh, x4, y4, z4, argb);
291
        int i4 = mesh.next();
292
        mesh.quad(i1, i2, i3, i4);
293
    }
294
295
    private static void quad(MeshBuilder mesh, Vec3 p1, Vec3 p2, Vec3 p3, Vec3 p4, int argb) {
296
        quad(mesh, p1.x, p1.y, p1.z, p2.x, p2.y, p2.z, p3.x, p3.y, p3.z, p4.x, p4.y, p4.z, argb);
297
    }
298
299
    private static void line(MeshBuilder mesh,
300
                             double x1, double y1, double z1,
301
                             double x2, double y2, double z2,
302
                             int argb) {
303
        mesh.ensureLineCapacity();
304
        putVertex(mesh, x1, y1, z1, argb);
305
        int i1 = mesh.next();
306
        putVertex(mesh, x2, y2, z2, argb);
307
        int i2 = mesh.next();
308
        mesh.line(i1, i2);
309
    }
310
311
    private static void line(MeshBuilder mesh, Vec3 p1, Vec3 p2, int argb) {
312
        line(mesh, p1.x, p1.y, p1.z, p2.x, p2.y, p2.z, argb);
313
    }
314
315
    private static void putVertex(MeshBuilder mesh, double x, double y, double z, int argb) {
316
        int a = (argb >>> 24) & 0xFF;
317
        int r = (argb >>> 16) & 0xFF;
318
        int g = (argb >>> 8) & 0xFF;
319
        int b = argb & 0xFF;
320
        mesh.vec3(x, y, z).color(r, g, b, a);
321
    }
322
323
    @Override
324
    public void onEnable() {
325
        syncControllerConfig();
326
        BacktrackController.INSTANCE.setEnabled(true);
327
    }
328
329
    @Override
330
    public void onDisable() {
331
        BacktrackController.INSTANCE.setEnabled(false);
332
        appliedConfig = null;
333
    }
334
335
    @Override
336
    public void onTick() {
337
        if (!isEnabled()) {
338
            return;
339
        }
340
        syncControllerConfig();
341
    }
342
343
    @Override
344
    public WorldPhase getWorldPhase() {
345
        return WorldPhase.END_MAIN;
346
    }
347
348
    @Override
349
    public void onRenderWorldEngine(Renderer3D renderer, Renderer3D depthRenderer, float tickDelta) {
350
        if (!isEnabled() || !render.get() || renderer == null) {
351
            return;
352
        }
353
        if (!BacktrackController.INSTANCE.shouldRenderTrackedBody()) {
354
            return;
355
        }
356
357
        Entity target = BacktrackController.INSTANCE.getTarget();
358
        Vec3 trackedPosition = BacktrackController.INSTANCE.getTrackedPosition();
359
        if (target == null || trackedPosition == null || trackedPosition.equals(Vec3.ZERO)) {
360
            return;
361
        }
362
363
        MeshBuilder tris = renderer.batch(CombatantRenderPipelines.WORLD_COLORED, Renderer3D.DepthMode.NONE);
364
        float previousLineWidth = RenderState.lineWidth;
365
        MeshBuilder lines;
366
        try {
367
            RenderState.lineWidth = Math.max(0.5f, lineWidth.get());
368
            lines = renderer.batch(CombatantRenderPipelines.WORLD_COLORED_LINES, Renderer3D.DepthMode.NONE);
369
        } finally {
370
            RenderState.lineWidth = previousLineWidth;
371
        }
372
373
        addBodyModel(tris, lines, target, trackedPosition, fillColor.getArgb(), lineColor.getArgb());
374
    }
375
376
    private void syncControllerConfig() {
377
        BacktrackController.Config config = buildConfig();
378
        if (!config.equals(appliedConfig) || !config.equals(BacktrackController.INSTANCE.getConfig())) {
379
            BacktrackController.INSTANCE.configure(config);
380
            appliedConfig = config;
381
        }
382
    }
383
384
    private BacktrackController.Config buildConfig() {
385
        float min = minRange.get();
386
        float max = maxRange.get();
387
        if (targetMode.get() == BacktrackController.TargetMode.ATTACK) {
388
            min = 0.0f;
389
        }
390
        int minDelayMs = minDelay.get();
391
        int maxDelayMs = maxDelay.get();
392
        int minNextDelayMs = minNextBacktrackDelay.get();
393
        int maxNextDelayMs = maxNextBacktrackDelay.get();
394
395
        return new BacktrackController.Config(
396
                Math.min(min, max),
397
                Math.max(min, max),
398
                Math.min(minDelayMs, maxDelayMs),
399
                Math.max(minDelayMs, maxDelayMs),
400
                Math.min(minNextDelayMs, maxNextDelayMs),
401
                Math.max(minNextDelayMs, maxNextDelayMs),
402
                trackingBuffer.get(),
403
                Math.round(chance.get()),
404
                pauseOnHurtTime.get(),
405
                hurtTime.get(),
406
                lastAttackTime.get(),
407
                targetMode.get()
408
        );
409
    }
410
}
411
LiquidBounce (Kotlin)COMBATsrc/main/kotlin/net/ccbluex/liquidbounce/features/module/modules/combat/backtrack/ModuleBacktrack.ktKotlin · 317 lines · 11.0 KB

Causes targets to lag allowing you to attack them more efficiently.

SettingsRangeDelayNextBacktrackDelayTrackingBufferHurtTimeTargetModeLastAttackTimeToWork
ModuleBacktrack.ktKotlin · Minecraft 26.2 · 317 lines · 11.0 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.combat.backtrack
20
21
import net.ccbluex.liquidbounce.config.types.group.ToggleableValueGroup
22
import net.ccbluex.liquidbounce.config.types.list.Tagged
23
import net.ccbluex.liquidbounce.config.utils.percentageChance
24
import net.ccbluex.liquidbounce.event.events.AttackEntityEvent
25
import net.ccbluex.liquidbounce.event.events.BlinkPacketEvent
26
import net.ccbluex.liquidbounce.event.events.GameTickEvent
27
import net.ccbluex.liquidbounce.event.events.TickPacketProcessEvent
28
import net.ccbluex.liquidbounce.event.events.TransferOrigin
29
import net.ccbluex.liquidbounce.event.events.WorldChangeEvent
30
import net.ccbluex.liquidbounce.event.handler
31
import net.ccbluex.liquidbounce.features.blink.BlinkManager
32
import net.ccbluex.liquidbounce.features.blink.TrackedEntityPosition
33
import net.ccbluex.liquidbounce.features.blink.esp.BlinkEspBox
34
import net.ccbluex.liquidbounce.features.blink.esp.BlinkEspData
35
import net.ccbluex.liquidbounce.features.blink.esp.BlinkEspModel
36
import net.ccbluex.liquidbounce.features.blink.esp.BlinkEspNone
37
import net.ccbluex.liquidbounce.features.blink.esp.BlinkEspWireframe
38
import net.ccbluex.liquidbounce.features.module.ClientModule
39
import net.ccbluex.liquidbounce.features.module.ModuleCategories
40
import net.ccbluex.liquidbounce.features.module.modules.combat.velocity.mode.VelocityReduce
41
import net.ccbluex.liquidbounce.utils.client.Chronometer
42
import net.ccbluex.liquidbounce.utils.client.inGame
43
import net.ccbluex.liquidbounce.utils.combat.findEnemy
44
import net.ccbluex.liquidbounce.utils.combat.shouldBeAttacked
45
import net.ccbluex.liquidbounce.utils.entity.boxedDistanceTo
46
import net.ccbluex.liquidbounce.utils.entity.rotation
47
import net.ccbluex.liquidbounce.utils.entity.squareBoxedDistanceTo
48
import net.ccbluex.liquidbounce.utils.entity.squaredBoxedDistanceTo
49
import net.minecraft.network.protocol.common.ClientboundDisconnectPacket
50
import net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket
51
import net.minecraft.network.protocol.game.ClientboundSetHealthPacket
52
import net.minecraft.network.protocol.game.ClientboundSoundPacket
53
import net.minecraft.network.protocol.game.ClientboundSystemChatPacket
54
import net.minecraft.network.protocol.game.ServerboundChatCommandPacket
55
import net.minecraft.network.protocol.game.ServerboundChatPacket
56
import net.minecraft.sounds.SoundEvents
57
import net.minecraft.world.entity.Entity
58
import net.minecraft.world.entity.LivingEntity
59
import net.minecraft.world.phys.Vec3
60
61
object ModuleBacktrack : ClientModule("Backtrack", ModuleCategories.COMBAT) {
62
63
    private val range by floatRange("Range", 1f..3f, 0f..10f)
64
    val delay by intRange("Delay", 100..150, 0..1000, "ms")
65
    private val nextBacktrackDelay by intRange("NextBacktrackDelay", 0..10, 0..2000, "ms")
66
    private val trackingBuffer by int("TrackingBuffer", 500, 0..2000, "ms")
67
    private val chance = percentageChance("Chance", 50f)
68
    private var chancePassed = chance.asBoolean
69
70
    private object PauseOnHurtTime : ToggleableValueGroup(this, "PauseOnHurtTime", false) {
71
        val hurtTime by int("HurtTime", 3, 0..10)
72
    }
73
74
    private val pauseOnHurtTime = tree(PauseOnHurtTime)
75
76
    private val targetMode by enumChoice("TargetMode", Mode.ATTACK)
77
    private val lastAttackTimeToWork by int("LastAttackTimeToWork", 1000, 0..5000)
78
79
    enum class Mode(override val tag: String) : Tagged {
80
        ATTACK("Attack"),
81
        RANGE("Range")
82
    }
83
84
    private val espMode = choices("Esp", 2) {
85
        arrayOf(
86
            BlinkEspBox(it, ::getEspData),
87
            BlinkEspModel(it, getEspData = ::getEspData),
88
            BlinkEspWireframe(it, ::getEspData),
89
            BlinkEspNone(it),
90
        )
91
    }.apply {
92
        doNotIncludeAlways()
93
    }
94
95
    private val chronometer = Chronometer()
96
    private val trackingBufferChronometer = Chronometer()
97
    private val attackChronometer = Chronometer()
98
99
    private var shouldPause = false
100
101
    private var target: Entity? = null
102
    private val position = TrackedEntityPosition()
103
104
    var currentDelay = delay.random()
105
106
    @Suppress("unused")
107
    private val queuePacketHandler = handler<BlinkPacketEvent> { event ->
108
        if (event.origin != TransferOrigin.INCOMING) {
109
            return@handler
110
        }
111
112
        if (VelocityReduce.ownsIncomingBlinkQueue) {
113
            return@handler
114
        }
115
116
        val packet = event.packet
117
        val shouldCancel = shouldCancelPackets()
118
        val hasQueuedIncoming = hasQueuedIncoming()
119
120
        if (packet == null) {
121
            if (shouldCancel || hasQueuedIncoming) {
122
                event.action = BlinkManager.Action.PASS
123
            }
124
            return@handler
125
        }
126
127
        if (!hasQueuedIncoming && !shouldCancel) {
128
            return@handler
129
        }
130
131
        when (packet) {
132
            // Ignore message-related packets
133
            is ServerboundChatPacket, is ClientboundSystemChatPacket, is ServerboundChatCommandPacket -> {
134
                event.action = BlinkManager.Action.PASS
135
                return@handler
136
            }
137
138
            // Flush on teleport or disconnect
139
            is ClientboundPlayerPositionPacket, is ClientboundDisconnectPacket -> {
140
                clear(true)
141
                return@handler
142
            }
143
144
            // Ignore own hurt sounds
145
            is ClientboundSoundPacket -> {
146
                if (packet.sound.value() == SoundEvents.PLAYER_HURT) {
147
                    event.action = BlinkManager.Action.PASS
148
                    return@handler
149
                }
150
            }
151
152
            // Flush on own death
153
            is ClientboundSetHealthPacket -> {
154
                if (packet.health <= 0) {
155
                    clear(true)
156
                    return@handler
157
                }
158
            }
159
        }
160
161
        // Update box position with these packets
162
        val target = target ?: return@handler
163
        val pos = position.handlePacket(packet, world, target)
164
        if (pos != null) {
165
            // Is the target's actual position closer than its tracked position?
166
            if (target.squareBoxedDistanceTo(player, pos) < target.squaredBoxedDistanceTo(player)) {
167
                // Process all packets. We want to be able to hit the enemy, not the opposite.
168
                event.action = BlinkManager.Action.FLUSH
169
                // And stop right here. No need to cancel further packets.
170
                return@handler
171
            }
172
        }
173
174
        event.action = BlinkManager.Action.QUEUE
175
    }
176
177
    private fun getEspData(): BlinkEspData? {
178
        val entity = target ?: return null
179
        val pos = position.base
180
        val rotation = entity.rotation
181
182
        return BlinkEspData(entity, pos, rotation)
183
    }
184
185
    @Suppress("unused")
186
    private val worldChangeHandler = handler<WorldChangeEvent> { event ->
187
        // Clear packets on disconnect only
188
        if (event.world == null) {
189
            clear(clearOnly = true)
190
        }
191
    }
192
193
    @Suppress("unused")
194
    private val tickPacketProcessHandler = handler<TickPacketProcessEvent> {
195
        if (!inGame) {
196
            clear(clearOnly = true)
197
            return@handler
198
        }
199
200
        if (VelocityReduce.ownsIncomingBlinkQueue) {
201
            return@handler
202
        }
203
204
        val hadQueuedIncoming = hasQueuedIncoming()
205
206
        if (shouldCancelPackets()) {
207
            val now = System.currentTimeMillis()
208
            BlinkManager.flush { snapshot ->
209
                snapshot.origin == TransferOrigin.INCOMING && snapshot.timestamp <= now - currentDelay
210
            }
211
        } else if (hadQueuedIncoming) {
212
            BlinkManager.flush(TransferOrigin.INCOMING)
213
            clear()
214
        }
215
216
        if (!hasQueuedIncoming()) {
217
            currentDelay = delay.random()
218
        }
219
    }
220
221
    @Suppress("unused")
222
    private val attackHandler = handler<AttackEntityEvent> { event ->
223
        attackChronometer.reset() // Update the last attack time
224
        chancePassed = chance.asBoolean
225
226
        if (targetMode != Mode.ATTACK) {
227
            return@handler
228
        }
229
230
        val enemy = event.entity
231
        processTarget(enemy)
232
    }
233
234
    @Suppress("unused")
235
    private val rangeTargetHandler = handler<GameTickEvent> {
236
        if (targetMode != Mode.RANGE) return@handler
237
238
        val enemy = world.findEnemy(range)
239
240
        if (enemy == null) {
241
            clear()
242
            return@handler
243
        }
244
245
        processTarget(enemy)
246
    }
247
248
    private fun processTarget(enemy: Entity) {
249
        shouldPause = enemy is LivingEntity && enemy.hurtTime >= PauseOnHurtTime.hurtTime
250
251
        if (!shouldBacktrack(enemy)) {
252
            return
253
        }
254
255
        // Reset on enemy change
256
        if (enemy != target) {
257
            clear(resetChronometer = false)
258
259
            // Instantly set new position, so it does not look like the box was created with delay
260
            position.setBaseFrom(enemy)
261
        }
262
263
        target = enemy
264
    }
265
266
    override fun onEnabled() {
267
        clear(false)
268
    }
269
270
    override fun onDisabled() {
271
        clear(true)
272
    }
273
274
    private fun clear(handlePackets: Boolean = true, clearOnly: Boolean = false, resetChronometer: Boolean = true) {
275
        if (handlePackets && !clearOnly) {
276
            BlinkManager.flush(TransferOrigin.INCOMING)
277
        } else if (clearOnly) {
278
            BlinkManager.packetQueue.removeIf { snapshot -> snapshot.origin == TransferOrigin.INCOMING }
279
        }
280
281
        if (target != null && resetChronometer) {
282
            chronometer.waitForAtLeast(nextBacktrackDelay.random().toLong())
283
        }
284
285
        target = null
286
        position.base = Vec3.ZERO
287
    }
288
289
    private fun shouldBacktrack(target: Entity): Boolean {
290
        val inRange = target.boxedDistanceTo(player) in range
291
292
        if (inRange) {
293
            trackingBufferChronometer.reset()
294
        }
295
296
        return (inRange || !trackingBufferChronometer.hasElapsed(trackingBuffer.toLong())) &&
297
            target.shouldBeAttacked() &&
298
            player.tickCount > 10 &&
299
            chancePassed &&
300
            chronometer.hasElapsed() &&
301
            !shouldPause() &&
302
            !attackChronometer.hasElapsed(lastAttackTimeToWork.toLong()) &&
303
            !VelocityReduce.backtrackBlocked
304
    }
305
306
    fun isLagging() = running && hasQueuedIncoming()
307
308
    private fun shouldPause() = pauseOnHurtTime.enabled && shouldPause
309
310
    fun shouldCancelPackets() =
311
        target?.let { target -> target.isAlive && shouldBacktrack(target) } == true
312
313
    private fun hasQueuedIncoming() =
314
        BlinkManager.packetQueue.any { snapshot -> snapshot.origin == TransferOrigin.INCOMING }
315
316
}
317