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:
  • 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 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
    • hasItemCount

      public static boolean hasItemCount(net.minecraft.world.Container container, InventoryPredicate predicate, int minCount, InventorySection section)
      Checks if a container's section contains at least minCount matching items.
      Parameters:
      container - Target inventory
      predicate - Item matching logic
      minCount - 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 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
    • 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 inventory
      predicate - Item matching logic
      section - 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 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
    • 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 inventory
      predicate - Item matching logic
      section - 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 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
    • 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 inventory
      predicate - Item matching logic
      amount - Maximum items to remove
      section - Section to remove from
      order - 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 inventory
      predicate - Item matching logic
      slots - Slot range to search
      order - 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 inventory
      predicate - Item matching logic
      section - Section to remove from
      order - 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 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
    • 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 inventory
      predicate - Item matching logic
      section - 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 check
      stack - 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 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)