Class InventoryUtils

java.lang.Object
net.xun.lib.common.api.util.InventoryUtils

public class InventoryUtils extends Object
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 Type
    Method
    Description
    static 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 void
    extractItems(net.minecraft.world.Container container, InventoryPredicate predicate, int amount, @Nullable SlotRange slots, InventoryCycleOrder order)
    Removes items from a container with slot priority control.
    static void
    extractSingleItem(net.minecraft.world.Container container, InventoryPredicate predicate, SlotRange slots, InventoryCycleOrder order)
    Removes a single item from the specified slot range.
    static int
    findFirstMatchingSlot(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 int
    getAvailableSpace(net.minecraft.world.Container container)
    Calculates total available space of a container
    static net.minecraft.world.item.ItemStack
    getItemInSlot(net.minecraft.world.Container container, int slotIndex)
    Get the item of the specific slot in the container
    static boolean
    hasItem(net.minecraft.world.Container container, InventoryPredicate predicate, @Nullable SlotRange slots)
    Checks if a container has at least one item matching the predicate.
    static boolean
    hasItemCount(net.minecraft.world.Container container, InventoryPredicate predicate, int minCount, @Nullable SlotRange slots)
    Checks if a container contains at least minCount items matching the predicate.
    static void
    insertAndDiscardOverflow(net.minecraft.world.Container container, net.minecraft.world.item.ItemStack stack)
    Adds items to a container and permanently discards any overflow
    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.
    static void
    validateContainer(net.minecraft.world.Container container)
     
    static void
    validateContainer(net.minecraft.world.Container container, boolean allowClientSide)
    Validates container accessibility and integrity.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 least minCount items matching the predicate.
      Parameters:
      container - Any inventory (player, chest, etc.)
      predicate - Item matching logic
      minCount - 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 inventory
      predicate - Item matching logic
      slots - 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 search
      predicate - The predicate to test items
      slots - 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 inventory
      slotIndex - 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 from
      predicate - Predicate to match items
      amount - Maximum number of items to remove
      slots - Optional slot range restriction
      order - 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 inventory
      predicate - Item matching logic
      slots - Slot range to search
      order - 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 inventory
      stack - 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 inventory
      stack - 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 search
      predicate - Predicate to test items
      slots - 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 validate
      allowClientSide - Allow client-side container access
      Throws:
      NullPointerException - if container is null
      IllegalStateException - if client-side modifications are disallowed
      IllegalArgumentException - for invalid container states
    • validateContainer

      public static void validateContainer(net.minecraft.world.Container container)