BowAimbot module

BowAimbot: Minecraft 26.3 cheat module, full source for every implementation (1 client: OfflineClient).

1 implementationCOMBAT

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.

OfflineClientCOMBATsrc/main/java/com/jellypudding/offlineclient/modules/combat/BowAimbot.javaJava · 403 lines · 16.8 KB

Aims your bow or crossbow at the nearest target whilst you draw it.

SettingsBabiesNamed mobsRangePredictPredict strengthAim at sleepingAim at invisibleAim at petsNeutral mobsIgnore flyingThrough wallsHighlightHighlight colourCharge readoutTrajectoryNo slowdown
BowAimbot.javaJava · Minecraft 26.2 · 403 lines · 16.8 KB · complete file, verbatimOriginal ↗
1
package com.jellypudding.offlineclient.modules.combat;
2
3
import com.jellypudding.offlineclient.event.Subscribe;
4
import com.jellypudding.offlineclient.event.events.Render2DEvent;
5
import com.jellypudding.offlineclient.event.events.Render3DEvent;
6
import com.jellypudding.offlineclient.event.events.TickEvent;
7
import com.jellypudding.offlineclient.module.Category;
8
import com.jellypudding.offlineclient.module.Module;
9
import com.jellypudding.offlineclient.modules.render.Trajectories;
10
import com.jellypudding.offlineclient.setting.BoolSetting;
11
import com.jellypudding.offlineclient.setting.ColorSetting;
12
import com.jellypudding.offlineclient.setting.EnumSetting;
13
import com.jellypudding.offlineclient.setting.NumberSetting;
14
import com.jellypudding.offlineclient.util.ColorUtil;
15
import com.jellypudding.offlineclient.util.EntityFilter;
16
import com.jellypudding.offlineclient.util.EntityUtil;
17
import com.jellypudding.offlineclient.util.Modules;
18
import com.jellypudding.offlineclient.util.ProjectileUtil;
19
import com.jellypudding.offlineclient.util.RenderUtil;
20
import com.jellypudding.offlineclient.util.TargetPriority;
21
22
import net.minecraft.client.gui.GuiGraphicsExtractor;
23
import net.minecraft.world.entity.Entity;
24
import net.minecraft.world.entity.LivingEntity;
25
import net.minecraft.world.entity.Mob;
26
import net.minecraft.world.entity.player.Player;
27
import net.minecraft.world.item.BowItem;
28
import net.minecraft.world.item.CrossbowItem;
29
import net.minecraft.world.item.ItemStack;
30
import net.minecraft.world.level.ClipContext;
31
import net.minecraft.world.phys.AABB;
32
import net.minecraft.world.phys.HitResult;
33
import net.minecraft.world.phys.Vec3;
34
35
import java.util.List;
36
37
// The pitch comes from the real arrow physics and the flight time leads the target.
38
public final class BowAimbot extends Module {
39
40
    public enum Calm { ALWAYS, ANGRY_ONLY, NEVER }
41
42
43
    // Pixels below the middle of the screen the readout sits.
44
    private static final int CROSSHAIR_GAP = 12;
45
46
    // Arrow speed in blocks per tick at a full bow draw.
47
    private static final double BOW_SPEED = 3.0;
48
    // Arrow speed in blocks per tick from a crossbow.
49
    private static final double CROSSBOW_SPEED = 3.15;
50
51
    private final EntityFilter filter = EntityFilter.living("Aim at", "aimed at", true,
52
        EntityFilter.Pick.NONE, List.of());
53
    private final BoolSetting babies = new BoolSetting("Babies",
54
        "Also aim at baby mobs.", true);
55
    private final BoolSetting named = new BoolSetting("Named mobs",
56
        "Also aim at mobs that have been given a name tag.", false);
57
    private final NumberSetting range = new NumberSetting("Range",
58
        "Furthest target to aim at in blocks.", 40, 5, 80, 1);
59
    private final EnumSetting<TargetPriority> priority = TargetPriority.setting("Aims at",
60
        TargetPriority.NEAREST);
61
    private final BoolSetting predict = new BoolSetting("Predict",
62
        "Lead moving targets by their speed and the arrow flight time.", true);
63
    private final NumberSetting predictStrength = new NumberSetting("Predict strength",
64
        "How much of the worked out lead is used.", 100, 0, 200, 5, "%").min(0).max(200)
65
        .under(predict);
66
    private final BoolSetting sleeping = new BoolSetting("Aim at sleeping",
67
        "Also aim at players lying in a bed.", false);
68
    private final BoolSetting invisible = new BoolSetting("Aim at invisible",
69
        "Also aim at entities you cannot see.", false);
70
    private final BoolSetting pets = new BoolSetting("Aim at pets",
71
        "Also aim at tamed animals and mounts.", false);
72
    private final EnumSetting<Calm> neutral = new EnumSetting<>("Neutral mobs",
73
        "Whether mobs that only fight back are aimed at.", Calm.NEVER)
74
        .describe(Calm.ALWAYS, "Always aim at them.")
75
        .describe(Calm.ANGRY_ONLY, "Only once they have turned on you.")
76
        .describe(Calm.NEVER, "Never aim at them.");
77
    private final NumberSetting flying = new NumberSetting("Ignore flying",
78
        "Skip players with no block this far under them. Nought turns it off.",
79
        0, 0, 4, 0.5, " blocks").min(0);
80
    private final BoolSetting walls = new BoolSetting("Through walls",
81
        "Also aim at targets you cannot see.", false);
82
    private final BoolSetting render = new BoolSetting("Highlight",
83
        "Draw a box around the target that fills in as the bow charges.", true);
84
    private final ColorSetting highlightColor = new ColorSetting("Highlight colour",
85
        "Colour of that box.", 0, 0.81f, 1f, false).under(render);
86
    private final BoolSetting readout = new BoolSetting("Charge readout",
87
        "Write how far the bow is drawn under your crosshair.", true);
88
    private final BoolSetting trajectory = new BoolSetting("Trajectory",
89
        "Draw the arc the arrow will fly. The Trajectories module draws the same arc so it takes over whilst it is on.", true);
90
    private final BoolSetting noSlow = new BoolSetting("No slowdown",
91
        "Move at full speed whilst drawing.", true);
92
93
    private Entity target;
94
    private float charge;
95
    private boolean wasDrawing;
96
97
    public BowAimbot() {
98
        super("BowAimbot", "Aims your bow or crossbow at the nearest target whilst you draw it.", Category.COMBAT);
99
        addSettings(filter.settings());
100
        addSettings(babies, named, sleeping, invisible, pets, neutral, flying,
101
            range, priority, predict, predictStrength, walls,
102
            render, highlightColor, readout, trajectory, noSlow);
103
        searchTags("bow aim", "crossbow", "aimbot", "arrow");
104
    }
105
106
    @Override
107
    public String getSuffix() {
108
        return target == null ? null : target.getName().getString();
109
    }
110
111
    @Override
112
    protected void onDisable() {
113
        target = null;
114
        charge = 0;
115
        wasDrawing = false;
116
    }
117
118
    // Consulted by LocalPlayerMixin to skip the bow draw slowdown.
119
    public boolean suppressesSlowdown() {
120
        return isEnabled() && noSlow.isOn() && inGame() && mc.player.isUsingItem();
121
    }
122
123
    @Subscribe
124
    private void onTick(TickEvent event) {
125
        if (!inGame() || mc.player.isSpectator()) {
126
            target = null;
127
            return;
128
        }
129
130
        double speed = launchSpeed();
131
        if (speed <= 0) {
132
            target = null;
133
            charge = 0;
134
            wasDrawing = false;
135
            return;
136
        }
137
138
        if (target != null && !valid(target)) {
139
            target = null;
140
        }
141
        // The crosshair choice is made once at the start of the draw whilst the
142
        // rotation is still the player's own.
143
        boolean drawStart = !wasDrawing;
144
        wasDrawing = true;
145
        if (target == null || (priority.is(TargetPriority.CLOSEST_ANGLE) && drawStart)) {
146
            target = EntityUtil.best(range.getValue(), priority.getValue(), this::valid);
147
        }
148
        if (target == null) {
149
            return;
150
        }
151
152
        aim(target, speed);
153
    }
154
155
    // Speed the arrow would leave with right now or zero when no ranged weapon is ready.
156
    private double launchSpeed() {
157
        ItemStack held = mc.player.getMainHandItem();
158
        if (held.getItem() instanceof BowItem) {
159
            if (!mc.player.isUsingItem() || !(mc.player.getUseItem().getItem() instanceof BowItem)) {
160
                return 0;
161
            }
162
            charge = BowItem.getPowerForTime(mc.player.getTicksUsingItem());
163
            return Math.max(charge, 0.1f) * BOW_SPEED;
164
        }
165
        if (held.getItem() instanceof CrossbowItem) {
166
            if (!CrossbowItem.isCharged(held) || !mc.options.keyUse.isDown()) {
167
                return 0;
168
            }
169
            charge = 1;
170
            return CROSSBOW_SPEED;
171
        }
172
        ItemStack offhand = mc.player.getOffhandItem();
173
        if (offhand.getItem() instanceof BowItem && mc.player.isUsingItem()
174
            && mc.player.getUseItem().getItem() instanceof BowItem) {
175
            charge = BowItem.getPowerForTime(mc.player.getTicksUsingItem());
176
            return Math.max(charge, 0.1f) * BOW_SPEED;
177
        }
178
        return 0;
179
    }
180
181
    private boolean valid(Entity entity) {
182
        if (entity == mc.player || entity == mc.getCameraEntity() || !filter.matches(entity)) {
183
            return false;
184
        }
185
        if (!entity.isAlive() || mc.player.distanceTo(entity) > range.getValue()) {
186
            return false;
187
        }
188
        if (!invisible.isOn() && entity.isInvisible()) {
189
            return false;
190
        }
191
        if (entity instanceof Player player) {
192
            if (player.isCreative() || EntityUtil.isFriend(player)) {
193
                return false;
194
            }
195
            if (!sleeping.isOn() && player.isSleeping()) {
196
                return false;
197
            }
198
            if (flying.getValue() > 0 && airborne(player)) {
199
                return false;
200
            }
201
        } else {
202
            if (!pets.isOn() && entity instanceof Mob mob && EntityUtil.isPet(mob)) {
203
                return false;
204
            }
205
            if (entity instanceof Mob mob && EntityUtil.isNeutral(mob) && skipNeutral(mob)) {
206
                return false;
207
            }
208
            if (entity instanceof LivingEntity living && living.isDeadOrDying()) {
209
                return false;
210
            }
211
            if (!babies.isOn() && entity instanceof LivingEntity living && living.isBaby()) {
212
                return false;
213
            }
214
            if (!named.isOn() && entity.hasCustomName()) {
215
                return false;
216
            }
217
        }
218
        return walls.isOn() || mc.player.hasLineOfSight(entity);
219
    }
220
221
    private boolean skipNeutral(Mob mob) {
222
        return switch (neutral.getValue()) {
223
            case ALWAYS -> false;
224
            case ANGRY_ONLY -> EntityUtil.isCalm(mob);
225
            case NEVER -> true;
226
        };
227
    }
228
229
    // True whilst nothing solid sits under the player within the set drop.
230
    private boolean airborne(Player player) {
231
        AABB feet = player.getBoundingBox().move(0, -flying.getValue(), 0)
232
            .expandTowards(0, flying.getValue(), 0);
233
        return mc.level.noCollision(player, feet);
234
    }
235
236
    // Turns the player towards the point the arrow needs to fly through.
237
    private void aim(Entity entity, double speed) {
238
        Vec3 eye = mc.player.getEyePosition();
239
        Vec3 aimPoint = visiblePoint(entity, eye);
240
        Vec3 targetVelocity = predict.isOn()
241
            ? EntityUtil.velocityOf(entity).scale(predictStrength.getValue() / 100.0)
242
            : Vec3.ZERO;
243
244
        // The server hands the arrow the distance the last packet moved.
245
        // The vertical part only counts in the air.
246
        Vec3 own = EntityUtil.velocityOf(mc.player);
247
        Vec3 drift = new Vec3(own.x, mc.player.onGround() ? 0 : own.y, own.z);
248
249
        double[] solution = solve(eye, aimPoint, speed);
250
        if (solution != null) {
251
            // The target keeps its pace whilst the momentum the arrow inherited from
252
            // a sprint or a jump bleeds off to the drag every tick.
253
            double flight = solution[1];
254
            Vec3 lead = aimPoint.add(targetVelocity.scale(flight))
255
                .subtract(drift.scale(carriedTicks(flight)));
256
            double[] again = solve(eye, lead, speed);
257
            if (again != null) {
258
                solution = again;
259
                aimPoint = lead;
260
            }
261
        }
262
263
        double dx = aimPoint.x - eye.x;
264
        double dz = aimPoint.z - eye.z;
265
        float yaw = (float) Math.toDegrees(Math.atan2(dz, dx)) - 90f;
266
        float pitch;
267
        if (solution == null) {
268
            double dy = aimPoint.y - eye.y;
269
            pitch = (float) -Math.toDegrees(Math.atan2(dy, Math.sqrt(dx * dx + dz * dz)));
270
        } else {
271
            pitch = (float) -solution[0];
272
        }
273
274
        // The view itself turns. A bow shot has to leave from where the camera points.
275
        mc.player.setYRot(yaw);
276
        mc.player.setXRot(Math.clamp(pitch, -90f, 90f));
277
    }
278
279
    // Aims at the target's middle when it is visible otherwise the highest
280
    // visible part since a target in a hole only shows head and chest.
281
    private Vec3 visiblePoint(Entity entity, Vec3 eye) {
282
        AABB box = entity.getBoundingBox();
283
        double x = (box.minX + box.maxX) / 2;
284
        double z = (box.minZ + box.maxZ) / 2;
285
        double height = box.maxY - box.minY;
286
        double[] shares = {0.5, 0.75, 0.9, 0.3};
287
        for (double share : shares) {
288
            Vec3 point = new Vec3(x, box.minY + height * share, z);
289
            HitResult hit = mc.level.clip(new ClipContext(eye, point,
290
                ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, mc.player));
291
            if (hit.getType() == HitResult.Type.MISS) {
292
                return point;
293
            }
294
        }
295
        return box.getCenter();
296
    }
297
298
    // Sum of the drag series over the flight. How far the inherited speed really carries.
299
    private static double carriedTicks(double ticks) {
300
        if (ticks <= 0) {
301
            return 0;
302
        }
303
        return (1 - Math.pow(ProjectileUtil.ARROW_DRAG, ticks)) / (1 - ProjectileUtil.ARROW_DRAG);
304
    }
305
306
    // Finds the lowest launch angle that lands an arrow on the point.
307
    // Returns the angle and flight time in ticks or null when out of reach.
308
    private static double[] solve(Vec3 from, Vec3 to, double speed) {
309
        double dx = to.x - from.x;
310
        double dz = to.z - from.z;
311
        double distance = Math.sqrt(dx * dx + dz * dz);
312
        double height = to.y - from.y;
313
        if (distance < 0.01) {
314
            return null;
315
        }
316
317
        double previous = -90;
318
        double previousHeight = Double.NaN;
319
        for (double angle = -89; angle <= 89; angle += 1) {
320
            double h = heightAt(angle, distance, speed);
321
            if (Double.isNaN(h)) {
322
                continue;
323
            }
324
            if (h >= height && !Double.isNaN(previousHeight)) {
325
                double low = previous;
326
                double high = angle;
327
                for (int i = 0; i < 16; i++) {
328
                    double mid = (low + high) / 2;
329
                    double midHeight = heightAt(mid, distance, speed);
330
                    if (Double.isNaN(midHeight) || midHeight < height) {
331
                        low = mid;
332
                    } else {
333
                        high = mid;
334
                    }
335
                }
336
                return new double[] {high, flightTicks(high, distance, speed)};
337
            }
338
            if (h >= height) {
339
                return new double[] {angle, flightTicks(angle, distance, speed)};
340
            }
341
            previous = angle;
342
            previousHeight = h;
343
        }
344
        return null;
345
    }
346
347
    // Ticks an arrow needs to travel the horizontal distance or NaN if it never gets there.
348
    private static double flightTicks(double angleDegrees, double distance, double speed) {
349
        double horizontal = speed * Math.cos(Math.toRadians(angleDegrees));
350
        double reach = horizontal / (1 - ProjectileUtil.ARROW_DRAG);
351
        if (horizontal <= 0 || distance >= reach) {
352
            return Double.NaN;
353
        }
354
        return Math.log(1 - distance / reach) / Math.log(ProjectileUtil.ARROW_DRAG);
355
    }
356
357
    // Height an arrow reaches at the horizontal distance using the closed
358
    // form of the per tick drag and gravity update.
359
    private static double heightAt(double angleDegrees, double distance, double speed) {
360
        double ticks = flightTicks(angleDegrees, distance, speed);
361
        if (Double.isNaN(ticks)) {
362
            return Double.NaN;
363
        }
364
        double vertical = speed * Math.sin(Math.toRadians(angleDegrees));
365
        double terminal = ProjectileUtil.ARROW_GRAVITY / (1 - ProjectileUtil.ARROW_DRAG);
366
        double fallen = 1 - Math.pow(ProjectileUtil.ARROW_DRAG, ticks);
367
        return (vertical + terminal) * fallen / (1 - ProjectileUtil.ARROW_DRAG) - terminal * ticks;
368
    }
369
370
    // A word under the crosshair that says when to let go.
371
    @Subscribe
372
    private void onRender2D(Render2DEvent event) {
373
        if (!readout.isOn() || target == null || !inGame()) {
374
            return;
375
        }
376
        String line = charge >= 1 ? "Target locked"
377
            : "Charging " + Math.round(charge * 100) + "%";
378
        GuiGraphicsExtractor context = event.getContext();
379
        int x = (context.guiWidth() - mc.font.width(line)) / 2;
380
        int y = context.guiHeight() / 2 + CROSSHAIR_GAP;
381
        context.guiRenderState.up();
382
        context.text(mc.font, line, x, y,
383
            charge >= 1 ? highlightColor.getColor() : RenderUtil.MUTED_TEXT, true);
384
    }
385
386
    @Subscribe
387
    private void onRender3D(Render3DEvent event) {
388
        if (target == null || !inGame()) {
389
            return;
390
        }
391
        if (render.isOn()) {
392
            int color = highlightColor.getColor();
393
            AABB box = EntityUtil.lerpedBox(target, event.getPartialTicks());
394
            event.getBatch().outlineBox(box, color, true);
395
            event.getBatch().solidBox(box, ColorUtil.withAlpha(color, (int) (90 * charge)), true);
396
        }
397
        Trajectories trajectories = Modules.get(Trajectories.class);
398
        if (trajectory.isOn() && trajectories != null && !trajectories.isEnabled()) {
399
            trajectories.draw(event.getBatch(), mc.player, event.getPartialTicks());
400
        }
401
    }
402
}
403