PotionArrows module

PotionArrows: 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/PotionArrows.javaJava · 322 lines · 11.4 KB

Shoots tipped arrows into yourself to take their effects.

SettingsEffectsCooldownSkip activeSilent bowChat infoOnly in holesOnly on groundMin health
PotionArrows.javaJava · Minecraft 26.2 · 322 lines · 11.4 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.TickEvent;
5
import com.jellypudding.offlineclient.module.Category;
6
import com.jellypudding.offlineclient.module.Module;
7
import com.jellypudding.offlineclient.setting.BoolSetting;
8
import com.jellypudding.offlineclient.setting.NumberSetting;
9
import com.jellypudding.offlineclient.setting.RegistryListSetting;
10
import com.jellypudding.offlineclient.util.BlockUtil;
11
import com.jellypudding.offlineclient.util.ChatUtil;
12
import com.jellypudding.offlineclient.util.EntityUtil;
13
import com.jellypudding.offlineclient.util.InputUtil;
14
import com.jellypudding.offlineclient.util.InventoryUtil;
15
import com.jellypudding.offlineclient.util.InventoryUtil.Swap;
16
import com.jellypudding.offlineclient.util.RotationManager;
17
import net.minecraft.core.BlockPos;
18
import net.minecraft.core.component.DataComponents;
19
import net.minecraft.core.registries.BuiltInRegistries;
20
import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket;
21
import net.minecraft.resources.Identifier;
22
import net.minecraft.world.effect.MobEffect;
23
import net.minecraft.world.effect.MobEffectInstance;
24
import net.minecraft.world.effect.MobEffects;
25
import net.minecraft.world.item.BowItem;
26
import net.minecraft.world.item.ItemStack;
27
import net.minecraft.world.item.Items;
28
import net.minecraft.world.item.alchemy.PotionContents;
29
30
import java.util.ArrayDeque;
31
import java.util.Deque;
32
import java.util.HashSet;
33
import java.util.List;
34
import java.util.Set;
35
36
// Draws the bow straight up just far enough for the arrow to fall back on your head.
37
public final class PotionArrows extends Module {
38
39
    private static final double HEART = 2;
40
41
    // The weakest draw that still fires. The arrow barely leaves the bow.
42
    private static final float RELEASE_POWER = 0.12f;
43
    private static final float STRAIGHT_UP = -90;
44
45
    private final RegistryListSetting<MobEffect> effects = new RegistryListSetting<>("Effects",
46
        "Effects to shoot yourself with in this order.", BuiltInRegistries.MOB_EFFECT,
47
        List.of(MobEffects.STRENGTH.value()));
48
    private final NumberSetting cooldown = new NumberSetting("Cooldown",
49
        "Ticks to wait between shots.", 10, 0, 40, 1, " ticks");
50
    private final BoolSetting skipActive = new BoolSetting("Skip active",
51
        "Leaves out effects you already have.", true);
52
    private final BoolSetting silentBow = new BoolSetting("Silent bow",
53
        "Borrows a bow from the main inventory when none is on the hotbar.", true);
54
    private final BoolSetting chatInfo = new BoolSetting("Chat info",
55
        "Says in chat why it stopped.", false);
56
    private final BoolSetting onlyInHoles = new BoolSetting("Only in holes",
57
        "Only shoots whilst you stand in a hole.", true);
58
    private final BoolSetting onlyOnGround = new BoolSetting("Only on ground",
59
        "Only shoots whilst you stand on the ground.", true);
60
    private final NumberSetting minHealth = new NumberSetting("Min health",
61
        "Stops once your health plus absorption drops under this many hearts.",
62
        5, 0, 18, 0.5, " hearts");
63
64
    private final InventoryUtil.HotbarLoan bow = new InventoryUtil.HotbarLoan();
65
66
    // Inventory indexes of the arrow stacks still to shoot.
67
    private final Deque<Integer> arrows = new ArrayDeque<>();
68
69
    // Network slots swapped. The next arrow is the one the bow reads first.
70
    private int movedFrom = -1;
71
    private int movedTo = -1;
72
    private boolean drawing;
73
    private int timer;
74
75
    public PotionArrows() {
76
        super("PotionArrows", "Shoots tipped arrows into yourself to take their effects.",
77
            Category.COMBAT);
78
        addSettings(effects, cooldown, skipActive, silentBow, chatInfo, onlyInHoles, onlyOnGround,
79
            minHealth);
80
        searchTags("quiver", "tipped arrow", "self shoot");
81
    }
82
83
    @Override
84
    public boolean savesEnabledState() {
85
        return false;
86
    }
87
88
    @Override
89
    public String getSuffix() {
90
        return count(arrows.size());
91
    }
92
93
    @Override
94
    protected void onEnable() {
95
        arrows.clear();
96
        movedFrom = -1;
97
        movedTo = -1;
98
        drawing = false;
99
        timer = 0;
100
        bow.forget();
101
        if (!inGame() || mc.player.isSpectator()) {
102
            setEnabled(false);
103
            return;
104
        }
105
        if (!allowed()) {
106
            return;
107
        }
108
        int slot = findBow();
109
        if (slot == -1) {
110
            stop("No usable bow.");
111
            return;
112
        }
113
        // A held bow keeps its draw and would fire the wrong arrow.
114
        InputUtil.release(mc.options.keyUse);
115
        mc.gameMode.releaseUsingItem(mc.player);
116
        if (!bow.select(slot)) {
117
            stop("Could not pick up the bow.");
118
            return;
119
        }
120
        collectArrows();
121
        if (arrows.isEmpty()) {
122
            stop("No tipped arrows for the chosen effects.");
123
        }
124
    }
125
126
    @Override
127
    protected void onDisable() {
128
        if (!inGame()) {
129
            return;
130
        }
131
        if (drawing) {
132
            InputUtil.release(mc.options.keyUse);
133
            mc.gameMode.releaseUsingItem(mc.player);
134
            drawing = false;
135
        }
136
        restoreArrow();
137
        bow.giveBack();
138
    }
139
140
    @Subscribe
141
    private void onTick(TickEvent event) {
142
        if (!inGame() || mc.player.isSpectator() || !allowed()) {
143
            return;
144
        }
145
        if (arrows.isEmpty()) {
146
            setEnabled(false);
147
            return;
148
        }
149
        if (timer > 0) {
150
            timer--;
151
            return;
152
        }
153
        if (!drawing) {
154
            startDraw();
155
        } else if (BowItem.getPowerForTime(mc.player.getTicksUsingItem()) >= RELEASE_POWER) {
156
            fire();
157
        }
158
    }
159
160
    private void startDraw() {
161
        if (!bow.stillMine() && !bow.select(findBow())) {
162
            stop("Lost the bow.");
163
            return;
164
        }
165
        if (!bringForward(arrows.peekFirst())) {
166
            return;
167
        }
168
        InputUtil.hold(mc.options.keyUse);
169
        drawing = true;
170
    }
171
172
    // The look packet goes out on its own. The release lands whilst the server
173
    // still believes the bow points at the sky.
174
    private void fire() {
175
        mc.player.connection.send(new ServerboundMovePlayerPacket.Rot(
176
            RotationManager.serverYaw(), STRAIGHT_UP, mc.player.onGround(),
177
            mc.player.horizontalCollision));
178
        InputUtil.release(mc.options.keyUse);
179
        mc.gameMode.releaseUsingItem(mc.player);
180
        drawing = false;
181
        restoreArrow();
182
        arrows.pollFirst();
183
        timer = cooldown.getInt();
184
    }
185
186
    // Every check that ends the module. The reason goes to chat when asked for.
187
    private boolean allowed() {
188
        if (!headroom()) {
189
            return stop("No room above your head for the arrow.");
190
        }
191
        if (EntityUtil.totalHealth(mc.player) < minHealth.getValue() * HEART) {
192
            return stop("Health is too low.");
193
        }
194
        if (onlyOnGround.isOn() && !mc.player.onGround()) {
195
            return stop("Not on the ground.");
196
        }
197
        if (onlyInHoles.isOn() && !BlockUtil.playerInHole()) {
198
            return stop("Not in a hole.");
199
        }
200
        return true;
201
    }
202
203
    private boolean stop(String reason) {
204
        if (chatInfo.isOn()) {
205
            ChatUtil.error("PotionArrows stopped. " + reason);
206
        }
207
        setEnabled(false);
208
        return false;
209
    }
210
211
    // The arrow needs the two blocks above the feet clear to rise and fall back.
212
    private boolean headroom() {
213
        BlockPos feet = mc.player.blockPosition();
214
        return BlockUtil.state(feet.above()).getCollisionShape(mc.level, feet.above()).isEmpty()
215
            && BlockUtil.state(feet.above(2)).getCollisionShape(mc.level, feet.above(2)).isEmpty();
216
    }
217
218
    // Inventory index of a bow the settings allow. Minus one when there is none.
219
    private int findBow() {
220
        int hotbar = InventoryUtil.findSlot(Items.BOW, InventoryUtil.HOTBAR_SIZE);
221
        if (hotbar != -1 || !silentBow.isOn()) {
222
            return hotbar;
223
        }
224
        return InventoryUtil.findSlot(Items.BOW, InventoryUtil.WHOLE_INVENTORY);
225
    }
226
227
    // One arrow stack per wanted effect in the order the list gives them.
228
    private void collectArrows() {
229
        Set<Integer> taken = new HashSet<>();
230
        for (Identifier id : effects.getValue()) {
231
            if (!BuiltInRegistries.MOB_EFFECT.containsKey(id)) {
232
                continue;
233
            }
234
            MobEffect effect = BuiltInRegistries.MOB_EFFECT.getValue(id);
235
            if (skipActive.isOn() && hasEffect(effect)) {
236
                continue;
237
            }
238
            for (int i = 0; i < InventoryUtil.WHOLE_INVENTORY; i++) {
239
                ItemStack stack = mc.player.getInventory().getItem(i);
240
                if (!taken.contains(i) && stack.is(Items.TIPPED_ARROW) && carries(stack, effect)) {
241
                    taken.add(i);
242
                    arrows.addLast(i);
243
                    break;
244
                }
245
            }
246
        }
247
    }
248
249
    private boolean hasEffect(MobEffect effect) {
250
        for (MobEffectInstance active : mc.player.getActiveEffects()) {
251
            if (active.getEffect().value() == effect) {
252
                return true;
253
            }
254
        }
255
        return false;
256
    }
257
258
    private static boolean carries(ItemStack stack, MobEffect effect) {
259
        PotionContents contents = stack.get(DataComponents.POTION_CONTENTS);
260
        if (contents == null) {
261
            return false;
262
        }
263
        for (MobEffectInstance instance : contents.getAllEffects()) {
264
            if (instance.getEffect().value() == effect) {
265
                return true;
266
            }
267
        }
268
        return false;
269
    }
270
271
    // Swaps the arrow into the slot the bow reads first. False whilst the click cannot land.
272
    private boolean bringForward(int index) {
273
        if (!mc.player.getInventory().getItem(index).is(Items.TIPPED_ARROW)) {
274
            // The stack was moved or used up since the list was built.
275
            arrows.pollFirst();
276
            return false;
277
        }
278
        int first = firstArrowSlot();
279
        int wanted = InventoryUtil.networkSlot(index);
280
        if (first == -1 || first == wanted) {
281
            return true;
282
        }
283
        if (!InventoryUtil.canClick() || !InventoryUtil.carried().isEmpty()) {
284
            return false;
285
        }
286
        if (InventoryUtil.swap(wanted, first) != Swap.DONE) {
287
            return false;
288
        }
289
        movedFrom = wanted;
290
        movedTo = first;
291
        return true;
292
    }
293
294
    // Puts the two swapped stacks back where they were. A shot that used up the
295
    // whole stack leaves the front slot empty and the other stack goes home on its own.
296
    private void restoreArrow() {
297
        if (movedFrom == -1) {
298
            return;
299
        }
300
        if (InventoryUtil.canClick() && InventoryUtil.carried().isEmpty()
301
            && InventoryUtil.swap(movedTo, movedFrom) == Swap.REFUSED) {
302
            InventoryUtil.swap(movedFrom, movedTo);
303
        }
304
        movedFrom = -1;
305
        movedTo = -1;
306
    }
307
308
    // The network slot of the arrow the bow would fire. The offhand beats everything.
309
    private int firstArrowSlot() {
310
        if (isArrow(mc.player.getOffhandItem())) {
311
            return InventoryUtil.OFFHAND_SLOT;
312
        }
313
        int slot = InventoryUtil.findSlot(PotionArrows::isArrow, InventoryUtil.WHOLE_INVENTORY);
314
        return slot == -1 ? -1 : InventoryUtil.networkSlot(slot);
315
    }
316
317
    private static boolean isArrow(ItemStack stack) {
318
        return stack.is(Items.ARROW) || stack.is(Items.TIPPED_ARROW)
319
            || stack.is(Items.SPECTRAL_ARROW);
320
    }
321
}
322