Class PlayerInventoryUtils

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

public class PlayerInventoryUtils extends Object
Specific utilities for managing player inventory, disable client-side by default

Methods for operating player inventory including:

  • Checking and managing items in player's hands
  • Searching and removing items from specific inventory sections
  • Swapping or clearing items in hands
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addItemToHands(@NotNull net.minecraft.world.entity.player.Player player, @NotNull net.minecraft.world.item.ItemStack item)
    Attempts to add the item to the player's main hand or offhand, whichever is empty first.
    static void
    clearBothHands(@NotNull net.minecraft.world.entity.player.Player player)
    Clears both the main hand and offhand of the player.
    static com.google.common.collect.ImmutableList<net.minecraft.world.item.ItemStack>
    collectMatching(net.minecraft.world.Container container, InventoryPredicate predicate, PlayerInventorySection section)
    Collects copies of items from an inventory section matching the predicate.
    static void
    extractItems(net.minecraft.world.entity.player.Player player, InventoryPredicate predicate, int amount, PlayerInventorySection section, InventoryCycleOrder order)
    Removes items from a specific inventory section.
    static void
    extractSingleItem(net.minecraft.world.entity.player.Player player, InventoryPredicate predicate, PlayerInventorySection section, InventoryCycleOrder order)
    Removes a single item from an inventory section.
    static int
    findFirstMatchingSlot(net.minecraft.world.entity.player.Player player, InventoryPredicate predicate, PlayerInventorySection section)
    Finds the first slot in an inventory section with a matching item.
    static int
    getAvailableSpace(net.minecraft.world.entity.player.Player player)
    Calculates total available space for a specific item type.
    static net.minecraft.world.item.ItemStack
    getItemInSlot(net.minecraft.world.entity.player.Player player, int slotIndex)
    Get the item of the specific slot in the inventory
    static boolean
    hasEmptyHand(@NotNull net.minecraft.world.entity.player.Player player)
    Checks if either of the player's hands is empty.
    static boolean
    hasEmptyHand(@NotNull net.minecraft.world.entity.player.Player player, net.minecraft.world.InteractionHand hand)
    Checks if the specified hand is empty.
    static boolean
    hasItem(net.minecraft.world.entity.player.Player player, InventoryPredicate predicate, PlayerInventorySection section)
    Checks if a container's section contains at least one matching item.
    static boolean
    hasItemCount(net.minecraft.world.entity.player.Player player, InventoryPredicate predicate, int minCount, PlayerInventorySection section)
    Checks if a container contains at least minCount items matching the predicate.
    static void
    insertAndDiscardOverflow(net.minecraft.world.entity.player.Player player, net.minecraft.world.item.ItemStack stack)
    Adds items to a player's inventory and permanently discards any overflow
    static net.minecraft.world.item.ItemStack
    insertItem(net.minecraft.world.entity.player.Player player, net.minecraft.world.item.ItemStack stack)
    Attempts to add an item stack to an inventory.
    static void
    setItemInMainHand(@NotNull net.minecraft.world.entity.player.Player player, @NotNull net.minecraft.world.item.ItemStack item)
    Sets the item in the player's main hand.
    static void
    setItemInOffHand(@NotNull net.minecraft.world.entity.player.Player player, @NotNull net.minecraft.world.item.ItemStack item)
    Sets the item in the player's offhand.
    static void
    swapHands(@NotNull net.minecraft.world.entity.player.Player player)
    Swaps items between the player's main hand and offhand.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • hasEmptyHand

      public static boolean hasEmptyHand(@NotNull @NotNull net.minecraft.world.entity.player.Player player)
      Checks if either of the player's hands is empty.
      Parameters:
      player - Target player, not null
      Returns:
      True if either hand has an empty stack
      Throws:
      NullPointerException - if player is null
    • hasEmptyHand

      public static boolean hasEmptyHand(@NotNull @NotNull net.minecraft.world.entity.player.Player player, net.minecraft.world.InteractionHand hand)
      Checks if the specified hand is empty.
      Parameters:
      player - Target player, not null
      hand - The hand to check
      Returns:
      True if specified hand has an empty stack
      Throws:
      NullPointerException - if player is null
    • hasItemCount

      public static boolean hasItemCount(net.minecraft.world.entity.player.Player player, InventoryPredicate predicate, int minCount, PlayerInventorySection section)
      Checks if a container contains at least minCount items matching the predicate.
      Parameters:
      player - The target player
      predicate - Item matching logic
      minCount - Minimum required items (≥1)
      section - Inventory section to check
      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.entity.player.Player player, InventoryPredicate predicate, PlayerInventorySection section)
      Checks if a container's section contains at least one matching item.
      Parameters:
      player - Target player
      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.entity.player.Player player, InventoryPredicate predicate, PlayerInventorySection section)
      Finds the first slot in an inventory section with a matching item.
      Parameters:
      player - Target player
      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
    • getItemInSlot

      public static net.minecraft.world.item.ItemStack getItemInSlot(net.minecraft.world.entity.player.Player player, int slotIndex)
      Get the item of the specific slot in the inventory
      Parameters:
      player - Target player
      slotIndex - The slot index
      Returns:
      ItemStack in the specific slot of the inventory
      See Also:
    • swapHands

      public static void swapHands(@NotNull @NotNull net.minecraft.world.entity.player.Player player)
      Swaps items between the player's main hand and offhand.
      Parameters:
      player - The player whose hands should be swapped, not null
      Throws:
      NullPointerException - if player is null
    • setItemInMainHand

      public static void setItemInMainHand(@NotNull @NotNull net.minecraft.world.entity.player.Player player, @NotNull @NotNull net.minecraft.world.item.ItemStack item)
      Sets the item in the player's main hand.

      Replaces any existing item in the main hand.

      Parameters:
      player - the target player, not null
      item - the item to set, not null
      Throws:
      NullPointerException - if player or item is null
    • setItemInOffHand

      public static void setItemInOffHand(@NotNull @NotNull net.minecraft.world.entity.player.Player player, @NotNull @NotNull net.minecraft.world.item.ItemStack item)
      Sets the item in the player's offhand.

      Replaces any existing item in the offhand.

      Parameters:
      player - the target player, not null
      item - the item to set, not null
      Throws:
      NullPointerException - if player or item is null
    • clearBothHands

      public static void clearBothHands(@NotNull @NotNull net.minecraft.world.entity.player.Player player)
      Clears both the main hand and offhand of the player.
      Parameters:
      player - the target player, not null
      Throws:
      NullPointerException - if player is null
    • addItemToHands

      public static void addItemToHands(@NotNull @NotNull net.minecraft.world.entity.player.Player player, @NotNull @NotNull net.minecraft.world.item.ItemStack item)
      Attempts to add the item to the player's main hand or offhand, whichever is empty first.

      If both hands are occupied, the item is not added.

      Parameters:
      player - the target player, not null
      item - the item to add, not null
      Throws:
      NullPointerException - if player or item is null
    • extractItems

      public static void extractItems(net.minecraft.world.entity.player.Player player, InventoryPredicate predicate, int amount, PlayerInventorySection section, InventoryCycleOrder order)
      Removes items from a specific inventory section.
      Parameters:
      player - Target player
      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.entity.player.Player player, InventoryPredicate predicate, PlayerInventorySection section, InventoryCycleOrder order)
      Removes a single item from an inventory section.
      Parameters:
      player - Target player
      predicate - Item matching logic
      section - Section to remove from
      order - Slot processing order
      Throws:
      NullPointerException - if any parameter is null
    • insertItem

      public static net.minecraft.world.item.ItemStack insertItem(net.minecraft.world.entity.player.Player player, net.minecraft.world.item.ItemStack stack)
      Attempts to add an item stack to an inventory.
      Parameters:
      player - Target player
      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.entity.player.Player player, net.minecraft.world.item.ItemStack stack)
      Adds items to a player's inventory and permanently discards any overflow
      Parameters:
      player - Target player
      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, PlayerInventorySection section)
      Collects copies of items from an inventory 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.entity.player.Player player)
      Calculates total available space for a specific item type.
      Parameters:
      player - Target player
      Returns:
      Total number of items that can be added
      Throws:
      NullPointerException - if container or stack is null