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:
-
Constructor Summary
Constructors -
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 com.google.common.collect.ImmutableList<net.minecraft.world.item.ItemStack> collectMatching(net.minecraft.world.Container container, InventoryPredicate predicate, InventorySection section) Collects copies of items from a container section 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 voidextractItems(net.minecraft.world.Container container, InventoryPredicate predicate, int amount, InventorySection section, InventoryCycleOrder order) Removes items from a specific container section.static voidextractSingleItem(net.minecraft.world.Container container, InventoryPredicate predicate, InventorySection section, InventoryCycleOrder order) Removes a single item from a container section.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 intfindFirstMatchingSlot(net.minecraft.world.Container container, InventoryPredicate predicate, InventorySection section) Finds the first slot in a container section with a matching item.static intgetAvailableSpace(net.minecraft.world.Container container, net.minecraft.world.item.ItemStack stack) Calculates total available space for a specific item type.static booleanhasItem(net.minecraft.world.Container container, InventoryPredicate predicate, @Nullable SlotRange slots) Checks if a container has at least one item matching the predicate.static booleanhasItem(net.minecraft.world.Container container, InventoryPredicate predicate, InventorySection section) Checks if a container's section contains at least one matching item.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 booleanhasItemCount(net.minecraft.world.Container container, InventoryPredicate predicate, int minCount, InventorySection section) Checks if a container's section contains at leastminCountmatching items.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.ItemStackinsetItem(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.
-
Constructor Details
-
InventoryUtils
public InventoryUtils()
-
-
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
-
hasItemCount
public static boolean hasItemCount(net.minecraft.world.Container container, InventoryPredicate predicate, int minCount, InventorySection section) Checks if a container's section contains at leastminCountmatching items.- Parameters:
container- Target inventorypredicate- Item matching logicminCount- Minimum required items (≥1)section- Inventory section to check- Returns:
- True if section contains sufficient items, false otherwise
- Throws:
NullPointerException- if any parameter 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
-
hasItem
public static boolean hasItem(net.minecraft.world.Container container, InventoryPredicate predicate, InventorySection section) Checks if a container's section contains at least one matching item.- Parameters:
container- Target inventorypredicate- Item matching logicsection- Inventory section to check- Returns:
- True if section contains at least one matching item
- Throws:
NullPointerException- if any parameter 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
-
findFirstMatchingSlot
public static int findFirstMatchingSlot(net.minecraft.world.Container container, InventoryPredicate predicate, InventorySection section) Finds the first slot in a container section with a matching item.- Parameters:
container- Target inventorypredicate- Item matching logicsection- Section to search within- Returns:
- Slot index of first match, or -1 if none
- Throws:
NullPointerException- if any parameter is null
-
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
-
extractItems
public static void extractItems(net.minecraft.world.Container container, InventoryPredicate predicate, int amount, InventorySection section, InventoryCycleOrder order) Removes items from a specific container section.- Parameters:
container- Target inventorypredicate- Item matching logicamount- Maximum items to removesection- Section to remove fromorder- Slot processing order- Throws:
NullPointerException- if any parameter 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
-
extractSingleItem
public static void extractSingleItem(net.minecraft.world.Container container, InventoryPredicate predicate, InventorySection section, InventoryCycleOrder order) Removes a single item from a container section.- Parameters:
container- Target inventorypredicate- Item matching logicsection- Section to remove fromorder- Slot processing order- Throws:
NullPointerException- if any parameter is null
-
insetItem
public static net.minecraft.world.item.ItemStack insetItem(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
-
collectMatching
public static com.google.common.collect.ImmutableList<net.minecraft.world.item.ItemStack> collectMatching(net.minecraft.world.Container container, InventoryPredicate predicate, InventorySection section) Collects copies of items from a container section matching the predicate.- Parameters:
container- Target inventorypredicate- Item matching logicsection- Section to search- Returns:
- Immutable list of matching item copies
- Throws:
NullPointerException- if any parameter is null
-
getAvailableSpace
public static int getAvailableSpace(net.minecraft.world.Container container, net.minecraft.world.item.ItemStack stack) Calculates total available space for a specific item type.- Parameters:
container- Container to checkstack- Item type to check space for- Returns:
- Total number of items that can be added
- 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)
-