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

Class: BlockPermutation

block

Beta

包含类型 BlockType 和属性(有时也称为方块状态)的组合,用于描述一个方块(但不属于特定的 Block)。

Example

addTranslatedSign.ts

import { world, BlockPermutation, BlockSignComponent, BlockComponentTypes, DimensionLocation } from '@minecraft/server';
import { MinecraftBlockTypes } from '@minecraft/vanilla-data';

function addTranslatedSign(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
  const players = world.getPlayers();

  const dim = players[0].dimension;

  const signBlock = dim.getBlock(targetLocation);

  if (!signBlock) {
    log('Could not find a block at specified location.');
    return -1;
  }
  const signPerm = BlockPermutation.resolve(MinecraftBlockTypes.StandingSign, { ground_sign_direction: 8 });

  signBlock.setPermutation(signPerm);

  const signComponent = signBlock.getComponent(BlockComponentTypes.Sign) as BlockSignComponent;

  signComponent?.setText({ translate: 'item.skull.player.name', with: [players[0].name] });
}

Properties

localizationKey

readonly localizationKey: string

Beta

Remarks

此方块排列的名称在 .lang 文件中使用的本地化键。

Key for the localization of this BlockPermutation's name used in .lang files.


type

readonly type: BlockType

Beta

Remarks

置换所具有的 BlockType

The BlockType that the permutation has.

Methods

canBeDestroyedByLiquidSpread()

canBeDestroyedByLiquidSpread(liquidType): boolean

Beta

Parameters

liquidType

Water

此函数应为之调用的液体类型。

The type of liquid this function should be called for.

Returns

boolean

此方块在接触液体时是否被移除。

Whether this block is removed when touched by liquid.

Remarks

返回此方块在接触液体时是否被移除。

Returns whether this block is removed when touched by liquid.

Throws

This function can throw errors.


canContainLiquid()

canContainLiquid(liquidType): boolean

Beta

Parameters

liquidType

Water

此函数应为之调用的液体类型。

The type of liquid this function should be called for.

Returns

boolean

此方块是否可以在其上放置液体。

Whether this block can have a liquid placed over it.

Remarks

返回此方块是否可以在其上放置液体,即被水浸没。

Returns whether this block can have a liquid placed over it, i.e. be waterlogged.

Throws

This function can throw errors.


getAllStates()

getAllStates(): Record<string, boolean | number | string>

Beta

Returns

Record<string, boolean | number | string>

返回置换所具有的所有方块状态的列表。

Returns the list of all of the block states that the permutation has.

Remarks

返回与此方块关联的所有可用方块状态。

Returns all available block states associated with this block.


getItemStack()

getItemStack(amount?): ItemStack | undefined

Beta

Parameters

amount?

number

要放入原型物品堆中的此方块排列的数量。 默认为:1 范围:[1, 255]

Number of instances of this block to place in the prototype item stack. Defaults to: 1 Bounds: [1, 255]

Returns

ItemStack | undefined

Remarks

基于此方块置换检索一个原型物品堆,可用于物品 Container/ContainerSlot API

Retrieves a prototype item stack based on this block permutation that can be used with item Container/ContainerSlot APIs.


getState()

getState<T>(stateName): any

Beta

Type Parameters

T

T extends string | number | symbol

Parameters

stateName

T

要返回值的方块状态名称。

Name of the block state who's value is to be returned.

Returns

any

如果置换具有该状态则返回状态值,否则返回 undefined

Returns the state if the permutation has it, else undefined.

Remarks

获取置换的状态。

Gets a state for the permutation.


getTags()

getTags(): string[]

Beta

Returns

string[]

Remarks

创建置换的副本。

Creates a copy of the permutation.


hasTag()

hasTag(tag): boolean

Beta

Parameters

tag

string

Returns

boolean

如果置换具有该标签则返回 true,否则返回 false

Returns true if the permutation has the tag, else false.

Remarks

检查置换是否具有特定标签。

Checks to see if the permutation has a specific tag.

Example

checkBlockTags.ts

import { DimensionLocation } from '@minecraft/server';

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

  // check that the block is loaded
  if (block) {
    log(`Block is dirt: ${block.hasTag('dirt')}`);
    log(`Block is wood: ${block.hasTag('wood')}`);
    log(`Block is stone: ${block.hasTag('stone')}`);
  }
}

