Class Inventory

java.lang.Object
net.minecraft.world.entity.player.Inventory
All Implemented Interfaces:
Iterable<ItemStack>, Clearable, Container, SlotProvider, Nameable, ContainerExtension

public class Inventory extends Object implements Container, Nameable
  • Field Details

  • Constructor Details

  • Method Details

    • getSelectedSlot

      public int getSelectedSlot()
    • setSelectedSlot

      public void setSelectedSlot(int selected)
    • getSelectedItem

      public ItemStack getSelectedItem()
    • setSelectedItem

      public ItemStack setSelectedItem(ItemStack itemStack)
    • getSelectionSize

      public static int getSelectionSize()
    • getNonEquipmentItems

      public NonNullList<ItemStack> getNonEquipmentItems()
    • hasRemainingSpaceForItem

      private boolean hasRemainingSpaceForItem(ItemStack slotItemStack, ItemStack newItemStack)
    • getFreeSlot

      public int getFreeSlot()
    • addAndPickItem

      public void addAndPickItem(ItemStack itemStack)
    • pickSlot

      public void pickSlot(int slot)
    • isHotbarSlot

      public static boolean isHotbarSlot(int slot)
    • findSlotMatchingItem

      public int findSlotMatchingItem(ItemStack itemStack)
      Finds the stack or an equivalent one in the main inventory
    • isUsableForCrafting

      public static boolean isUsableForCrafting(ItemStack item)
    • findSlotMatchingCraftingIngredient

      public int findSlotMatchingCraftingIngredient(Holder<Item> item, ItemStack existingItem)
    • getSuitableHotbarSlot

      public int getSuitableHotbarSlot()
    • clearOrCountMatchingItems

      public int clearOrCountMatchingItems(Predicate<ItemStack> predicate, int amountToRemove, Container craftSlots)
    • addResource

      private int addResource(ItemStack itemStack)
      This function stores as many items of an ItemStack as possible in a matching slot and returns the quantity of left over items.
    • addResource

      private int addResource(int slot, ItemStack itemStack)
    • getSlotWithRemainingSpace

      public int getSlotWithRemainingSpace(ItemStack newItemStack)
      Stores a stack in the player's inventory. It first tries to place it in the selected slot in the player's hotbar, then the offhand slot, then any available/empty slot in the player's inventory.
    • tick

      public void tick()
    • add

      public boolean add(ItemStack itemStack)
      Adds the stack to the first empty slot in the player's inventory. Returns false if it's not possible to place the entire stack in the inventory.
    • add

      public boolean add(int slot, ItemStack itemStack)
      Adds the stack to the specified slot in the player's inventory. Returns false if it's not possible to place the entire stack in the inventory.
    • placeItemBackInInventory

      public void placeItemBackInInventory(ItemStack itemStack)
    • placeItemBackInInventory

      public void placeItemBackInInventory(ItemStack itemStack, boolean shouldSendSetSlotPacket)
    • createInventoryUpdatePacket

      public ClientboundSetPlayerInventoryPacket createInventoryUpdatePacket(int slot)
    • removeItem

      public ItemStack removeItem(int slot, int count)
      Removes up to a specified number of items from an inventory slot and returns them in a new stack.
      Specified by:
      removeItem in interface Container
    • removeItem

      public void removeItem(ItemStack itemStack)
    • removeItemNoUpdate

      public ItemStack removeItemNoUpdate(int slot)
      Removes a stack from the given slot and returns it.
      Specified by:
      removeItemNoUpdate in interface Container
    • setItem

      public void setItem(int slot, ItemStack itemStack)
      Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
      Specified by:
      setItem in interface Container
    • save

      public void save(ValueOutput.TypedOutputList<ItemStackWithSlot> output)
    • load

      public void load(ValueInput.TypedInputList<ItemStackWithSlot> input)
    • getContainerSize

      public int getContainerSize()
      Specified by:
      getContainerSize in interface Container
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Container
    • getItem

      public ItemStack getItem(int slot)
      Returns the stack in the given slot.
      Specified by:
      getItem in interface Container
    • getName

      public Component getName()
      Specified by:
      getName in interface Nameable
    • dropAll

      public void dropAll()
    • setChanged

      public void setChanged()
      Specified by:
      setChanged in interface Container
    • getTimesChanged

      public int getTimesChanged()
    • stillValid

      public boolean stillValid(Player player)
      Don't rename this method to canInteractWith due to conflicts with Container
      Specified by:
      stillValid in interface Container
    • contains

      public boolean contains(ItemStack searchStack)
      Returns true if the specified ItemStack exists in the inventory.
    • contains

      public boolean contains(TagKey<Item> tag)
    • contains

      public boolean contains(Predicate<ItemStack> predicate)
    • replaceWith

      public void replaceWith(Inventory other)
      Copy the ItemStack contents from another InventoryPlayer instance
    • clearContent

      public void clearContent()
      Specified by:
      clearContent in interface Clearable
    • fillStackedContents

      public void fillStackedContents(StackedItemContents contents)
    • removeFromSelected

      public ItemStack removeFromSelected(boolean all)
      Parameters:
      all - Whether to remove the entire stack of items. If false, removes a single item.