Interface Inventory

All Superinterfaces:
Clearable
All Known Subinterfaces:
Hopper, RecipeInputInventory, SidedInventory, SingleStackInventory, VehicleInventory
All Known Implementing Classes:
AbstractFurnaceBlockEntity, BarrelBlockEntity, BlastFurnaceBlockEntity, BrewingStandBlockEntity, ChestBlockEntity, ChestBoatEntity, ChestMinecartEntity, ChiseledBookshelfBlockEntity, ComposterBlock.ComposterInventory, ComposterBlock.DummyInventory, ComposterBlock.FullComposterInventory, CraftingInventory, CraftingResultInventory, DispenserBlockEntity, DoubleInventory, DropperBlockEntity, EnderChestInventory, FurnaceBlockEntity, HopperBlockEntity, HopperMinecartEntity, JukeboxBlockEntity, LockableContainerBlockEntity, LootableContainerBlockEntity, MerchantInventory, PlayerInventory, ShulkerBoxBlockEntity, SimpleInventory, SmokerBlockEntity, StorageMinecartEntity, TrappedChestBlockEntity

public interface Inventory extends Clearable
A container of ItemStacks. In general, when a player stores an item stack and can retrieve the same item stack back, that stack is stored in an inventory. The inventory can be persistent, like chests or donkeys, or it can be created without backing storage, like the slots in crafting tables. It is the responsibility of the user to sync or save the contents of the inventory.

Entities and block entities that can hold item stacks generally implement this interface themselves, allowing hopper interactions. Call PlayerEntity.getInventory() to get the player's inventory (including armors and offhand).

An inventory has a fixed size, and each element in the inventory is identified by the slot number, which is between zero and size() - 1 like arrays. When a slot of the inventory is empty, it should be filled with ItemStack.EMPTY.

An implementation of this interface should have a field of DefaultedList.ofSize(int, Object) with the second argument as ItemStack.EMPTY, and implement methods by delegating to the list. The list itself should not be modified directly, and the list's size should remain constant throughout the lifetime of the inventory. Implementations must call markDirty() when the inventory is modified.

