11package blazingtwist .itemcounts .config ;
22
3+ import it .unimi .dsi .fastutil .ints .Int2ObjectMap ;
34import me .shedaniel .autoconfig .ConfigData ;
45import me .shedaniel .autoconfig .annotation .Config ;
56import me .shedaniel .autoconfig .annotation .ConfigEntry ;
67import net .minecraft .client .util .InputUtil ;
8+ import net .minecraft .entity .EquipmentSlot ;
79import net .minecraft .entity .player .PlayerEntity ;
810import net .minecraft .entity .player .PlayerInventory ;
911import net .minecraft .item .ItemStack ;
1012
11- import java .util .Collection ;
1213import java .util .stream .IntStream ;
1314import java .util .stream .Stream ;
1415
@@ -177,6 +178,12 @@ public HudColors() {
177178 }
178179
179180 public static class ItemCountSeparationRules {
181+ // still better than hard-coding '40' and getting sneaky errors when they inevitably change this again...
182+ public static final int offHandSlotIdx = PlayerInventory .EQUIPMENT_SLOTS .int2ObjectEntrySet ().stream ()
183+ .filter (e -> e .getValue () == EquipmentSlot .OFFHAND )
184+ .map (Int2ObjectMap .Entry ::getIntKey )
185+ .findFirst ().orElse (0 );
186+
180187 @ AutoConfigConstructor
181188 public ItemCountSeparationRules () {
182189 }
@@ -193,18 +200,18 @@ public ItemCountSeparationRules(boolean separateMetadata, boolean separateName,
193200
194201 public int getTotalItemCount (PlayerEntity player , ItemStack stack ) {
195202 PlayerInventory inventory = player .getInventory ();
196- return Stream .of (inventory .main , inventory .offHand , inventory .armor )
197- .flatMap (Collection ::stream )
203+ // Thank you Mojank for this crap. Your previous model was perfectly descriptive.
204+ return IntStream .range (0 , inventory .getMainStacks ().size () + EquipmentSlot .values ().length )
205+ .mapToObj (inventory ::getStack )
198206 .filter (other -> mergeStackCounts (stack , other ))
199207 .mapToInt (ItemStack ::getCount )
200208 .sum ();
201209 }
202210
203211 public int getHotbarItemCount (PlayerEntity player , ItemStack stack ) {
204- Stream <ItemStack > hotbarStacks = IntStream .range (0 , 9 )
205- .mapToObj (i -> player .getInventory ().main .get (i ));
206-
207- return Stream .concat (hotbarStacks , player .getInventory ().offHand .stream ())
212+ PlayerInventory inventory = player .getInventory ();
213+ return Stream .concat (IntStream .range (0 , 9 ).boxed (), IntStream .of (offHandSlotIdx ).boxed ())
214+ .map (inventory ::getStack )
208215 .filter (other -> mergeStackCounts (stack , other ))
209216 .mapToInt (ItemStack ::getCount )
210217 .sum ();
0 commit comments