实验性
只读
实验性
empty只读
实验性
is只读
实验性
size实验性
要添加的物品堆。
The stack of items to add.
向容器中添加一个物品。物品将放置在第一个可用槽位中,并可以与相同类型的现有物品堆叠。 若希望将物品放置在特定槽位中,请使用 Container.setItem。
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.
无法在只读模式下调用此函数,详见 WorldBeforeEvents。
实验性
要从中检索物品的槽位的从零开始的索引。
Zero-based index of the slot to retrieve items from.
获取指定槽位中的 ItemStack。若槽位为空,则返回 undefined
。
此方法不会更改或清空指定槽位的内容。要获取某个槽位的引用,请参阅 Container.getSlot。
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.
若容器无效或 slot
索引超出范围,则抛出异常。
Throws if the container is invalid or if the slot
index is
out of bounds.
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.
无法在只读模式下调用此函数,详见 WorldBeforeEvents。
若此容器或 toContainer
无效,或者 fromSlot
或 toSlot
索引超出范围,则抛出异常。
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.
可选
itemStack: ItemStack要放置在指定槽位中的物品堆。将 itemStack
设置为 undefined 将清空该槽位。
Stack of items to place within the specified slot. Setting
itemStack
to undefined will clear the slot.
实验性
要从此容器交换的槽位的从零开始的索引。
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.
无法在只读模式下调用此函数,详见 WorldBeforeEvents。
实验性
要从中转移物品的槽位的从零开始的索引。
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.
一个包含无法转移的物品的物品堆。若所有物品都已转移,则返回 undefined。
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.
无法在只读模式下调用此函数,详见 WorldBeforeEvents。
若此容器或 toContainer
无效,或者 fromSlot
或 toSlot
索引超出范围,则抛出异常。
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.
示例: containers.ts