AutoPotion module

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

1 implementationPLAYER

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.

OfflineClientPLAYERsrc/main/java/com/jellypudding/offlineclient/modules/player/AutoPotion.javaJava · 244 lines · 8.2 KB

Drinks a potion when you are hurt or burning or running low.

SettingsHealthPotionsSplash potionsFire resistanceTop upTop up at
AutoPotion.javaJava · Minecraft 26.2 · 244 lines · 8.2 KB · complete file, verbatimOriginal ↗
1
package com.jellypudding.offlineclient.modules.player;
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.EntityUtil;
11
import com.jellypudding.offlineclient.util.FaceMode;
12
import com.jellypudding.offlineclient.util.InventoryUtil;
13
import com.jellypudding.offlineclient.util.Modules;
14
import com.jellypudding.offlineclient.util.RotationPriority;
15
import com.jellypudding.offlineclient.util.UseHold;
16
import net.minecraft.core.component.DataComponents;
17
import net.minecraft.core.registries.BuiltInRegistries;
18
import net.minecraft.world.InteractionHand;
19
import net.minecraft.world.effect.MobEffect;
20
import net.minecraft.world.effect.MobEffectInstance;
21
import net.minecraft.world.effect.MobEffects;
22
import net.minecraft.world.item.Item;
23
import net.minecraft.world.item.ItemStack;
24
import net.minecraft.world.item.Items;
25
import net.minecraft.world.item.alchemy.PotionContents;
26
27
import java.util.List;
28
import java.util.function.Predicate;
29
30
// Drinks a potion when health drops or you catch fire or an effect is about to end.
31
// Bottles anywhere in the inventory count.
32
public final class AutoPotion extends Module {
33
34
    // Ticks to wait after a drink whilst the effect and the health land.
35
    private static final int SETTLE_TICKS = 10;
36
37
    private static final int SECOND = 20;
38
39
    private final NumberSetting health = new NumberSetting("Health",
40
        "Drink a healing potion at or below this many hearts. Zero turns it off.",
41
        7, 0, 10, 0.5, " hearts").min(0).max(20);
42
    private final RegistryListSetting<Item> potions = new RegistryListSetting<>("Potions",
43
        "Bottles that may be drunk. Click to pick them.", BuiltInRegistries.ITEM,
44
        List.of(Items.POTION));
45
    private final BoolSetting splash = new BoolSetting("Splash potions",
46
        "Throws a splash healing potion at your feet when you carry one. Quicker than drinking.", true);
47
    private final BoolSetting fire = new BoolSetting("Fire resistance",
48
        "Drink fire resistance whilst you are burning.", true);
49
    private final BoolSetting topUp = new BoolSetting("Top up",
50
        "Drink again when an effect you already have is about to run out.", true);
51
    private final NumberSetting topUpAt = new NumberSetting("Top up at",
52
        "Seconds of effect left before topping up.", 10, 1, 60, 1, "s")
53
        .min(1).under(topUp);
54
55
    private boolean drinking;
56
    private final UseHold use = new UseHold();
57
    private int settle;
58
    private final InventoryUtil.HotbarLoan loan = new InventoryUtil.HotbarLoan();
59
60
    public AutoPotion() {
61
        super("AutoPotion", "Drinks a potion when you are hurt or burning or running low.",
62
            Category.PLAYER);
63
        addSettings(health, potions, splash, fire, topUp, topUpAt);
64
        searchTags("potion", "auto drink", "strength", "fire resistance");
65
    }
66
67
    public boolean isDrinking() {
68
        return isEnabled() && drinking;
69
    }
70
71
    @Override
72
    public String getSuffix() {
73
        return drinking ? "drinking" : null;
74
    }
75
76
    @Override
77
    protected void onDisable() {
78
        stopDrinking();
79
    }
80
81
    @Subscribe
82
    private void onTick(TickEvent event) {
83
        if (!inGame() || mc.player.isSpectator()) {
84
            stopDrinking();
85
            return;
86
        }
87
        if (mc.player.isDeadOrDying()) {
88
            // Respawn rebuilds the inventory.
89
            loan.forget();
90
            stopDrinking();
91
            settle = 0;
92
            return;
93
        }
94
        if (settle > 0) {
95
            settle--;
96
            return;
97
        }
98
99
        if (drinking) {
100
            continueDrinking();
101
            return;
102
        }
103
        if (!InventoryUtil.inventoryFree()) {
104
            return;
105
        }
106
        if (handBusy()) {
107
            return;
108
        }
109
110
        int slot = findWanted();
111
        if (slot == -1) {
112
            return;
113
        }
114
        beginDrinking(slot);
115
    }
116
117
    private boolean handBusy() {
118
        return Modules.feeding(this);
119
    }
120
121
    private int findWanted() {
122
        if (EntityUtil.healthAtOrBelow(health.getValue())) {
123
            if (splash.isOn()) {
124
                int thrown = findSplash();
125
                if (thrown != -1) {
126
                    throwSplash(thrown);
127
                    return -1;
128
                }
129
            }
130
            int slot = findPotion(effect -> effect == MobEffects.INSTANT_HEALTH.value()
131
                || effect == MobEffects.REGENERATION.value());
132
            if (slot != -1) {
133
                return slot;
134
            }
135
        }
136
        if (fire.isOn() && mc.player.isOnFire()
137
            && !mc.player.hasEffect(MobEffects.FIRE_RESISTANCE)) {
138
            int slot = findPotion(effect -> effect == MobEffects.FIRE_RESISTANCE.value());
139
            if (slot != -1) {
140
                return slot;
141
            }
142
        }
143
        if (!topUp.isOn()) {
144
            return -1;
145
        }
146
        int threshold = topUpAt.getInt() * SECOND;
147
        for (MobEffectInstance active : mc.player.getActiveEffects()) {
148
            if (!active.endsWithin(threshold)) {
149
                continue;
150
            }
151
            MobEffect wanted = active.getEffect().value();
152
            int slot = findPotion(effect -> effect == wanted);
153
            if (slot != -1) {
154
                return slot;
155
            }
156
        }
157
        return -1;
158
    }
159
160
    private int findPotion(Predicate<MobEffect> wanted) {
161
        for (int i = 0; i < InventoryUtil.WHOLE_INVENTORY; i++) {
162
            ItemStack stack = mc.player.getInventory().getItem(i);
163
            if (!isPotion(stack)) {
164
                continue;
165
            }
166
            PotionContents contents = stack.get(DataComponents.POTION_CONTENTS);
167
            for (MobEffectInstance effect : contents.getAllEffects()) {
168
                if (wanted.test(effect.getEffect().value())) {
169
                    return i;
170
                }
171
            }
172
        }
173
        return -1;
174
    }
175
176
    // A splash bottle that heals. Thrown at the feet it lands at once.
177
    private int findSplash() {
178
        for (int i = 0; i < InventoryUtil.WHOLE_INVENTORY; i++) {
179
            ItemStack stack = mc.player.getInventory().getItem(i);
180
            if (!stack.is(Items.SPLASH_POTION) || !stack.has(DataComponents.POTION_CONTENTS)) {
181
                continue;
182
            }
183
            for (MobEffectInstance effect : stack.get(DataComponents.POTION_CONTENTS).getAllEffects()) {
184
                MobEffect kind = effect.getEffect().value();
185
                if (kind == MobEffects.INSTANT_HEALTH.value() || kind == MobEffects.REGENERATION.value()) {
186
                    return i;
187
                }
188
            }
189
        }
190
        return -1;
191
    }
192
193
    // The look packet goes out first. The bottle then lands on your own feet.
194
    private void throwSplash(int slot) {
195
        if (!loan.select(slot)) {
196
            return;
197
        }
198
        FaceMode.SPAM.faceExact(mc.player.getYRot(), 90, RotationPriority.PLACE);
199
        mc.gameMode.useItem(mc.player, InteractionHand.MAIN_HAND);
200
        loan.giveBack();
201
        settle = SETTLE_TICKS;
202
    }
203
204
    // Splash and lingering bottles are thrown and not drunk.
205
    private boolean isPotion(ItemStack stack) {
206
        return potions.contains(stack.getItem())
207
            && stack.has(DataComponents.POTION_CONTENTS)
208
            && stack.has(DataComponents.CONSUMABLE);
209
    }
210
211
    private void beginDrinking(int slot) {
212
        if (!loan.select(slot)) {
213
            return;
214
        }
215
        drinking = true;
216
        use.begin();
217
    }
218
219
    private void continueDrinking() {
220
        // A screen swallows the use key.
221
        if (mc.gui.screen() != null || !isPotion(mc.player.getInventory().getSelectedItem())) {
222
            stopDrinking();
223
            return;
224
        }
225
        if (!use.tick()) {
226
            finishDrink();
227
        }
228
    }
229
230
    private void finishDrink() {
231
        stopDrinking();
232
        settle = SETTLE_TICKS;
233
    }
234
235
    private void stopDrinking() {
236
        if (drinking) {
237
            drinking = false;
238
            use.release();
239
        }
240
        // A loan whose return was refused earlier gets another go.
241
        loan.giveBack();
242
    }
243
}
244