Skip to main content

Class: Container

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

Example

let leftLocation = test.worldLocation({ x: 2, y: 2, z: 2 }); // left chest location
let rightLocation = test.worldLocation({ x: 4, y: 2, z: 2 }); // right chest location

const chestCart = test.spawn("chest_minecart", { x: 6, y: 2, z: 2 });

let leftChestBlock = defaultDimension.getBlock(leftLocation);
let rightChestBlock = defaultDimension.getBlock(rightLocation);

leftChestBlock.setType(MinecraftBlockTypes.chest);
rightChestBlock.setType(MinecraftBlockTypes.chest);

const rightChestInventoryComp = rightChestBlock.getComponent("inventory");
const leftChestInventoryComp = leftChestBlock.getComponent("inventory");
const chestCartInventoryComp = chestCart.getComponent("inventory");

const rightChestContainer = rightChestInventoryComp.container;
const leftChestContainer = leftChestInventoryComp.container;
const chestCartContainer = chestCartInventoryComp.container;

rightChestContainer.setItem(0, new ItemStack(Items.apple, 10, 0));
test.assert(rightChestContainer.getItem(0).id === "apple", "Expected apple in right container slot index 0");

rightChestContainer.setItem(1, new ItemStack(Items.emerald, 10, 0));
test.assert(rightChestContainer.getItem(1).id === "emerald", "Expected emerald in right container slot index 1");

test.assert(rightChestContainer.size === 27, "Unexpected size: " + rightChestContainer.size);
test.assert(
rightChestContainer.emptySlotsCount === 25,
"Unexpected emptySlotsCount: " + rightChestContainer.emptySlotsCount
);

const itemStack = rightChestContainer.getItem(0);
test.assert(itemStack.id === "apple", "Expected apple");
test.assert(itemStack.amount === 10, "Expected 10 apples");
test.assert(itemStack.data === 0, "Expected 0 data");

leftChestContainer.setItem(0, new ItemStack(Items.cake, 10, 0));

rightChestContainer.transferItem(0, 4, chestCartContainer); // transfer the apple from the right chest to a chest cart
rightChestContainer.swapItems(1, 0, leftChestContainer); // swap the cake and emerald

test.assert(chestCartContainer.getItem(4).id === "apple", "Expected apple in left container slot index 4");
test.assert(leftChestContainer.getItem(0).id === "emerald", "Expected emerald in left container slot index 0");
test.assert(rightChestContainer.getItem(1).id === "cake", "Expected cake in right container slot index 1");

Constructors

new Container()

private new Container(): Container

Returns

Container

Properties

emptySlotsCount

readonly emptySlotsCount: number

Remarks

Count of the slots in the container that are empty.

Throws

Throws if the container is invalid.


size

readonly 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

addItem()

addItem(itemStack): ItemStack

Parameters

ParameterTypeDescription
itemStackItemStackThe 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 Container.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.


clearAll()

clearAll(): void

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.


getItem()

getItem(slot): ItemStack

Parameters

ParameterTypeDescription
slotnumberZero-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 Container.getSlot.

Throws

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

Example

// A function that gets a copy of the first item in the player's hotbar
import { Player, EntityInventoryComponent, ItemStack } from '@minecraft/server';

function getFirstHotbarItem(player: Player): ItemStack | undefined {
const inventory = player.getComponent(EntityInventoryComponent.componentId);
if (inventory && inventory.container) {
return inventory.container.getItem(0);
}
return undefined;
}

getSlot()

getSlot(slot): ContainerSlot

Parameters

ParameterTypeDescription
slotnumberThe 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.

Throws

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


isValid()

isValid(): boolean

Returns

boolean

Remarks

Returns whether a container object (or the entity or block that this container is associated with) is still available for use in this context.


moveItem()

moveItem(fromSlot, toSlot, toContainer): void

Parameters

ParameterTypeDescription
fromSlotnumberZero-based index of the slot to transfer an item from, on
this container.
toSlotnumberZero-based index of the slot to transfer an item to, on
toContainer.
toContainerContainerTarget 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

// A function that moves an item from one slot of the player's inventory to another player's inventory
import { Player, EntityComponentTypes } from '@minecraft/server';

function moveBetweenPlayers(slotId: number, fromPlayer: Player, toPlayer: Player) {
const fromInventory = fromPlayer.getComponent(EntityComponentTypes.Inventory);
const toInventory = toPlayer.getComponent(EntityComponentTypes.Inventory);

if (fromInventory && toInventory && fromInventory.container && toInventory.container) {
fromInventory.container.moveItem(slotId, slotId, toInventory.container);
}
}

setItem()

setItem(slot, itemStack?): void

Parameters

ParameterTypeDescription
slotnumberZero-based index of the slot to set an item at.
itemStack?ItemStackStack 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.


swapItems()

swapItems(slot, otherSlot, otherContainer): void

Parameters

ParameterTypeDescription
slotnumberZero-based index of the slot to swap from this container.
otherSlotnumberZero-based index of the slot to swap with.
otherContainerContainerTarget 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

// A function that swaps an item from one slot of the player's inventory to another player's inventory
import { Player, EntityComponentTypes } from '@minecraft/server';

function swapBetweenPlayers(slotId: number, fromPlayer: Player, toPlayer: Player) {
const fromInventory = fromPlayer.getComponent(EntityComponentTypes.Inventory);
const toInventory = toPlayer.getComponent(EntityComponentTypes.Inventory);

if (fromInventory && toInventory && fromInventory.container && toInventory.container) {
fromInventory.container.swapItems(slotId, slotId, toInventory.container);
}
}

transferItem()

transferItem(fromSlot, toContainer): ItemStack

Parameters

ParameterTypeDescription
fromSlotnumberZero-based index of the slot to transfer an item from, on
this container.
toContainerContainerTarget container to transfer to. Note this can be the same
container as the source.

Returns

ItemStack

An itemStack with the items that couldn't be transferred. Returns undefined if all items were transferred.

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

// A function that moves an item from one slot of the player's inventory to another player's inventory
import { Player, EntityComponentTypes } from '@minecraft/server';

function moveBetweenPlayers(slotId: number, fromPlayer: Player, toPlayer: Player) {
const fromInventory = fromPlayer.getComponent(EntityComponentTypes.Inventory);
const toInventory = toPlayer.getComponent(EntityComponentTypes.Inventory);

if (fromInventory && toInventory && fromInventory.container && toInventory.container) {
fromInventory.container.transferItem(slotId, toInventory.container);
}
}