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.
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.
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;
}
}
The index of the slot to return. This index must be within the bounds of the container.
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.
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);
}
}
}
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.
Sets an item stack within a particular slot.
This function can't be called in read-only mode.
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.
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.
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);
}
}
}
Represents a container that can hold sets of items. Used with entities such as Players, Chest Minecarts, Llamas, and more.
Example: containers.ts