auto-eat module

auto-eat: Minecraft 26.3 cheat module, full source for every implementation (1 client: Meteor Client).

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.

Meteor ClientPlayersrc/main/java/meteordevelopment/meteorclient/systems/modules/player/AutoEat.javaJava · 361 lines · 11.6 KB

Automatically eats food.

Settingsblacklistpause-auraspause-baritonesearch-inventoryfood-prioritythreshold-modehealth-thresholdhunger-threshold
AutoEat.javaJava · Minecraft 26.2 · 361 lines · 11.6 KB · complete file, verbatimOriginal ↗
1
/*
2
 * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
3
 * Copyright (c) Meteor Development.
4
 */
5
6
package meteordevelopment.meteorclient.systems.modules.player;
7
8
import it.unimi.dsi.fastutil.booleans.BooleanBinaryOperator;
9
import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
10
import meteordevelopment.meteorclient.events.entity.player.ItemUseCrosshairTargetEvent;
11
import meteordevelopment.meteorclient.events.world.TickEvent;
12
import meteordevelopment.meteorclient.pathing.PathManagers;
13
import meteordevelopment.meteorclient.settings.*;
14
import meteordevelopment.meteorclient.systems.modules.Categories;
15
import meteordevelopment.meteorclient.systems.modules.Module;
16
import meteordevelopment.meteorclient.systems.modules.Modules;
17
import meteordevelopment.meteorclient.systems.modules.combat.AnchorAura;
18
import meteordevelopment.meteorclient.systems.modules.combat.BedAura;
19
import meteordevelopment.meteorclient.systems.modules.combat.CrystalAura;
20
import meteordevelopment.meteorclient.systems.modules.combat.KillAura;
21
import meteordevelopment.meteorclient.utils.Utils;
22
import meteordevelopment.meteorclient.utils.player.InvUtils;
23
import meteordevelopment.meteorclient.utils.player.SlotUtils;
24
import meteordevelopment.orbit.EventHandler;
25
import meteordevelopment.orbit.EventPriority;
26
import net.minecraft.core.component.DataComponents;
27
import net.minecraft.world.food.FoodProperties;
28
import net.minecraft.world.item.Item;
29
import net.minecraft.world.item.ItemStack;
30
import net.minecraft.world.item.Items;
31
32
import java.util.List;
33
34
public class AutoEat extends Module {
35
    @SuppressWarnings("unchecked")
36
    private static final Class<? extends Module>[] AURAS = new Class[]{KillAura.class, CrystalAura.class, AnchorAura.class, BedAura.class};
37
38
    // Settings groups
39
    private final SettingGroup sgGeneral = settings.getDefaultGroup();
40
    private final SettingGroup sgThreshold = settings.createGroup("Threshold");
41
42
    // General
43
    public final Setting<List<Item>> blacklist = sgGeneral.add(new ItemListSetting.Builder()
44
        .name("blacklist")
45
        .description("Which items to not eat.")
46
        .defaultValue(
47
            Items.ENCHANTED_GOLDEN_APPLE,
48
            Items.GOLDEN_APPLE,
49
            Items.CHORUS_FRUIT,
50
            Items.POISONOUS_POTATO,
51
            Items.PUFFERFISH,
52
            Items.CHICKEN,
53
            Items.ROTTEN_FLESH,
54
            Items.SPIDER_EYE,
55
            Items.SUSPICIOUS_STEW
56
        )
57
        .filter(Utils::isFood)
58
        .bypassFilterWhenSavingAndLoading()
59
        .build()
60
    );
61
62
    private final Setting<Boolean> pauseAuras = sgGeneral.add(new BoolSetting.Builder()
63
        .name("pause-auras")
64
        .description("Pauses all auras when eating.")
65
        .defaultValue(true)
66
        .build()
67
    );
68
69
    private final Setting<Boolean> pauseBaritone = sgGeneral.add(new BoolSetting.Builder()
70
        .name("pause-baritone")
71
        .description("Pause baritone when eating.")
72
        .defaultValue(true)
73
        .build()
74
    );
75
76
    private final Setting<Boolean> searchInventory = sgGeneral.add(new BoolSetting.Builder()
77
        .name("search-inventory")
78
        .description("Search the full inventory for food, not only the hotbar.")
79
        .defaultValue(false)
80
        .build()
81
    );
82
83
    private final Setting<Priority> prioritise = sgGeneral.add(new EnumSetting.Builder<Priority>()
84
        .name("food-priority")
85
        .description("Which aspect of the food to prioritise selecting for.")
86
        .defaultValue(Priority.Saturation)
87
        .build()
88
    );
89
90
    // Threshold
91
    private final Setting<ThresholdMode> thresholdMode = sgThreshold.add(new EnumSetting.Builder<ThresholdMode>()
92
        .name("threshold-mode")
93
        .description("The threshold mode to trigger auto eat.\n'Both' == health AND hunger, 'Any' == health OR hunger")
94
        .defaultValue(ThresholdMode.Any)
95
        .build()
96
    );
97
98
    private final Setting<Double> healthThreshold = sgThreshold.add(new DoubleSetting.Builder()
99
        .name("health-threshold")
100
        .description("The level of health you eat at.")
101
        .defaultValue(10)
102
        .range(1, 19)
103
        .sliderRange(1, 19)
104
        .visible(() -> thresholdMode.get() != ThresholdMode.Hunger)
105
        .build()
106
    );
107
108
    private final Setting<Integer> hungerThreshold = sgThreshold.add(new IntSetting.Builder()
109
        .name("hunger-threshold")
110
        .description("The level of hunger you eat at.")
111
        .defaultValue(16)
112
        .range(1, 19)
113
        .sliderRange(1, 19)
114
        .visible(() -> thresholdMode.get() != ThresholdMode.Health)
115
        .build()
116
    );
117
118
    // Module state
119
    public boolean eating;
120
    private int slot, prevSlot;
121
122
    private final List<Class<? extends Module>> wasAura = new ReferenceArrayList<>();
123
    private boolean wasBaritone = false;
124
125
    public AutoEat() {
126
        super(Categories.Player, "auto-eat", "Automatically eats food.");
127
    }
128
129
    @Override
130
    public void onDeactivate() {
131
        if (eating) stopEating();
132
    }
133
134
    /**
135
     * Main tick handler for the module's eating logic
136
     */
137
    @EventHandler(priority = EventPriority.LOW)
138
    private void onTick(TickEvent.Pre event) {
139
        // Don't eat if AutoGap is already eating
140
        if (mc.player == null) return;
141
        if (Modules.get().get(AutoGap.class).isEating()) return;
142
143
        // case 1: Already eating
144
        if (eating) {
145
            // Stop eating if we shouldn't eat anymore
146
            if (!shouldEat()) {
147
                stopEating();
148
                return;
149
            }
150
151
            // Check if the item in current slot is not food anymore
152
            if (!Utils.isFood(mc.player.getInventory().getItem(slot))) {
153
                int newSlot = findSlot();
154
155
                // Stop if no food found
156
                if (newSlot == -1) {
157
                    stopEating();
158
                    return;
159
                }
160
161
                changeSlot(newSlot);
162
            }
163
164
            // Continue eating the food
165
            eat();
166
            return;
167
        }
168
169
        // case 2: Not eating yet but should start
170
        if (shouldEat()) {
171
            startEating();
172
        }
173
    }
174
175
    @EventHandler
176
    private void onItemUseCrosshairTarget(ItemUseCrosshairTargetEvent event) {
177
        if (eating) event.target = null;
178
    }
179
180
    private void startEating() {
181
        prevSlot = mc.player.getInventory().getSelectedSlot();
182
        eat();
183
        if (!eating) return;
184
185
        // Pause auras
186
        wasAura.clear();
187
        if (pauseAuras.get()) {
188
            for (Class<? extends Module> klass : AURAS) {
189
                Module module = Modules.get().get(klass);
190
191
                if (module.isActive()) {
192
                    wasAura.add(klass);
193
                    module.toggle();
194
                }
195
            }
196
        }
197
198
        // Pause baritone
199
        if (pauseBaritone.get() && PathManagers.get().isPathing() && !wasBaritone) {
200
            wasBaritone = true;
201
            PathManagers.get().pause();
202
        }
203
    }
204
205
    private void eat() {
206
        if (!changeSlot(slot)) return;
207
        setPressed(true);
208
        if (!mc.player.isUsingItem()) Utils.rightClick();
209
210
        eating = true;
211
    }
212
213
    private void stopEating() {
214
        if (prevSlot != SlotUtils.OFFHAND) changeSlot(prevSlot);
215
        setPressed(false);
216
217
        eating = false;
218
219
        // Resume auras
220
        if (pauseAuras.get()) {
221
            for (Class<? extends Module> klass : AURAS) {
222
                if (wasAura.contains(klass)) {
223
                    Modules.get().get(klass).enable();
224
                }
225
            }
226
        }
227
228
        // Resume baritone
229
        if (pauseBaritone.get() && wasBaritone) {
230
            wasBaritone = false;
231
            PathManagers.get().resume();
232
        }
233
    }
234
235
    private void setPressed(boolean pressed) {
236
        mc.options.keyUse.setDown(pressed);
237
    }
238
239
    /**
240
     * Prepares a slot for eating. Uses offhand or hotbar directly.
241
     * Moves a main-inventory item to an empty hotbar slot; returns false if none.
242
     */
243
    private boolean changeSlot(int slot) {
244
        // offhand: use directly
245
        if (slot == SlotUtils.OFFHAND) {
246
            this.slot = SlotUtils.OFFHAND;
247
            return true;
248
        }
249
250
        // hotbar: select
251
        if (SlotUtils.isHotbar(slot)) {
252
            InvUtils.swap(slot, false);
253
            this.slot = slot;
254
            return true;
255
        }
256
257
        // main inventory: move to empty hotbar, abort if none
258
        int emptySlot = InvUtils.find(ItemStack::isEmpty, SlotUtils.HOTBAR_START, SlotUtils.HOTBAR_END).slot();
259
        if (emptySlot == -1) return false;
260
261
        InvUtils.move().from(slot).toHotbar(emptySlot);
262
        InvUtils.swap(emptySlot, false);
263
        this.slot = emptySlot;
264
        return true;
265
    }
266
267
    public boolean shouldEat() {
268
        if (mc.player == null) return false;
269
        boolean healthLow = mc.player.getHealth() <= healthThreshold.get();
270
        boolean hungerLow = mc.player.getFoodData().getFoodLevel() <= hungerThreshold.get();
271
        if (!thresholdMode.get().test(healthLow, hungerLow)) return false;
272
273
        slot = findSlot();
274
        if (slot == -1) return false;
275
276
        ItemStack item = mc.player.getInventory().getItem(slot);
277
        FoodProperties prop = item.get(DataComponents.FOOD);
278
        if (prop == null || !Utils.isFood(item)) return false;
279
280
        return (mc.player.getFoodData().needsFood() || prop.canAlwaysEat());
281
    }
282
283
    /**
284
     * Finds the best slot to eat from, preferring:
285
     * offhand => hotbar => main inventory (if allowed).
286
     */
287
    private int findSlot() {
288
        // prefer offhand
289
        Item offHandItem = mc.player.getOffhandItem().getItem();
290
        if (Utils.isFood(offHandItem) && !blacklist.get().contains(offHandItem)) return SlotUtils.OFFHAND;
291
292
        // if offhand empty, prefer best in hotbar
293
        int slot = findBestFood(SlotUtils.HOTBAR_START, SlotUtils.HOTBAR_END);
294
        if (slot != -1) return slot;
295
296
        // if allowed, search main inventory
297
        if (searchInventory.get()) {
298
            return findBestFood(SlotUtils.MAIN_START, SlotUtils.MAIN_END);
299
        }
300
301
        return -1; // nothing found :(
302
    }
303
304
    private int findBestFood(int start, int end) {
305
        int best = -1;
306
        float bestHunger = -1;
307
308
        for (int i = start; i <= end; i++) {
309
            // Skip if item isn't food
310
            ItemStack stack = mc.player.getInventory().getItem(i);
311
            FoodProperties food = stack.get(DataComponents.FOOD);
312
            if (!Utils.isFood(stack)) continue;
313
            if (food == null) continue;
314
315
            // Skip if item is in blacklist
316
            Item item = stack.getItem();
317
            if (blacklist.get().contains(item)) continue;
318
319
            // Check if hunger value is better
320
            float hunger = prioritise.get().value(food);
321
            if (hunger > bestHunger) {
322
                bestHunger = hunger;
323
                best = i;
324
            }
325
        }
326
327
        return best;
328
    }
329
330
    public enum ThresholdMode {
331
        Health((health, _) -> health),
332
        Hunger((_, hunger) -> hunger),
333
        Any((health, hunger) -> health || hunger),
334
        Both((health, hunger) -> health && hunger);
335
336
        private final BooleanBinaryOperator predicate;
337
338
        ThresholdMode(BooleanBinaryOperator predicate) {
339
            this.predicate = predicate;
340
        }
341
342
        public boolean test(boolean health, boolean hunger) {
343
            return predicate.apply(health, hunger);
344
        }
345
    }
346
347
    public enum Priority {
348
        Combined,
349
        Hunger,
350
        Saturation;
351
352
        public float value(FoodProperties food) {
353
            return switch (this) {
354
                case Combined -> food.nutrition() + food.saturation();
355
                case Hunger -> food.nutrition();
356
                case Saturation -> food.saturation();
357
            };
358
        }
359
    }
360
}
361