BetaPrivateconstructorOptional ReadonlycontainerIf 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.
This property can't be read in early-execution mode.
ReadonlyemptyThis property can't be read in early-execution mode.
ReadonlyisReturns whether a container object (or the entity or block that this container is associated with) is still available for use in this context.
This property can't be read in early-execution mode.
ReadonlysizeThe 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.
This property can't be read in early-execution mode.
ReadonlytitleThis property can't be read in early-execution mode.
ReadonlyweightThis property can't be read in early-execution mode.
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 restricted-execution mode.
Won't throw ContainerRules error for over weight limit but will instead add items up to the weight limit.
Error
This function can't be called in early-execution 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);
}
This function can't be called in restricted-execution mode.
This function can't be called in early-execution mode.
Optionalplayer: Player
The player whose screen to close. When omitted, every viewer's screen is closed.
The number of screens asked to close. A screen that is already closing is not counted again.
Requests closure of the container screen of a player, or of every player viewing this container when no player is given. From then on, those players can no longer change the container through their screens. Each screen raises a VirtualContainerCloseAfterEvent with the ServerClosed reason after the client has closed its UI and pending inventory requests have finished. Closure may complete on a later tick.
This function can't be called in restricted-execution mode.
This function can't be called in early-execution mode.
The item to find.
This function can't be called in early-execution mode.
The item to find.
This function can't be called in early-execution mode.
The item to find.
This function can't be called in early-execution mode.
This function can't be called in early-execution mode.
This function can't be called in early-execution mode.
Zero-based index of the slot to retrieve items from. Minimum value: 0
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;
}
}
This function can't be called in early-execution mode.
The index of the slot to return. This index must be within the bounds of the container. Minimum value: 0
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 early-execution mode.
The players that have a screen open onto this container.
This function can't be called in early-execution mode.
Zero-based index of the slot to transfer an item from, on this container. Minimum value: 0
Zero-based index of the slot to transfer an item to, on
toContainer.
Minimum value: 0
Target container to transfer to. Note this can be the same container as the source.
This function can't be called in restricted-execution mode.
Throws if either this container or toContainer are invalid
or if the fromSlot or toSlot indices out of bounds.
Error
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);
}
}
}
This function can't be called in early-execution mode.
The player to show the container to.
Shows a chest style screen onto this container to a player. Only that player sees the screen; any number of players can view the same container at once, each with a screen of their own. Changing dimension closes the screen with the ServerClosed reason, dying closes it with the PlayerDied reason, and switching to spectator mode closes it with the PlayerSpectating reason.
This function can't be called in restricted-execution mode.
Throws VirtualContainerOpenError when the screen request cannot be sent, with a reason that names why, such as the player still loading or already having a container screen open; try again later. Not throwing means the request was sent, not that the screen has already appeared: the player's device can still decline to show it, which closes it with the UserBusy reason. Throws InvalidEntityError if the player is no longer valid or is leaving the game.
This function can't be called in early-execution mode.
Zero-based index of the slot to set an item at. Minimum value: 0
OptionalitemStack: ItemStack
Stack of items to place within the specified slot. Setting
itemStack to undefined will clear the slot.
This function can't be called in restricted-execution mode.
This function can't be called in early-execution 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. Minimum value: 0
Zero-based index of the slot to swap with. Minimum value: 0
Target container to swap with. Note this can be the same container as this source.
This function can't be called in restricted-execution mode.
Throws if either this container or otherContainer are
invalid or if the slot or otherSlot are out of bounds.
Error
This function can't be called in early-execution mode.
Zero-based index of the slot to transfer an item from, on this container. Minimum value: 0
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 restricted-execution mode.
Throws if either this container or toContainer are invalid
or if the fromSlot or toSlot indices out of bounds.
Won't throw ContainerRules error for over weight
limit but will instead add items up to the weight limit.
Error
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);
}
}
}
This function can't be called in early-execution mode.
A container that only exists in memory. It is not tied to a block, an entity or the world, is never saved, and is destroyed once the script no longer references it and no player is viewing it. It supports the full Container API and can be shown to any number of players at once with open. When the world shuts down or scripts reload, open screens close without raising close events; save the contents from system.beforeEvents.shutdown if they need to outlive the session.
Source