Class Inventory

java.lang.Object
net.minecraft.world.entity.player.Inventory
All Implemented Interfaces:
Clearable, Container, Nameable

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

  • Constructor Details

    • Inventory

      public Inventory(Player player)
  • Method Details

    • getSelected

      public ItemStack getSelected()
    • getSelectionSize

      public static int getSelectionSize()
    • hasRemainingSpaceForItem

      private boolean hasRemainingSpaceForItem(ItemStack destination, ItemStack origin)
    • getFreeSlot

      public int getFreeSlot()
    • setPickedItem

      public void setPickedItem(ItemStack stack)
    • pickSlot

      public void pickSlot(int index)
    • isHotbarSlot

      public static boolean isHotbarSlot(int index)
    • findSlotMatchingItem

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

      public int findSlotMatchingUnusedItem(ItemStack stack)
    • getSuitableHotbarSlot

      public int getSuitableHotbarSlot()
    • swapPaint

      public void swapPaint(double direction)
      Change the selected item in the hotbar after a mouse scroll. Select the slot to the left if direction is positive, or to the right if negative.
    • clearOrCountMatchingItems

      public int clearOrCountMatchingItems(Predicate<ItemStack> stackPredicate, int maxCount, Container inventory)
    • addResource

      private int addResource(ItemStack stack)
      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 stack)
    • getSlotWithRemainingSpace

      public int getSlotWithRemainingSpace(ItemStack stack)
      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 stack)
      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 stack)
      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 stack)
    • placeItemBackInInventory

      public void placeItemBackInInventory(ItemStack stack, boolean sendPacket)
    • removeItem

      public ItemStack removeItem(int index, 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 stack)
    • removeItemNoUpdate

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

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

      public float getDestroySpeed(BlockState state)
    • save

      public ListTag save(ListTag listTag)
      Writes the inventory out as a list of compound tags. This is where the slot indices are used (+100 for armor, +80 for crafting).
    • load

      public void load(ListTag listTag)
      Reads from the given tag list and fills the slots in the inventory with the correct items.
    • 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 index)
      Returns the stack in the given slot.
      Specified by:
      getItem in interface Container
    • getName

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

      public ItemStack getArmor(int slot)
      Returns:
      a player armor item (as an ItemStack) contained in specified armor slot
    • 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 stack)
      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 playerInventory)
      Copy the ItemStack contents from another InventoryPlayer instance
    • clearContent

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

      public void fillStackedContents(StackedContents stackedContent)
    • removeFromSelected

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