类 ItemDurabilityComponent实验性

表示物品耐久组件。当出现在物品上时,表示该物品可以在使用中受到损坏。 注意,只能在数驱物品上获取和使用该组件。

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.

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

层级 (查看完整内容)

属性

damage: number

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

Returns the current damage level of this particular item.

无法在只读模式下修改此属性,详见 WorldBeforeEvents

maxDurability: number

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

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

This property can throw when used.

typeId: string

Identifier of the component.

componentId: "minecraft:durability" = 'minecraft:durability'

方法

  • 实验性

    参数

    • 可选unbreakingEnchantmentLevel: number

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

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

    返回 number

    使用时的最大损坏概率。

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

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

    无法在只读模式下调用此函数,详见 WorldBeforeEvents

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

  • 实验性

    返回 NumberRange

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

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

    无法在只读模式下调用此函数,详见 WorldBeforeEvents

    This function can throw errors.

  • 实验性

    返回 boolean

    Whether the component is valid.

    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.