0
0
mirror of https://github.com/Wurst-Imperium/Wurst7.git synced 2024-09-19 17:02:13 +02:00

Add count method to InventoryUtils

This commit is contained in:
Alexander01998 2024-05-20 15:19:38 +02:00
parent d619c2aa50
commit d11b0d67b5

View File

@ -67,6 +67,59 @@ public enum InventoryUtils
*/
public static int indexOf(Predicate<ItemStack> predicate, int maxInvSlot,
boolean includeOffhand)
{
return getMatchingSlots(predicate, maxInvSlot, includeOffhand)
.findFirst().orElse(-1);
}
public static int count(Item item)
{
return count(stack -> stack.isOf(item), 36, false);
}
public static int count(Item item, int maxInvSlot)
{
return count(stack -> stack.isOf(item), maxInvSlot, false);
}
public static int count(Item item, int maxInvSlot, boolean includeOffhand)
{
return count(stack -> stack.isOf(item), maxInvSlot, includeOffhand);
}
public static int count(Predicate<ItemStack> predicate)
{
return count(predicate, 36, false);
}
public static int count(Predicate<ItemStack> predicate, int maxInvSlot)
{
return count(predicate, maxInvSlot, false);
}
/**
* Counts the number of items in the player's inventory that match the given
* predicate, searching from slot 0 to {@code maxInvSlot-1}.
*
* @param predicate
* checks if an item should be counted
* @param maxInvSlot
* the maximum slot to search (exclusive), usually 9 for the
* hotbar or 36 for the whole inventory
* @param includeOffhand
* also search the offhand (slot 40), even if maxInvSlot is lower
* @return
* the number of matching items in the player's inventory
*/
public static int count(Predicate<ItemStack> predicate, int maxInvSlot,
boolean includeOffhand)
{
return (int)getMatchingSlots(predicate, maxInvSlot, includeOffhand)
.count();
}
private static IntStream getMatchingSlots(Predicate<ItemStack> predicate,
int maxInvSlot, boolean includeOffhand)
{
PlayerInventory inventory = MC.player.getInventory();
@ -75,11 +128,8 @@ public enum InventoryUtils
if(includeOffhand)
stream = IntStream.concat(stream, IntStream.of(40));
// find the slot of the item we want
int slot = stream.filter(i -> predicate.test(inventory.getStack(i)))
.findFirst().orElse(-1);
return slot;
// filter out the slots we don't want
return stream.filter(i -> predicate.test(inventory.getStack(i)));
}
public static boolean selectItem(Item item)