类 BlockInventoryComponent实验性

表示方块在世界中的库存。用于像箱子这样的方块。

  • Represents the inventory of a block in the world. Used with blocks like chests.
import { ItemStack, BlockInventoryComponent, DimensionLocation } from "@minecraft/server";
import { MinecraftBlockTypes, MinecraftItemTypes } from "@minecraft/vanilla-data";

function placeItemsInChest(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
// Fetch block
const block = targetLocation.dimension.getBlock(targetLocation);

if (!block) {
log("Could not find block. Maybe it is not loaded?", -1);
return;
}

// Make it a chest
block.setType(MinecraftBlockTypes.Chest);

// Get the inventory
const inventoryComponent = block.getComponent("inventory") as BlockInventoryComponent;

if (!inventoryComponent || !inventoryComponent.container) {
log("Could not find inventory component.", -1);
return;
}

const inventoryContainer = inventoryComponent.container;

// Set slot 0 to a stack of 10 apples
inventoryContainer.setItem(0, new ItemStack(MinecraftItemTypes.Apple, 10));
}

层级 (查看层级一览)

属性

block: Block

此组件所属的方块实例。

Block instance that this component pertains to.

container?: Container

表示包含一个 ItemStack 的容器。

使用该属性时可能会抛出异常。

isValid: boolean

返回组件是否有效。若组件的拥有者有效,并且组件需要的任何 额外验证也通过,则该组件被认为是有效的。

Returns whether the component is valid. A component is considered valid if its owner is valid, in addition to any addition to any additional validation required by the component.

typeId: string

组件的标识符。 Identifier of the component.

componentId: "minecraft:inventory" = 'minecraft:inventory'