isLiquidBlocking()

isLiquidBlocking(liquidType): boolean

Beta

Parameters

liquidType

Water

此函数应为之调用的液体类型。

The type of liquid this function should be called for.

Returns

boolean

此方块是否阻止液体流动。

Whether this block stops liquid from flowing.

Remarks

返回此方块是否阻止液体流动。

Returns whether this block stops liquid from flowing.

Throws

This function can throw errors.


liquidSpreadCausesSpawn()

liquidSpreadCausesSpawn(liquidType): boolean

Beta

Parameters

liquidType

Water

此函数应为之调用的液体类型。

The type of liquid this function should be called for.

Returns

boolean

此方块在接触液体时是否被移除并生成其物品。

Whether this block is removed and spawns its item when touched by liquid.

Remarks

返回此方块在接触液体时是否被移除并生成其物品。

Returns whether this block is removed and spawns its item when touched by liquid.

Throws

This function can throw errors.


matches()

matches<T>(blockName, states?): boolean

Beta

Type Parameters

T

T extends string = MinecraftBlockTypes

Parameters

blockName

T

要比较的可选状态集合。

An optional set of states to compare against.

states?

BlockStateArg<T>

Returns

boolean

Remarks

返回指定置换是否与此置换匹配。如果未指定状态,则 matches 会在更广泛的类型集合上进行检查。

Returns a boolean whether a specified permutation matches this permutation. If states is not specified, matches checks against the set of types more broadly.


withState()

withState<T>(name, value): BlockPermutation

Beta

Type Parameters

T

T extends string | number | symbol

Parameters

name

T

方块属性的标识符。

Identifier of the block property.

value

BlockStateSuperset

方块属性的值。

Value of the block property.

Returns

BlockPermutation

Remarks

返回一个设置了特定属性的派生方块排列。

Returns a derived BlockPermutation with a specific property set.

Throws

This function can throw errors.


resolve()

static resolve<T>(blockName, states?): BlockPermutation

Beta

Type Parameters

T

T extends string = MinecraftBlockTypes

Parameters

blockName

T

要检查的方块的标识符。

Identifier of the block to check.

states?

BlockStateArg<T>

Returns

BlockPermutation

Remarks

给定类型标识符和可选的属性集合,将返回一个可在其他方块 API 中使用的方块排列对象(例如 block.setPermutation)。

Given a type identifier and an optional set of properties, will return a BlockPermutation object that is usable in other block APIs (e.g., block.setPermutation)

Throws

This function can throw errors.

Example

addBlockColorCube.ts

import { BlockPermutation, DimensionLocation } from '@minecraft/server';
import { Vector3Utils } from '@minecraft/math';
import { MinecraftBlockTypes } from '@minecraft/vanilla-data';

function addBlockColorCube(targetLocation: DimensionLocation) {
  const allWoolBlocks: string[] = [
    MinecraftBlockTypes.WhiteWool,
    MinecraftBlockTypes.OrangeWool,
    MinecraftBlockTypes.MagentaWool,
    MinecraftBlockTypes.LightBlueWool,
    MinecraftBlockTypes.YellowWool,
    MinecraftBlockTypes.LimeWool,
    MinecraftBlockTypes.PinkWool,
    MinecraftBlockTypes.GrayWool,
    MinecraftBlockTypes.LightGrayWool,
    MinecraftBlockTypes.CyanWool,
    MinecraftBlockTypes.PurpleWool,
    MinecraftBlockTypes.BlueWool,
    MinecraftBlockTypes.BrownWool,
    MinecraftBlockTypes.GreenWool,
    MinecraftBlockTypes.RedWool,
    MinecraftBlockTypes.BlackWool,
  ];

  const cubeDim = 7;

  let colorIndex = 0;

  for (let x = 0; x <= cubeDim; x++) {
    for (let y = 0; y <= cubeDim; y++) {
      for (let z = 0; z <= cubeDim; z++) {
        colorIndex++;
        targetLocation.dimension
          .getBlock(Vector3Utils.add(targetLocation, { x, y, z }))
          ?.setPermutation(BlockPermutation.resolve(allWoolBlocks[colorIndex % allWoolBlocks.length]));
      }
    }
  }
}

同领域相关