Package net.xun.lib.common.api.util
Class PlayerInventoryUtils
java.lang.Object
net.xun.lib.common.api.util.PlayerInventoryUtils
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 TypeMethodDescriptionstatic voidaddItemToHands(@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 voidclearBothHands(@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 voidextractItems(net.minecraft.world.entity.player.Player player, InventoryPredicate predicate, int amount, PlayerInventorySection section, InventoryCycleOrder order) Removes items from a specific inventory section.static voidextractSingleItem(net.minecraft.world.entity.player.Player player, InventoryPredicate predicate, PlayerInventorySection section, InventoryCycleOrder order) Removes a single item from an inventory section.static intfindFirstMatchingSlot(net.minecraft.world.entity.player.Player player, InventoryPredicate predicate, PlayerInventorySection section) Finds the first slot in an inventory section with a matching item.static intgetAvailableSpace(net.minecraft.world.entity.player.Player player) Calculates total available space for a specific item type.static net.minecraft.world.item.ItemStackgetItemInSlot(net.minecraft.world.entity.player.Player player, int slotIndex) Get the item of the specific slot in the inventorystatic booleanhasEmptyHand(@NotNull net.minecraft.world.entity.player.Player player) Checks if either of the player's hands is empty.static booleanhasEmptyHand(@NotNull net.minecraft.world.entity.player.Player player, net.minecraft.world.InteractionHand hand) Checks if the specified hand is empty.static booleanhasItem(net.minecraft.world.entity.player.Player player, InventoryPredicate predicate, PlayerInventorySection section) Checks if a container's section contains at least one matching item.static booleanhasItemCount(net.minecraft.world.entity.player.Player player, InventoryPredicate predicate, int minCount, PlayerInventorySection section) Checks if a container contains at leastminCountitems matching the predicate.static voidinsertAndDiscardOverflow(net.minecraft.world.entity.player.Player player, net.minecraft.world.item.ItemStack stack) Adds items to a player's inventory and permanently discards any overflowstatic net.minecraft.world.item.ItemStackinsertItem(net.minecraft.world.entity.player.Player player, net.minecraft.world.item.ItemStack stack) Attempts to add an item stack to an inventory.static voidsetItemInMainHand(@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 voidsetItemInOffHand(@NotNull net.minecraft.world.entity.player.Player player, @NotNull net.minecraft.world.item.ItemStack item) Sets the item in the player's offhand.static voidswapHands(@NotNull net.minecraft.world.entity.player.Player player) Swaps items between the player's main hand and offhand.
-
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 nullhand- 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 leastminCountitems matching the predicate.- Parameters:
player- The target playerpredicate- Item matching logicminCount- 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 playerpredicate- 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.entity.player.Player player, InventoryPredicate predicate, PlayerInventorySection section) Finds the first slot in an inventory section with a matching item.- Parameters:
player- Target playerpredicate- Item matching logicsection- 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 playerslotIndex- 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 nullitem- 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 nullitem- 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 nullitem- 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 playerpredicate- 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.entity.player.Player player, InventoryPredicate predicate, PlayerInventorySection section, InventoryCycleOrder order) Removes a single item from an inventory section.- Parameters:
player- Target playerpredicate- Item matching logicsection- Section to remove fromorder- 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 playerstack- 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 playerstack- 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 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.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
-