Skip to content

Commit 126324b

Browse files
uxmlenchri-k
authored andcommitted
fix: AutoEat forcing slot until thresholds satisfied (MeteorDevelopment#5380)
(cherry picked from commit 13e10f8)
1 parent 118d6f3 commit 126324b

File tree

1 file changed

+44
-39
lines changed
  • src/main/java/meteordevelopment/meteorclient/systems/modules/player

1 file changed

+44
-39
lines changed

src/main/java/meteordevelopment/meteorclient/systems/modules/player/AutoEat.java

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232

3333
public class AutoEat extends Module {
3434
@SuppressWarnings("unchecked")
35-
private static final Class<? extends Module>[] AURAS = new Class[]{KillAura.class, CrystalAura.class, AnchorAura.class, BedAura.class};
35+
private static final Class<? extends Module>[] AURAS = new Class[]{ KillAura.class, CrystalAura.class, AnchorAura.class, BedAura.class };
3636

37+
// Settings groups
3738
private final SettingGroup sgGeneral = settings.getDefaultGroup();
3839
private final SettingGroup sgThreshold = settings.createGroup("Threshold");
3940

4041
// General
41-
4242
public final Setting<List<Item>> blacklist = sgGeneral.add(new ItemListSetting.Builder()
4343
.name("blacklist")
4444
.description("Which items to not eat.")
@@ -72,7 +72,6 @@ public class AutoEat extends Module {
7272
);
7373

7474
// Threshold
75-
7675
private final Setting<ThresholdMode> thresholdMode = sgThreshold.add(new EnumSetting.Builder<ThresholdMode>()
7776
.name("threshold-mode")
7877
.description("The threshold mode to trigger auto eat.")
@@ -100,6 +99,7 @@ public class AutoEat extends Module {
10099
.build()
101100
);
102101

102+
// Module state
103103
public boolean eating;
104104
private int slot, prevSlot;
105105

@@ -115,46 +115,43 @@ public void onDeactivate() {
115115
if (eating) stopEating();
116116
}
117117

118+
/**
119+
* Main tick handler for the module's eating logic
120+
*/
118121
@EventHandler(priority = EventPriority.LOW)
119122
private void onTick(TickEvent.Pre event) {
120-
// Skip if Auto Gap is already eating
123+
// Don't eat if AutoGap is already eating
121124
if (Modules.get().get(AutoGap.class).isEating()) return;
122125

126+
// case 1: Already eating
123127
if (eating) {
124-
// If we are eating check if we should still be eating
125-
if (shouldEat()) {
126-
// Check if the item in current slot is not food
127-
if (mc.player.getInventory().getStack(slot).get(DataComponentTypes.FOOD) != null) {
128-
// If not try finding a new slot
129-
int slot = findSlot();
130-
131-
// If no valid slot was found then stop eating
132-
if (slot == -1) {
133-
stopEating();
134-
return;
135-
}
136-
// Otherwise change to the new slot
137-
else {
138-
changeSlot(slot);
139-
}
140-
}
141-
142-
// Continue eating
143-
eat();
144-
}
145-
// If we shouldn't be eating anymore then stop
146-
else {
128+
// Stop eating if we shouldn't eat anymore
129+
if (!shouldEat()) {
147130
stopEating();
131+
return;
148132
}
149-
} else {
150-
// If we are not eating check if we should start eating
151-
if (shouldEat()) {
152-
// Try to find a valid slot
153-
slot = findSlot();
154-
155-
// If slot was found then start eating
156-
if (slot != -1) startEating();
133+
134+
// Check if the item in current slot is not food anymore
135+
if (mc.player.getInventory().getStack(slot).get(DataComponentTypes.FOOD) == null) {
136+
int newSlot = findSlot();
137+
138+
// Stop if no food found
139+
if (newSlot == -1) {
140+
stopEating();
141+
return;
142+
}
143+
144+
changeSlot(newSlot);
157145
}
146+
147+
// Continue eating the food
148+
eat();
149+
return;
150+
}
151+
152+
// case 2: Not eating yet but should start
153+
if (shouldEat()) {
154+
startEating();
158155
}
159156
}
160157

@@ -229,10 +226,16 @@ private void changeSlot(int slot) {
229226
}
230227

231228
public boolean shouldEat() {
232-
boolean health = mc.player.getHealth() <= healthThreshold.get();
233-
boolean hunger = mc.player.getHungerManager().getFoodLevel() <= hungerThreshold.get();
229+
boolean healthLow = mc.player.getHealth() <= healthThreshold.get();
230+
boolean hungerLow = mc.player.getHungerManager().getFoodLevel() <= hungerThreshold.get();
231+
slot = findSlot();
232+
if (slot == -1) return false;
233+
234+
FoodComponent food = mc.player.getInventory().getStack(slot).get(DataComponentTypes.FOOD);
235+
if (food == null) return false;
234236

235-
return thresholdMode.get().test(health, hunger);
237+
return thresholdMode.get().test(healthLow, hungerLow)
238+
&& (mc.player.getHungerManager().isNotFull() || food.canAlwaysEat());
236239
}
237240

238241
private int findSlot() {
@@ -258,8 +261,10 @@ private int findSlot() {
258261
}
259262

260263
Item offHandItem = mc.player.getOffHandStack().getItem();
261-
if (offHandItem.getComponents().get(DataComponentTypes.FOOD) != null && !blacklist.get().contains(offHandItem) && offHandItem.getComponents().get(DataComponentTypes.FOOD).nutrition() > bestHunger)
264+
FoodComponent offHandFood = offHandItem.getComponents().get(DataComponentTypes.FOOD);
265+
if (offHandFood != null && !blacklist.get().contains(offHandItem) && offHandFood.nutrition() > bestHunger) {
262266
slot = SlotUtils.OFFHAND;
267+
}
263268

264269
return slot;
265270
}

0 commit comments

Comments
 (0)