Class ContainerBeta

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

Hierarchy

  • Container

Constructors

Properties

emptySlotsCount: number

Remarks

Count of the slots in the container that are empty.

Throws

Throws if the container is invalid.

size: number

Remarks

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

Throws if the container is invalid.

Methods

  • Parameters

    • itemStack: ItemStack

      The stack of items to add.

    Returns ItemStack

    Remarks

    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 setItem if you wish to set the item in a particular slot.

    This function can't be called in read-only mode.

    Throws

    This function can throw errors.

  • Returns void

    Remarks

    Clears all inventory items in the container.

    This function can't be called in read-only mode.

    Throws

    Throws if the container is invalid.

  • Parameters

    • slot: number

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

    Returns ItemStack

    Remarks

    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 getSlot.

    This function can't be called in read-only mode.

    Throws

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

    Example

    getItem.ts

           // 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

    Remarks

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

    This function can't be called in read-only mode.

    Throws

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

  • Parameters

    • fromSlot: number

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

    • toSlot: number

      Zero-based index of the slot to transfer an item to, on toContainer.

    • toContainer: Container

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

    Returns void

    Remarks

    Moves an item from one slot to another, potentially across containers.

    This function can't be called in read-only mode.

    Throws

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

    Example

    moveItem.ts

           // Move an item from the first slot of fromPlayer's inventory to the fifth slot of toPlayer's inventory
    const fromInventory = fromPlayer.getComponent('inventory') as EntityInventoryComponent;
    const toInventory = toPlayer.getComponent('inventory') as EntityInventoryComponent;
    fromInventory.container.moveItem(0, 4, toInventory.container);
  • Parameters

    • slot: number

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

    • Optional itemStack: ItemStack

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

    Returns void

    Remarks

    Sets an item stack within a particular slot.

    This function can't be called in read-only mode.

    Throws

    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 void

    Remarks

    Swaps items between two different slots within containers.

    This function can't be called in read-only mode.

    Throws

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

    Example

    swapItems.ts

           // 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.

    • toContainer: Container

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

    Returns ItemStack

    Remarks

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

    This function can't be called in read-only mode.

    Throws

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

    Example

    transferItem.ts

           // 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);