1212import meteordevelopment .meteorclient .systems .modules .Modules ;
1313import meteordevelopment .meteorclient .systems .modules .combat .AutoTotem ;
1414import meteordevelopment .meteorclient .utils .player .InvUtils ;
15+ import meteordevelopment .meteorclient .utils .player .SlotUtils ;
1516import meteordevelopment .orbit .EventHandler ;
1617import net .minecraft .item .Item ;
1718import net .minecraft .item .ItemStack ;
@@ -129,32 +130,38 @@ private void checkSlot(int slot, ItemStack stack) {
129130 ItemStack prevStack = items [slot ];
130131 items [slot ] = stack .copy ();
131132
133+ if (slot == 9 ) slot = SlotUtils .OFFHAND ;
134+
132135 if (excludedItems .get ().contains (stack .getItem ())) return ;
133136 if (excludedItems .get ().contains (prevStack .getItem ())) return ;
134137
135138 int fromSlot = -1 ;
136139
137140 // If there are still items left in the stack, but it just crossed the threshold
138141 if (stack .isStackable () && !stack .isEmpty () && stack .getCount () <= minCount .get ()) {
139- fromSlot = findItem (stack , slot , minCount .get () - stack .getCount () + 1 );
142+ fromSlot = findItem (stack , slot , minCount .get () - stack .getCount () + 1 , true );
140143 }
141144
142145 // If the stack just went from above the threshold to empty in a single tick
143146 // this can happen if the threshold is set low enough while using modules that
144147 // place many blocks per tick, like surround or holefiller
145148 if (prevStack .isStackable () && stack .isEmpty () && !prevStack .isEmpty ()) {
146- fromSlot = findItem (prevStack , slot , minCount .get () - stack .getCount () + 1 );
149+ fromSlot = findItem (prevStack , slot , minCount .get () - stack .getCount () + 1 , false );
147150 }
148151
149152 // Unstackable items
150153 if (unstackable .get () && !prevStack .isStackable () && stack .isEmpty () && !prevStack .isEmpty ()) {
151- fromSlot = findItem (prevStack , slot , 1 );
154+ fromSlot = findItem (prevStack , slot , 1 , false );
152155 }
153156
157+ // eliminate occasional loops when moving items from hotbar to itself
158+ if (fromSlot == mc .player .getInventory ().getSelectedSlot () || fromSlot == SlotUtils .OFFHAND ) return ;
159+ if (fromSlot < 9 && fromSlot < slot && slot != mc .player .getInventory ().getSelectedSlot () && slot != SlotUtils .OFFHAND ) return ;
160+
154161 InvUtils .move ().from (fromSlot ).to (slot );
155162 }
156163
157- private int findItem (ItemStack lookForStack , int excludedSlot , int goodEnoughCount ) {
164+ private int findItem (ItemStack lookForStack , int excludedSlot , int goodEnoughCount , boolean mustCombine ) {
158165 int slot = -1 ;
159166 int count = 0 ;
160167
@@ -164,6 +171,7 @@ private int findItem(ItemStack lookForStack, int excludedSlot, int goodEnoughCou
164171 ItemStack stack = mc .player .getInventory ().getStack (i );
165172 if (stack .getItem () != lookForStack .getItem ()) continue ;
166173
174+ if (mustCombine && !ItemStack .areItemsAndComponentsEqual (lookForStack , stack )) continue ;
167175 if (sameEnchants .get () && !stack .getEnchantments ().equals (lookForStack .getEnchantments ())) continue ;
168176
169177 if (stack .getCount () > count ) {
0 commit comments