@minecraft/server / Container
Class: Container ​
Defined in: @minecraft/server/index.d.ts:6323
Represents a container that can hold sets of items. Used with entities such as Players, Chest Minecarts, Llamas, and more.
Example ​
containers.ts
import { ItemStack, EntityInventoryComponent, BlockInventoryComponent, DimensionLocation } from '@minecraft/server';
import { MinecraftBlockTypes, MinecraftItemTypes, MinecraftEntityTypes } from '@minecraft/vanilla-data';
function containers(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
const xLocation = targetLocation; // left chest location
const xPlusTwoLocation = { x: targetLocation.x + 2, y: targetLocation.y, z: targetLocation.z }; // right chest
const chestCart = targetLocation.dimension.spawnEntity(MinecraftEntityTypes.ChestMinecart, {
x: targetLocation.x + 4,
y: targetLocation.y,
z: targetLocation.z,
});
const xChestBlock = targetLocation.dimension.getBlock(xLocation);
const xPlusTwoChestBlock = targetLocation.dimension.getBlock(xPlusTwoLocation);
if (!xChestBlock || !xPlusTwoChestBlock) {
log('Could not retrieve chest blocks.');
return;
}
xChestBlock.setType(MinecraftBlockTypes.Chest);
xPlusTwoChestBlock.setType(MinecraftBlockTypes.Chest);
const xPlusTwoChestInventoryComp = xPlusTwoChestBlock.getComponent('inventory') as BlockInventoryComponent;
const xChestInventoryComponent = xChestBlock.getComponent('inventory') as BlockInventoryComponent;
const chestCartInventoryComp = chestCart.getComponent('inventory') as EntityInventoryComponent;
const xPlusTwoChestContainer = xPlusTwoChestInventoryComp.container;
const xChestContainer = xChestInventoryComponent.container;
const chestCartContainer = chestCartInventoryComp.container;
if (!xPlusTwoChestContainer || !xChestContainer || !chestCartContainer) {
log('Could not retrieve chest containers.');
return;
}
xPlusTwoChestContainer.setItem(0, new ItemStack(MinecraftItemTypes.Apple, 10));
if (xPlusTwoChestContainer.getItem(0)?.typeId !== MinecraftItemTypes.Apple) {
log('Expected apple in x+2 container slot index 0', -1);
}
xPlusTwoChestContainer.setItem(1, new ItemStack(MinecraftItemTypes.Emerald, 10));
if (xPlusTwoChestContainer.getItem(1)?.typeId !== MinecraftItemTypes.Emerald) {
log('Expected emerald in x+2 container slot index 1', -1);
}
if (xPlusTwoChestContainer.size !== 27) {
log('Unexpected size: ' + xPlusTwoChestContainer.size, -1);
}
if (xPlusTwoChestContainer.emptySlotsCount !== 25) {
log('Unexpected emptySlotsCount: ' + xPlusTwoChestContainer.emptySlotsCount, -1);
}
xChestContainer.setItem(0, new ItemStack(MinecraftItemTypes.Cake, 10));
xPlusTwoChestContainer.transferItem(0, chestCartContainer); // transfer the apple from the xPlusTwo chest to a chest cart
xPlusTwoChestContainer.swapItems(1, 0, xChestContainer); // swap the cake from x and the emerald from xPlusTwo
if (chestCartContainer.getItem(0)?.typeId !== MinecraftItemTypes.Apple) {
log('Expected apple in minecraft chest container slot index 0', -1);
}
if (xChestContainer.getItem(0)?.typeId === MinecraftItemTypes.Emerald) {
log('Expected emerald in x container slot index 0', -1);
}
if (xPlusTwoChestContainer.getItem(1)?.typeId === MinecraftItemTypes.Cake) {
log('Expected cake in x+2 container slot index 1', -1);
}
}Properties ​
containerRules? ​
readonlyoptionalcontainerRules?:ContainerRules
Defined in: @minecraft/server/index.d.ts:6332
Remarks ​
If these rules are defined other container operations will throw if they cause these rules to be invalidated. For example, adding a shulker box to a vanilla bundle.
emptySlotsCount ​
readonlyemptySlotsCount:number
Defined in: @minecraft/server/index.d.ts:6340
Remarks ​
Count of the slots in the container that are empty.
Throws ​
Throws if the container is invalid.
isValid ​
readonlyisValid:boolean
Defined in: @minecraft/server/index.d.ts:6348
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.
size ​
readonlysize:number
Defined in: @minecraft/server/index.d.ts:6359
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.
weight ​
readonlyweight:number
Defined in: @minecraft/server/index.d.ts:6368
Remarks ​
The combined weight of all items in the container.
Throws ​
This property can throw when used.
Methods ​
addItem() ​
addItem(
itemStack):ItemStack|undefined
Defined in: @minecraft/server/index.d.ts:6386
Parameters ​
itemStack ​
The stack of items to add.
Returns ​
ItemStack | undefined
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 restricted-execution mode.
Throws ​
This function can throw errors.
Error
clearAll() ​
clearAll():
void
Defined in: @minecraft/server/index.d.ts:6396
Returns ​
void
Remarks ​
Clears all inventory items in the container.
This function can't be called in restricted-execution mode.
Throws ​
Throws if the container is invalid.
contains() ​
contains(
itemStack):boolean
Defined in: @minecraft/server/index.d.ts:6407
Parameters ​
itemStack ​
The item to find.
Returns ​
boolean
Remarks ​
Attempts to find an item inside the container
Throws ​
This function can throw errors.
find() ​
find(
itemStack):number|undefined
Defined in: @minecraft/server/index.d.ts:6419
Parameters ​
itemStack ​
The item to find.
Returns ​
number | undefined
Remarks ​
Find the index of the first instance of an item inside the container
Throws ​
This function can throw errors.
findLast() ​
findLast(
itemStack):number|undefined
Defined in: @minecraft/server/index.d.ts:6431
Parameters ​
itemStack ​
The item to find.
Returns ​
number | undefined
Remarks ​
Find the index of the last instance of an item inside the container
Throws ​
This function can throw errors.
firstEmptySlot() ​
firstEmptySlot():
number|undefined
Defined in: @minecraft/server/index.d.ts:6440
Returns ​
number | undefined
Remarks ​
Finds the index of the first empty slot inside the container
Throws ​
This function can throw errors.
firstItem() ​
firstItem():
number|undefined
Defined in: @minecraft/server/index.d.ts:6449
Returns ​
number | undefined
Remarks ​
Finds the index of the first item inside the container
Throws ​
This function can throw errors.
getItem() ​
getItem(
slot):ItemStack|undefined
Defined in: @minecraft/server/index.d.ts:6485
Parameters ​
slot ​
number
Zero-based index of the slot to retrieve items from. Minimum value: 0
Returns ​
ItemStack | undefined
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 ​
getFirstHotbarItem.ts
import { world, EntityInventoryComponent, DimensionLocation } from '@minecraft/server';
function getFirstHotbarItem(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
for (const player of world.getAllPlayers()) {
const inventory = player.getComponent(EntityInventoryComponent.componentId) as EntityInventoryComponent;
if (inventory && inventory.container) {
const firstItem = inventory.container.getItem(0);
if (firstItem) {
log('First item in hotbar is: ' + firstItem.typeId);
}
return inventory.container.getItem(0);
}
return undefined;
}
}getSlot() ​
getSlot(
slot):ContainerSlot
Defined in: @minecraft/server/index.d.ts:6499
Parameters ​
slot ​
number
The index of the slot to return. This index must be within the bounds of the container. Minimum value: 0
Returns ​
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.
moveItem() ​
moveItem(
fromSlot,toSlot,toContainer):void
Defined in: @minecraft/server/index.d.ts:6552
Parameters ​
fromSlot ​
number
Zero-based index of the slot to transfer an item from, on this container. Minimum value: 0
toSlot ​
number
Zero-based index of the slot to transfer an item to, on toContainer. Minimum value: 0
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 restricted-execution mode.
Throws ​
Throws if either this container or toContainer are invalid or if the fromSlot or toSlot indices out of bounds.
Error
Example ​
moveBetweenContainers.ts
import { world, EntityInventoryComponent, EntityComponentTypes, DimensionLocation } from '@minecraft/server';
import { MinecraftEntityTypes } from '@minecraft/vanilla-data';
function moveBetweenContainers(targetLocation: DimensionLocation) {
const players = world.getAllPlayers();
const chestCart = targetLocation.dimension.spawnEntity(MinecraftEntityTypes.ChestMinecart, {
x: targetLocation.x + 1,
y: targetLocation.y,
z: targetLocation.z,
});
if (players.length > 0) {
const fromPlayer = players[0];
const fromInventory = fromPlayer.getComponent(EntityComponentTypes.Inventory) as EntityInventoryComponent;
const toInventory = chestCart.getComponent(EntityComponentTypes.Inventory) as EntityInventoryComponent;
if (fromInventory && toInventory && fromInventory.container && toInventory.container) {
fromInventory.container.moveItem(0, 0, toInventory.container);
}
}
}setItem() ​
setItem(
slot,itemStack?):void
Defined in: @minecraft/server/index.d.ts:6573
Parameters ​
slot ​
number
Zero-based index of the slot to set an item at. Minimum value: 0
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 restricted-execution mode.
Throws ​
Throws if the container is invalid or if the slot index is out of bounds.
Error
swapItems() ​
swapItems(
slot,otherSlot,otherContainer):void
Defined in: @minecraft/server/index.d.ts:6597
Parameters ​
slot ​
number
Zero-based index of the slot to swap from this container. Minimum value: 0
otherSlot ​
number
Zero-based index of the slot to swap with. Minimum value: 0
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 restricted-execution mode.
Throws ​
Throws if either this container or otherContainer are invalid or if the slot or otherSlot are out of bounds.
Error
transferItem() ​
transferItem(
fromSlot,toContainer):ItemStack|undefined
Defined in: @minecraft/server/index.d.ts:6649
Parameters ​
fromSlot ​
number
Zero-based index of the slot to transfer an item from, on this container. Minimum value: 0
toContainer ​
Container
Target container to transfer to. Note this can be the same container as the source.
Returns ​
ItemStack | undefined
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 restricted-execution mode.
Throws ​
Throws if either this container or toContainer are invalid or if the fromSlot or toSlot indices out of bounds.
Error
Example ​
transferBetweenContainers.ts
import { world, EntityInventoryComponent, EntityComponentTypes, DimensionLocation } from '@minecraft/server';
import { MinecraftEntityTypes } from '@minecraft/vanilla-data';
function transferBetweenContainers(targetLocation: DimensionLocation) {
const players = world.getAllPlayers();
const chestCart = targetLocation.dimension.spawnEntity(MinecraftEntityTypes.ChestMinecart, {
x: targetLocation.x + 1,
y: targetLocation.y,
z: targetLocation.z,
});
if (players.length > 0) {
const fromPlayer = players[0];
const fromInventory = fromPlayer.getComponent(EntityComponentTypes.Inventory) as EntityInventoryComponent;
const toInventory = chestCart.getComponent(EntityComponentTypes.Inventory) as EntityInventoryComponent;
if (fromInventory && toInventory && fromInventory.container && toInventory.container) {
fromInventory.container.transferItem(0, toInventory.container);
}
}
}