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

Class: ItemDurabilityComponent

component

Beta

表示物品耐久组件。当出现在物品上时,表示该物品在使用过程中会损耗耐久。 注意,此组件仅对数驱物品生效。

When present on an item, this item can take damage in the process of being used. Note that this component only applies to data-driven items.

Example

giveHurtDiamondSword.ts

import {
  world,
  ItemStack,
  EntityInventoryComponent,
  EntityComponentTypes,
  ItemComponentTypes,
  ItemDurabilityComponent,
  DimensionLocation,
} from '@minecraft/server';
import { MinecraftItemTypes } from '@minecraft/vanilla-data';

function giveHurtDiamondSword(targetLocation: DimensionLocation) {
  const hurtDiamondSword = new ItemStack(MinecraftItemTypes.DiamondSword);

  const durabilityComponent = hurtDiamondSword.getComponent(ItemComponentTypes.Durability) as ItemDurabilityComponent;

  if (durabilityComponent !== undefined) {
    durabilityComponent.damage = durabilityComponent.maxDurability / 2;
  }

  for (const player of world.getAllPlayers()) {
    const inventory = player.getComponent(EntityComponentTypes.Inventory) as EntityInventoryComponent;
    if (inventory && inventory.container) {
      inventory.container.addItem(hurtDiamondSword);
    }
  }
}

Extends

Properties

damage

damage: number

World Mutation Beta

Remarks

此物品当前的损坏值。 物品当前耐久度为 maxDurability - damage。 当被设置为 负数,InfinityNaN 等值时,值为 0。

Returns the current damage level of this particular item.


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

ItemComponent.isValid


maxDurability

readonly maxDurability: number

Beta

Remarks

表示该物品在损坏前可以承受的损坏值。

Represents the amount of damage that this item can take before breaking.

Throws

This property can throw when used.


typeId

readonly typeId: string

Beta

Remarks

组件的标识符。

Identifier of the component.

Inherited from

ItemComponent.typeId


unbreakable

unbreakable: boolean

World Mutation Beta

Remarks

表示物品是否会损坏或损失耐久度。将其设置为 true 会暂时移除物品的耐久度 HUD,并暂停该物品的耐久度损失。

Whether an item breaks or loses durability. Setting to true temporarily removes item's durability HUD, and freezes durability loss on item.


componentId

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

Beta

Methods

getDamageChance()

getDamageChance(unbreakingEnchantmentLevel?): number

World Mutation Beta

Parameters

unbreakingEnchantmentLevel?

number

耐久魔咒等级,在计算损坏概率时受到此参数的影响。 传入的 unbreakingEnchantmentLevel 参数必须介于 [0, 3]。

Unbreaking factor to consider in factoring the damage chance. Incoming unbreaking parameter must be within the range [0, 3]. Defaults to: 0 Bounds: [0, 3]

Returns

number

使用时的最大损坏概率。

Remarks

返回根据 damageRange 属性生成的最大损坏概率, 附带一个耐久附魔等级作为可选参数。

Returns the maximum chance that this item would be damaged using the damageRange property, given an unbreaking enchantment level.

Throws

unbreakingEnchantmentLevel 参数未在范围内时,抛出 TypeError


getDamageChanceRange()

getDamageChanceRange(): NumberRange

World Mutation Beta

Returns

NumberRange

Remarks

返回用于计算物品损失耐久的概率范围。最终物品损失耐久的概率将落在此范围中。

A range of numbers that is used to calculate the damage chance for an item. The damage chance will fall within this range.

Throws

This function can throw errors.

同领域相关