Script API - v1.19.70
    Preparing search index...

    Class InventoryComponentContainerBeta

    Represents a container that can hold sets of items. Used with entities such as Players, Chest Minecarts, Llamas, and more.

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    emptySlotsCount: number

    Count of the slots in the container that are empty.

    Throws if the container is invalid.

    size: number

    The number of slots in this container. For example, a standard single-block chest has a size of 27. Note, a player's inventory container contains a total of 36 slots, 9 hotbar slots plus 27 inventory slots.

    Throws if the container is invalid.

    Methods

    • Parameters

      • itemStack: ItemStack

        The stack of items to add.

      Returns void

      Adds an item to the container. The item is placed in the first available slot(s) and can be stacked with existing items of the same type. Note, use Container.setItem if you wish to set the item in a particular slot.

      This function can throw errors.

    • Returns void

      Clears all inventory items in the container.

      Throws if the container is invalid.

    • Parameters

      • slot: number

      Returns void

      Clears a specific item at a slot within the container.

      Throws if the container is invalid.

    • Parameters

      • slot: number

        Zero-based index of the slot to retrieve items from.

      Returns ItemStack

      Gets an ItemStack of the item at the specified slot. If the slot is empty, returns undefined. This method does not change or clear the contents of the specified slot. To get a reference to a particular slot, see Container.getSlot.

      Throws if the container is invalid or if the slot index is out of bounds.

      // Get a copy of the first item in the player's hotbar
      const inventory = player.getComponent("inventory") as EntityInventoryComponent;
      const itemStack = inventory.container.getItem(0);
    • Parameters

      • slot: number

        The index of the slot to return. This index must be within the bounds of the container.

      Returns ContainerSlot

      Returns a container slot. This acts as a reference to a slot at the given index for this container.

      Throws if the container is invalid or if the slot index is out of bounds.

    • Parameters

      • slot: number

        Zero-based index of the slot to set an item at.

      • OptionalitemStack: ItemStack

        Stack of items to place within the specified slot. Setting itemStack to undefined will clear the slot.

      Returns void

      Sets an item stack within a particular slot.

      Throws if the container is invalid or if the slot index is out of bounds.

    • Parameters

      • slot: number

        Zero-based index of the slot to swap from this container.

      • otherSlot: number

        Zero-based index of the slot to swap with.

      • otherContainer: Container

        Target container to swap with. Note this can be the same container as this source.

      Returns boolean

      Swaps items between two different slots within containers.

      Throws if either this container or otherContainer are invalid or if the slot or otherSlot are out of bounds.

      // Swaps an item between slots 0 and 4 in the player's inventory
      const inventory = fromPlayer.getComponent('inventory') as EntityInventoryComponent;
      inventory.container.swapItems(0, 4, inventory);
    • Parameters

      • fromSlot: number

        Zero-based index of the slot to transfer an item from, on this container.

      • toSlot: number
      • toContainer: Container

        Target container to transfer to. Note this can be the same container as the source.

      Returns boolean

      Moves an item from one slot to another container, or to the first available slot in the same container.

      Throws if either this container or toContainer are invalid or if the fromSlot or toSlot indices out of bounds.

      // Transfer an item from the first slot of fromPlayer's inventory to toPlayer's inventory
      const fromInventory = fromPlayer.getComponent('inventory') as EntityInventoryComponent;
      const toInventory = toPlayer.getComponent('inventory') as EntityInventoryComponent;
      fromInventory.container.transferItem(0, toInventory.container);