@@ -244,17 +244,16 @@ private boolean changeSlot(int slot) {
244244 return true ;
245245 }
246246
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 ;
247+ // main inventory move to empty hotbar, abort if none
248+ var emptySlot = InvUtils .find (ItemStack ::isEmpty , SlotUtils .HOTBAR_START , SlotUtils .HOTBAR_END ).slot ();
249+ if (emptySlot == -1 ) return false ;
250250
251- InvUtils .move ().fromMain (slot - SlotUtils .MAIN_START ).toHotbar (hotbarIndex );
252- InvUtils .swap (hotbarIndex , false );
253- this .slot = hotbarIndex ;
251+ InvUtils .move ().fromMain (slot - SlotUtils .MAIN_START ).toHotbar (emptySlot );
252+ InvUtils .swap (emptySlot , false );
253+ this .slot = emptySlot ;
254254 return true ;
255255 }
256256
257-
258257 public boolean shouldEat () {
259258 boolean healthLow = mc .player .getHealth () <= healthThreshold .get ();
260259 boolean hungerLow = mc .player .getHungerManager ().getFoodLevel () <= hungerThreshold .get ();
@@ -265,44 +264,50 @@ public boolean shouldEat() {
265264 if (food == null ) return false ;
266265
267266 return thresholdMode .get ().test (healthLow , hungerLow )
268- && (mc .player .getHungerManager ().isNotFull () || food .canAlwaysEat ());
267+ && (mc .player .getHungerManager ().isNotFull () || food .canAlwaysEat ());
269268 }
270269
271270 private int findSlot () {
272- int slot = -1 ;
273- int bestHunger = -1 ;
271+ // prefer best in hotbar
272+ int slot = findBestInRange (SlotUtils .HOTBAR_START , SlotUtils .HOTBAR_END );
273+ if (slot != -1 ) return slot ;
274+
275+ // if hotbar empty, prefer offhand
276+ Item offHandItem = mc .player .getOffHandStack ().getItem ();
277+ FoodComponent offHandFood = offHandItem .getComponents ().get (DataComponentTypes .FOOD );
278+ if (offHandFood != null && !blacklist .get ().contains (offHandItem )) return SlotUtils .OFFHAND ;
274279
275- // search hotbar only, or hotbar + inventory if enabled
276- int end = searchInventory .get () ? SlotUtils .MAIN_END : SlotUtils .HOTBAR_END ;
280+ // if allowed, search main inventory
281+ if (searchInventory .get ()) {
282+ return findBestInRange (SlotUtils .MAIN_START , SlotUtils .MAIN_END );
283+ }
277284
278- for (int i = SlotUtils .HOTBAR_START ; i <= end ; i ++) {
279- // Skip if item isn't food
280- Item item = mc .player .getInventory ().getStack (i ).getItem ();
281- FoodComponent foodComponent = item .getComponents ().get (DataComponentTypes .FOOD );
282- if (foodComponent == null ) continue ;
285+ return -1 ; // nothing found
286+ }
283287
284- // Skip if item is in blacklist
288+ private int findBestInRange (int start , int end ) {
289+ int best = -1 ;
290+ int bestHunger = -1 ;
291+
292+ for (int i = start ; i <= end ; i ++) {
293+ var stack = mc .player .getInventory ().getStack (i );
294+ var food = stack .get (DataComponentTypes .FOOD );
295+ if (food == null ) continue ;
296+
297+ Item item = stack .getItem ();
285298 if (blacklist .get ().contains (item )) continue ;
286299
287- // Check if hunger value is better
288- int hunger = foodComponent .nutrition ();
300+ int hunger = food .nutrition ();
289301 if (hunger > bestHunger ) {
290- // Select the current item
291- slot = i ;
292302 bestHunger = hunger ;
303+ best = i ;
293304 }
294305 }
295306
296- // Offhand check
297- Item offHandItem = mc .player .getOffHandStack ().getItem ();
298- FoodComponent offHandFood = offHandItem .getComponents ().get (DataComponentTypes .FOOD );
299- if (offHandFood != null && !blacklist .get ().contains (offHandItem ) && offHandFood .nutrition () > bestHunger ) {
300- slot = SlotUtils .OFFHAND ;
301- }
302-
303- return slot ;
307+ return best ;
304308 }
305309
310+
306311 public enum ThresholdMode {
307312 Health ((health , hunger ) -> health ),
308313 Hunger ((health , hunger ) -> hunger ),
0 commit comments