For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /server/classes/BlockInventoryComponent.md.

Class: BlockInventoryComponent

block inventory component

Beta

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

Example

placeItemsInChest.ts

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));
}

Extends

Properties

block

readonly block: Block

Beta

Remarks

此组件所属的方块。

Block instance that this component pertains to.

Inherited from

BlockComponent.block


container?

readonly optional container?: Container

Beta

Remarks

持有 ItemStack 的容器。

Throws

This property can throw when used.


isValid

readonly isValid: boolean

Beta

Remarks

返回该组件是否有效。如果组件的拥有者有效,并且满足组件所需的任何额外验证,则该组件被视为有效。

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.

Inherited from

BlockComponent.isValid


typeId

readonly typeId: string

Beta

Remarks

组件的标识符。

Identifier of the component.

Inherited from

BlockComponent.typeId


componentId

readonly static componentId: "minecraft:inventory" = 'minecraft:inventory'

Beta

同领域相关