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/BlockDynamicPropertiesComponent.md.

Class: BlockDynamicPropertiesComponent experimental

block component

Beta

表示世界中方块的动态属性。仅适用于方块实体。每个内容包、每个方块实体在其动态属性存储中最多 1KB。

Example

rememberPlayerInteraction.ts

import { system } from '@minecraft/server-wrapper';

system.beforeEvents.startup.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('scripting_demo_pack:block_entity_onPlayerInteract', {
    onPlayerInteract: e => {
      if (e.player === undefined) {
        return;
      }

      const dynamicProperties = e.block.getComponent('minecraft:dynamic_properties');
      if (!dynamicProperties) {
        return;
      }

      const lastInteractorValue = dynamicProperties.get('last_interactor');
      const lastVisitor = typeof lastInteractorValue === 'string' ? lastInteractorValue : 'unknown';
      const lastTick = Number(dynamicProperties.get('last_interact_tick') ?? system.currentTick);
      const ticksAgo = Math.max(0, system.currentTick - lastTick);

      if (lastVisitor === e.player.name) {
        e.player.sendMessage("do you remember that player? I 'member, it was here " + String(ticksAgo) + ' ticks ago!');
      } else {
        e.player.sendMessage("oh, I don't remember that player");
      }

      dynamicProperties.set('last_interactor', e.player.name);
      dynamicProperties.set('last_interact_tick', system.currentTick);
    },
  });
});

Extends

Properties

block

readonly block: Block

Beta

Remarks

此组件所属的方块。

Block instance that this component pertains to.

Inherited from

BlockComponent.block


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:dynamic_properties" = 'minecraft:dynamic_properties'

Beta

Methods

get()

get(key): string | number | boolean | Vector3 | undefined

Beta

Parameters

key

string

Returns

string | number | boolean | Vector3 | undefined

Remarks

返回使用提供的键存储的动态属性。键对于每个内容包是唯一的,不能用于检索从其他内容包设置的动态属性。 如果未找到键,则返回 undefined

Returns a DynamicProperty that was stored with the provided key. Keys are unique to each content pack and cannot be used to retrieve dynamic properties set from other content packs. Returns undefined if the key was not found.

Throws

This function can throw errors.

Error

InvalidBlockComponentError

LocationInUnloadedChunkError

LocationOutOfWorldBoundariesError


set()

set(key, value?): void

World Mutation Beta

Parameters

key

string

value?

string | number | boolean | Vector3

Returns

void

Remarks

使用提供的键和值设置动态属性。键对于每个内容包是唯一的,不能用于为其他内容包设置动态属性。 值可以是 Number、String 或 Vector3。使用 undefined 值设置属性将将其从存储中移除。 存储大小使用计入每个内容包 1KB 的限制。

Sets a dynamic property with the provided key and value. Keys are unique to each content pack and cannot be used to set dynamic properties for other content packs. Values can be either a Number, a String or a Vector3. Setting a property with an undefined value will remove it from the storage. Storage size usage is counted towards the 1KBytes limit per content pack.

Throws

This function can throw errors.

Error

InvalidBlockComponentError

LocationInUnloadedChunkError

LocationOutOfWorldBoundariesError


totalByteCount()

totalByteCount(): number

Beta

Returns

number

Remarks

返回此方块实体动态属性存储的当前大小(以字节为单位)。字节计数仅计算由您的内容包设置的属性。 1KB 限制是每个内容包的。

Returns the current size, in bytes, of the dynamic properties storage for this block entity. The byte count only accounts for properties set by your content pack. The 1KBytes limit is per content pack.

Throws

This function can throw errors.

Error

InvalidBlockComponentError

LocationInUnloadedChunkError

LocationOutOfWorldBoundariesError

同领域相关