2525import net .minecraft .component .DataComponentTypes ;
2626import net .minecraft .component .type .FoodComponent ;
2727import net .minecraft .item .Item ;
28+ import net .minecraft .item .ItemStack ;
2829import net .minecraft .item .Items ;
2930
3031import java .util .List ;
@@ -71,6 +72,13 @@ public class AutoEat extends Module {
7172 .build ()
7273 );
7374
75+ private final Setting <Boolean > searchInventory = sgGeneral .add (new BoolSetting .Builder ()
76+ .name ("search-inventory" )
77+ .description ("Search the full inventory for food, not only the hotbar." )
78+ .defaultValue (false )
79+ .build ()
80+ );
81+
7482 // Threshold
7583 private final Setting <ThresholdMode > thresholdMode = sgThreshold .add (new EnumSetting .Builder <ThresholdMode >()
7684 .name ("threshold-mode" )
@@ -185,7 +193,7 @@ private void startEating() {
185193 }
186194
187195 private void eat () {
188- changeSlot (slot );
196+ if (! changeSlot (slot )) return ;
189197 setPressed (true );
190198 if (!mc .player .isUsingItem ()) Utils .rightClick ();
191199
@@ -218,11 +226,35 @@ private void setPressed(boolean pressed) {
218226 mc .options .useKey .setPressed (pressed );
219227 }
220228
221- private void changeSlot (int slot ) {
222- InvUtils .swap (slot , false );
223- this .slot = slot ;
229+ /**
230+ * Prepares a slot for eating. Uses offhand or hotbar directly.
231+ * Moves a main-inventory item to an empty hotbar slot; returns false if none.
232+ */
233+ private boolean changeSlot (int slot ) {
234+ // offhand: use directly
235+ if (slot == SlotUtils .OFFHAND ) {
236+ this .slot = SlotUtils .OFFHAND ;
237+ return true ;
238+ }
239+
240+ // hotbar: select
241+ if (SlotUtils .isHotbar (slot )) {
242+ InvUtils .swap (slot , false );
243+ this .slot = slot ;
244+ return true ;
245+ }
246+
247+ // main: move to empty hotbar, abort if none
248+ var hotbarIndex = InvUtils .find (ItemStack ::isEmpty , SlotUtils .HOTBAR_START , SlotUtils .HOTBAR_END ).slot ();
249+ if (hotbarIndex == -1 ) return false ;
250+
251+ InvUtils .move ().fromMain (slot - SlotUtils .MAIN_START ).toHotbar (hotbarIndex );
252+ InvUtils .swap (hotbarIndex , false );
253+ this .slot = hotbarIndex ;
254+ return true ;
224255 }
225256
257+
226258 public boolean shouldEat () {
227259 boolean healthLow = mc .player .getHealth () <= healthThreshold .get ();
228260 boolean hungerLow = mc .player .getHungerManager ().getFoodLevel () <= hungerThreshold .get ();
@@ -240,24 +272,28 @@ private int findSlot() {
240272 int slot = -1 ;
241273 int bestHunger = -1 ;
242274
243- for (int i = 0 ; i < 9 ; i ++) {
275+ // search hotbar only, or hotbar + inventory if enabled
276+ int end = searchInventory .get () ? SlotUtils .MAIN_END : SlotUtils .HOTBAR_END ;
277+
278+ for (int i = SlotUtils .HOTBAR_START ; i <= end ; i ++) {
244279 // Skip if item isn't food
245280 Item item = mc .player .getInventory ().getStack (i ).getItem ();
246281 FoodComponent foodComponent = item .getComponents ().get (DataComponentTypes .FOOD );
247282 if (foodComponent == null ) continue ;
248283
284+ // Skip if item is in blacklist
285+ if (blacklist .get ().contains (item )) continue ;
286+
249287 // Check if hunger value is better
250288 int hunger = foodComponent .nutrition ();
251289 if (hunger > bestHunger ) {
252- // Skip if item is in blacklist
253- if (blacklist .get ().contains (item )) continue ;
254-
255290 // Select the current item
256291 slot = i ;
257292 bestHunger = hunger ;
258293 }
259294 }
260295
296+ // Offhand check
261297 Item offHandItem = mc .player .getOffHandStack ().getItem ();
262298 FoodComponent offHandFood = offHandItem .getComponents ().get (DataComponentTypes .FOOD );
263299 if (offHandFood != null && !blacklist .get ().contains (offHandItem ) && offHandFood .nutrition () > bestHunger ) {
0 commit comments