Edit

ItemDurabilityComponent Class

Extends

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.

Properties

damage

damage: number;

Returns the current damage level of this particular item.

Type: number

Notes:

  • This property can't be edited in restricted-execution mode.

maxDurability

read-only maxDurability: number;

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

Type: number

Notes:

  • This property can throw errors when used.

unbreakable

unbreakable: boolean;

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

Type: boolean

Notes:

  • This property can't be edited in restricted-execution mode.

Methods

getDamageChance

getDamageChance(unbreakingEnchantmentLevel?: number): number

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

Parameters

  • unbreakingEnchantmentLevel?: number = 0

    • Bounds: [0, 3]

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

Returns number

Notes:

  • This function can't be called in restricted-execution mode.
  • This function can throw errors.

getDamageChanceRange

getDamageChanceRange(): minecraftcommon.NumberRange

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

Returns @minecraft/common.NumberRange

Notes:

  • This function can't be called in restricted-execution mode.
  • This function can throw errors.

Constants

componentId

static read-only componentId = "minecraft:durability";

Type: string

Examples

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

(preview) Work with this sample on the MCTools.dev code sandbox.