Private
constructorReadonly
emptyCount of the slots in the container that are empty.
Throws if the container is invalid.
Readonly
sizeThe 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.
The stack of items to add.
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.
This function can throw errors.
import { EntityInventoryComponent, ItemStack, world } from "@minecraft/server";
for (const player of world.getAllPlayers()) {
const inventory = player.getComponent(
"inventory"
) as EntityInventoryComponent;
const item = new ItemStack("minecraft:diamond_sword", 10);
inventory.container.addItem(item);
}
Zero-based index of the slot to retrieve items from.
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.
// 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;
}
The index of the slot to return. This index must be within the bounds of the container.
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.
Zero-based index of the slot to transfer an item from, on this container.
Zero-based index of the slot to transfer an item to, on
toContainer
.
Target container to transfer to. Note this can be the same container as the source.
Moves an item from one slot to another, potentially across containers.
This function can't be called in read-only mode.
Throws if either this container or toContainer
are invalid
or if the fromSlot
or toSlot
indices out of bounds.
// 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);
}
}
Zero-based index of the slot to set an item at.
Optional
itemStack: ItemStackStack of items to place within the specified slot. Setting
itemStack
to undefined will clear the slot.
Optional
Sets an item stack within a particular slot.
This function can't be called in read-only mode.
Throws if the container is invalid or if the slot
index is
out of bounds.
import { EntityInventoryComponent, ItemStack, world } from "@minecraft/server";
for (const player of world.getAllPlayers()) {
const inventory = player.getComponent(
"inventory"
) as EntityInventoryComponent;
const item = new ItemStack("minecraft:diamond_sword", 10);
inventory.container.setItem(0, item);
}
Zero-based index of the slot to swap from this container.
Zero-based index of the slot to swap with.
Target container to swap with. Note this can be the same container as this source.
Swaps items between two different slots within containers.
This function can't be called in read-only mode.
Throws if either this container or otherContainer
are
invalid or if the slot
or otherSlot
are out of bounds.
// 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);
}
}
Zero-based index of the slot to transfer an item from, on this container.
Target container to transfer to. Note this can be the same container as the source.
An itemStack with the items that couldn't be transferred. Returns undefined if all items were transferred.
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 if either this container or toContainer
are invalid
or if the fromSlot
or toSlot
indices out of bounds.
// 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);
}
}
Represents a container that can hold sets of items. Used with entities such as Players, Chest Minecarts, Llamas, and more.
Example
containers.js