Package net.xun.lib.common.api.util
Class InventoryUtils
java.lang.Object
net.xun.lib.common.api.util.InventoryUtils
Universal inventory utilities for all container types. (sever-side only)
This class offers static methods for common inventory utils including:
- Item quantity checks
- Slot searching and item removal
- Inventory space verification
- Item collection with custom predicates
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic com.google.common.collect.ImmutableList<net.minecraft.world.item.ItemStack> collectMatching(net.minecraft.world.Container container, InventoryPredicate predicate, @Nullable SlotRange slots) Collects copies of all item stacks matching the predicate.static voidextractItems(net.minecraft.world.Container container, InventoryPredicate predicate, int amount, @Nullable SlotRange slots, InventoryCycleOrder order) Removes items from a container with slot priority control.static voidextractSingleItem(net.minecraft.world.Container container, InventoryPredicate predicate, SlotRange slots, InventoryCycleOrder order) Removes a single item from the specified slot range.static intfindFirstMatchingSlot(net.minecraft.world.Container container, InventoryPredicate predicate, @Nullable SlotRange slots) Finds the first slot in the container that contains an item matching the predicate.static intgetAvailableSpace(net.minecraft.world.Container container) Calculates total available space of a containerstatic net.minecraft.world.item.ItemStackgetItemInSlot(net.minecraft.world.Container container, int slotIndex) Get the item of the specific slot in the containerstatic booleanhasItem(net.minecraft.world.Container container, InventoryPredicate predicate, @Nullable SlotRange slots) Checks if a container has at least one item matching the predicate.static booleanhasItemCount(net.minecraft.world.Container container, InventoryPredicate predicate, int minCount, @Nullable SlotRange slots) Checks if a container contains at leastminCountitems matching the predicate.static voidinsertAndDiscardOverflow(net.minecraft.world.Container container, net.minecraft.world.item.ItemStack stack) Adds items to a container and permanently discards any overflowstatic net.minecraft.world.item.ItemStackinsertItem(net.minecraft.world.Container container, net.minecraft.world.item.ItemStack stack) Attempts to add an item stack to a container.static voidvalidateContainer(net.minecraft.world.Container container) static voidvalidateContainer(net.minecraft.world.Container container, boolean allowClientSide) Validates container accessibility and integrity.
-
Method Details
-
hasItemCount
public static boolean hasItemCount(net.minecraft.world.Container container, InventoryPredicate predicate, int minCount, @Nullable @Nullable SlotRange slots) Checks if a container contains at leastminCountitems matching the predicate.- Parameters:
container- Any inventory (player, chest, etc.)predicate- Item matching logicminCount- Minimum required items (≥1)slots- Optional slot range (null for entire inventory)- Returns:
- True if container contains sufficient matching items, false otherwise
- Throws:
NullPointerException- if container or predicate is null
-
hasItem
public static boolean hasItem(net.minecraft.world.Container container, InventoryPredicate predicate, @Nullable @Nullable SlotRange slots) Checks if a container has at least one item matching the predicate.- Parameters:
container- Target inventorypredicate- Item matching logicslots- Slot range to check (null for entire inventory)- Returns:
- True if at least one matching item exists
- Throws:
NullPointerException- if container or predicate is null
-
findFirstMatchingSlot
public static int findFirstMatchingSlot(net.minecraft.world.Container container, InventoryPredicate predicate, @Nullable @Nullable SlotRange slots) Finds the first slot in the container that contains an item matching the predicate.- Parameters:
container- The container to searchpredicate- The predicate to test itemsslots- Optional slot range to restrict search (null for entire container)- Returns:
- Slot index of first match, or -1 if none
- Throws:
NullPointerException- if container or predicate is null
-
getItemInSlot
public static net.minecraft.world.item.ItemStack getItemInSlot(net.minecraft.world.Container container, int slotIndex) Get the item of the specific slot in the container- Parameters:
container- Target inventoryslotIndex- The slot index- Returns:
- ItemStack in the specific slot of the container
- See Also:
-
extractItems
public static void extractItems(net.minecraft.world.Container container, InventoryPredicate predicate, int amount, @Nullable @Nullable SlotRange slots, InventoryCycleOrder order) Removes items from a container with slot priority control.- Parameters:
container- Container to remove frompredicate- Predicate to match itemsamount- Maximum number of items to removeslots- Optional slot range restrictionorder- Slot processing order strategy- Throws:
NullPointerException- if container, predicate, or order is null
-
extractSingleItem
public static void extractSingleItem(net.minecraft.world.Container container, InventoryPredicate predicate, SlotRange slots, InventoryCycleOrder order) Removes a single item from the specified slot range.- Parameters:
container- Target inventorypredicate- Item matching logicslots- Slot range to searchorder- Slot processing order- Throws:
NullPointerException- if any parameter is null
-
insertItem
public static net.minecraft.world.item.ItemStack insertItem(net.minecraft.world.Container container, net.minecraft.world.item.ItemStack stack) Attempts to add an item stack to a container.- Parameters:
container- Target inventorystack- Item stack to add (will not be modified)- Returns:
- Remaining items that couldn't be added (empty stack if all were added)
- Throws:
NullPointerException- if container or stack is null
-
insertAndDiscardOverflow
public static void insertAndDiscardOverflow(net.minecraft.world.Container container, net.minecraft.world.item.ItemStack stack) Adds items to a container and permanently discards any overflow- Parameters:
container- Target inventorystack- Item stack to add (will not be modified)- Throws:
NullPointerException- if container or stack is null
-
collectMatching
public static com.google.common.collect.ImmutableList<net.minecraft.world.item.ItemStack> collectMatching(net.minecraft.world.Container container, InventoryPredicate predicate, @Nullable @Nullable SlotRange slots) Collects copies of all item stacks matching the predicate.- Parameters:
container- Container to searchpredicate- Predicate to test itemsslots- Optional slot range restriction- Returns:
- Immutable list of matching item copies
- Throws:
NullPointerException- if container or predicate is null
-
getAvailableSpace
public static int getAvailableSpace(net.minecraft.world.Container container) Calculates total available space of a container- Parameters:
container- Container to check- Returns:
- The number of empty slots
- Throws:
NullPointerException- if container or stack is null
-
validateContainer
public static void validateContainer(net.minecraft.world.Container container, boolean allowClientSide) Validates container accessibility and integrity.- Parameters:
container- Container to validateallowClientSide- Allow client-side container access- Throws:
NullPointerException- if container is nullIllegalStateException- if client-side modifications are disallowedIllegalArgumentException- for invalid container states
-
validateContainer
public static void validateContainer(net.minecraft.world.Container container)
-