See Also:
API Note:
If an inventory is needed for temporary storage, use SimpleInventory. For persistent storage in entities or block entities, use VehicleInventory or LockableContainerBlockEntity.
Mappings:
Namespace Name
official bgr
intermediary net/minecraft/class_1263
named net/minecraft/inventory/Inventory
  • Field Details

    • MAX_COUNT_PER_STACK

      static final int MAX_COUNT_PER_STACK
      See Also:
      Mappings:
      Namespace Name Mixin selector
      official m_ Lbgr;m_:I
      intermediary field_29952 Lnet/minecraft/class_1263;field_29952:I
      named MAX_COUNT_PER_STACK Lnet/minecraft/inventory/Inventory;MAX_COUNT_PER_STACK:I
    • field_42619

      static final int field_42619
      See Also:
      Mappings:
      Namespace Name Mixin selector
      official n_ Lbgr;n_:I
      intermediary field_42619 Lnet/minecraft/class_1263;field_42619:I
      named field_42619 Lnet/minecraft/inventory/Inventory;field_42619:I
  • Method Details

    • size

      int size()
      Returns the size of the inventory.

      The inventory should support the slot ID from 0 to size() - 1. This should remain constant throughout the inventory's lifetime.

      Returns:
      the size of the inventory
      Mappings:
      Namespace Name Mixin selector
      official b Lbgr;b()I
      intermediary method_5439 Lnet/minecraft/class_1263;method_5439()I
      named size Lnet/minecraft/inventory/Inventory;size()I
    • isEmpty

      boolean isEmpty()
      Returns whether the inventory consists entirely of empty item stacks.
      Returns:
      whether the inventory consists entirely of empty item stacks
      Mappings:
      Namespace Name Mixin selector
      official af_ Lbgr;af_()Z
      intermediary method_5442 Lnet/minecraft/class_1263;method_5442()Z
      named isEmpty Lnet/minecraft/inventory/Inventory;isEmpty()Z
    • getStack

      ItemStack getStack(int slot)
      Returns the stack currently stored at slot.

      If the slot is empty, or is outside the bounds of this inventory, this returns ItemStack.EMPTY.

      Returns:
      the stack currently stored at slot
      Mappings:
      Namespace Name Mixin selector
      official a Lbgr;a(I)Lcjf;
      intermediary method_5438 Lnet/minecraft/class_1263;method_5438(I)Lnet/minecraft/class_1799;
      named getStack Lnet/minecraft/inventory/Inventory;getStack(I)Lnet/minecraft/item/ItemStack;
    • removeStack

      ItemStack removeStack(int slot, int amount)
      Removes a specific number of items from slot.
      Returns:
      the removed items as a stack
      Mappings:
      Namespace Name Mixin selector
      official a Lbgr;a(II)Lcjf;
      intermediary method_5434 Lnet/minecraft/class_1263;method_5434(II)Lnet/minecraft/class_1799;
      named removeStack Lnet/minecraft/inventory/Inventory;removeStack(II)Lnet/minecraft/item/ItemStack;
    • removeStack

      ItemStack removeStack(int slot)
      Removes the stack currently stored at slot.
      Returns:
      the stack previously stored at the indicated slot
      Mappings:
      Namespace Name Mixin selector
      official b Lbgr;b(I)Lcjf;
      intermediary method_5441 Lnet/minecraft/class_1263;method_5441(I)Lnet/minecraft/class_1799;
      named removeStack Lnet/minecraft/inventory/Inventory;removeStack(I)Lnet/minecraft/item/ItemStack;
    • setStack

      void setStack(int slot, ItemStack stack)
      Sets the stack stored at slot to stack.
      Mappings:
      Namespace Name Mixin selector
      official a Lbgr;a(ILcjf;)V
      intermediary method_5447 Lnet/minecraft/class_1263;method_5447(ILnet/minecraft/class_1799;)V
      named setStack Lnet/minecraft/inventory/Inventory;setStack(ILnet/minecraft/item/ItemStack;)V
    • getMaxCountPerStack

      default int getMaxCountPerStack()
      Returns the maximum number of items a stack can contain when placed inside this inventory.

      No slots may have more than this number of items. It is effectively the stacking limit for this inventory's slots.

      Returns:
      the maximum number of items a stack can contain when placed inside this inventory
      Mappings:
      Namespace Name Mixin selector
      official ag_ Lbgr;ag_()I
      intermediary method_5444 Lnet/minecraft/class_1263;method_5444()I
      named getMaxCountPerStack Lnet/minecraft/inventory/Inventory;getMaxCountPerStack()I
    • markDirty

      void markDirty()
      Marks the inventory as modified. Implementations should call this method every time the inventory is changed in any way.
      See Also:
      API Note:
      Implementations should mark the inventory for synchronization or saving in this method. Since this is called frequently, it is not recommended to synchronize or save the inventory directly in this method. If this inventory is implemented in a block entity, then it should always call super.markDirty(); to ensure the block entity gets saved.
      Mappings:
      Namespace Name Mixin selector
      official e Lbgr;e()V
      intermediary method_5431 Lnet/minecraft/class_1263;method_5431()V
      named markDirty Lnet/minecraft/inventory/Inventory;markDirty()V
    • canPlayerUse

      boolean canPlayerUse(PlayerEntity player)
      Returns:
      whether player can use this inventory
      See Also:
      API Note:
      Implementations should check the distance between the inventory holder and player. For convenience, this interface offers two methods used by block entities to implement this check.
      Mappings:
      Namespace Name Mixin selector
      official a Lbgr;a(Lcbu;)Z
      intermediary method_5443 Lnet/minecraft/class_1263;method_5443(Lnet/minecraft/class_1657;)Z
      named canPlayerUse Lnet/minecraft/inventory/Inventory;canPlayerUse(Lnet/minecraft/entity/player/PlayerEntity;)Z
    • onOpen

      default void onOpen(PlayerEntity player)
      Called when the inventory is opened. Specifically, this is called inside the ScreenHandler constructor. This does nothing by default.

      The method is called in both the client and the server. However, because clientside screen handler is created with a SimpleInventory, other implementations can (and the vanilla code does) assume that the method is called in the server.

      Mappings:
      Namespace Name Mixin selector
      official d_ Lbgr;d_(Lcbu;)V
      intermediary method_5435 Lnet/minecraft/class_1263;method_5435(Lnet/minecraft/class_1657;)V
      named onOpen Lnet/minecraft/inventory/Inventory;onOpen(Lnet/minecraft/entity/player/PlayerEntity;)V
    • onClose

      default void onClose(PlayerEntity player)
      Called when the inventory is closed. Specifically, this is called inside ScreenHandler.onClosed(net.minecraft.entity.player.PlayerEntity). This does nothing by default.

      The method is called in both the client and the server. However, because clientside screen handler is created with a SimpleInventory, other implementations can (and the vanilla code does) assume that the method is called in the server.

      Mappings:
      Namespace Name Mixin selector
      official c Lbgr;c(Lcbu;)V
      intermediary method_5432 Lnet/minecraft/class_1263;method_5432(Lnet/minecraft/class_1657;)V
      named onClose Lnet/minecraft/inventory/Inventory;onClose(Lnet/minecraft/entity/player/PlayerEntity;)V
    • isValid

      default boolean isValid(int slot, ItemStack stack)
      Returns whether stack is valid for the slot.

      Implementations can, for example, use this to check whether the item is in a specific tag. This returns true by default.

      Returns:
      whether stack is valid for the slot
      Mappings:
      Namespace Name Mixin selector
      official b Lbgr;b(ILcjf;)Z
      intermediary method_5437 Lnet/minecraft/class_1263;method_5437(ILnet/minecraft/class_1799;)Z
      named isValid Lnet/minecraft/inventory/Inventory;isValid(ILnet/minecraft/item/ItemStack;)Z
    • canTransferTo

      default boolean canTransferTo(Inventory hopperInventory, int slot, ItemStack stack)
      Returns whether a hopper can transfer stack from slot to the hopper.

      This returns true by default.

      Returns:
      whether a hopper can transfer stack from slot to the hopper
      Mappings:
      Namespace Name Mixin selector
      official a Lbgr;a(Lbgr;ILcjf;)Z
      intermediary method_49104 Lnet/minecraft/class_1263;method_49104(Lnet/minecraft/class_1263;ILnet/minecraft/class_1799;)Z
      named canTransferTo Lnet/minecraft/inventory/Inventory;canTransferTo(Lnet/minecraft/inventory/Inventory;ILnet/minecraft/item/ItemStack;)Z
    • count

      default int count(Item item)
      Returns the number of times item occurs in this inventory across all stored stacks.
      Returns:
      the number of times item occurs in this inventory across all stored stacks
      Mappings:
      Namespace Name Mixin selector
      official a_ Lbgr;a_(Lcja;)I
      intermediary method_18861 Lnet/minecraft/class_1263;method_18861(Lnet/minecraft/class_1792;)I
      named count Lnet/minecraft/inventory/Inventory;count(Lnet/minecraft/item/Item;)I
    • containsAny

      default boolean containsAny(Set<Item> items)
      Returns whether this inventory contains any of items.
      Returns:
      whether this inventory contains any of items
      See Also:
      Mappings:
      Namespace Name Mixin selector
      official a Lbgr;a(Ljava/util/Set;)Z
      intermediary method_18862 Lnet/minecraft/class_1263;method_18862(Ljava/util/Set;)Z
      named containsAny Lnet/minecraft/inventory/Inventory;containsAny(Ljava/util/Set;)Z
    • containsAny

      default boolean containsAny(Predicate<ItemStack> predicate)
      Returns whether this inventory contains any of the stacks matching predicate.
      Returns:
      whether this inventory contains any of the stacks matching predicate
      See Also:
      Mappings:
      Namespace Name Mixin selector
      official a_ Lbgr;a_(Ljava/util/function/Predicate;)Z
      intermediary method_43256 Lnet/minecraft/class_1263;method_43256(Ljava/util/function/Predicate;)Z
      named containsAny Lnet/minecraft/inventory/Inventory;containsAny(Ljava/util/function/Predicate;)Z
    • canPlayerUse

      static boolean canPlayerUse(BlockEntity blockEntity, PlayerEntity player)
      Returns whether player can use this blockEntity.
      Returns:
      whether player can use this blockEntity
      See Also:
      API Note:
      This is used by block entities to implement canPlayerUse(PlayerEntity).
      Implementation Note:
      This method checks whether the given block entity exists and whether the player is within 8 blocks of the block entity.
      Mappings:
      Namespace Name Mixin selector
      official a Lbgr;a(Ldcv;Lcbu;)Z
      intermediary method_49105 Lnet/minecraft/class_1263;method_49105(Lnet/minecraft/class_2586;Lnet/minecraft/class_1657;)Z
      named canPlayerUse Lnet/minecraft/inventory/Inventory;canPlayerUse(Lnet/minecraft/block/entity/BlockEntity;Lnet/minecraft/entity/player/PlayerEntity;)Z
    • canPlayerUse

      static boolean canPlayerUse(BlockEntity blockEntity, PlayerEntity player, int range)
      Returns whether player can use this blockEntity.
      Returns:
      whether player can use this blockEntity
      See Also:
      API Note:
      This is used by block entities to implement canPlayerUse(PlayerEntity).
      Implementation Note:
      This method checks whether the given block entity exists and whether the player is within range blocks of the block entity.
      Mappings:
      Namespace Name Mixin selector
      official a Lbgr;a(Ldcv;Lcbu;I)Z
      intermediary method_49106 Lnet/minecraft/class_1263;method_49106(Lnet/minecraft/class_2586;Lnet/minecraft/class_1657;I)Z
      named canPlayerUse Lnet/minecraft/inventory/Inventory;canPlayerUse(Lnet/minecraft/block/entity/BlockEntity;Lnet/minecraft/entity/player/PlayerEntity;I)Z