Inventory Utility - C00kier/CookiersLib GitHub Wiki

InventoryUtility provides utility methods to interact with a player’s inventory in a Bukkit/Spigot Minecraft plugin.

Class Overview

This class offers several helpful methods including:

  • Checking for empty slots in a player’s inventory.
  • Finding the slot index of a specific item.
  • Filling or partially filling inventories with a given item.
  • Checking if a player has enough of a material in inventory.
  • Removing specified amounts of materials from inventories.

Has empty inventory slot

Checks if the player has at least one empty slot in their inventory.

hasEmptyInventorySlot(Player player)

Parameters:

  • player — The player whose inventory is checked.

Returns:

  • true if an empty slot exists, otherwise false.

Get index of slot for specified item

Searches the player's inventory for the first occurrence of the specified item.

getSlotIndexBasedOnItemStack(Player player, ItemStack targetItem)

Parameters:

  • player — The player to search in.
  • targetItem — The item to find.

Returns:

  • An Optional containing the slot index if found, or empty if not found.

Populate all inventory slots with item

Fills every slot of the provided inventory with the given item, overwriting existing contents.

populateInventoryWithItemStack(Inventory inventory, ItemStack itemStack)

Parameters:

  • inventory — The inventory to fill.
  • itemStack — The item to place in every slot.

Populate empty inventory slots with item

Fills only the empty slots of the inventory with the specified item, leaving occupied slots unchanged.

populateEmptySpacesWithItemStack(Inventory inventory, ItemStack itemStack)

Parameters:

  • inventory — The inventory to partially fill.
  • itemStack — The item to place in empty slots.

Check if has specified amount of material

Checks if the player has at least the specified amount of a material in their inventory.

hasEnoughMaterialInInventory(Player player, Material material, int neededAmount)

Parameters:

  • player — The player to check.
  • material — The material type.
  • neededAmount — The required amount.

Returns:

  • true if the player has at least the required amount, false otherwise.

Remove specified material amount from inventory

Removes the specified amount of the material from the player’s inventory, spanning multiple stacks if necessary.

removeMaterialFromInventory(Player player, Material material, int amountToRemove)

Parameters:

  • player — The player whose inventory to modify.
  • material — The material to remove.
  • amountToRemove — The total amount to remove.