Skip to content

@minecraft/server


@minecraft/server / ItemDurabilityComponent

Class: ItemDurabilityComponent ​

Defined in: @minecraft/server/index.d.ts:14034

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

typescript
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

Defined in: @minecraft/server/index.d.ts:14043

Remarks ​

Returns the current damage level of this particular item.

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


isValid ​

readonly isValid: boolean

Defined in: @minecraft/server/index.d.ts:6234

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

Defined in: @minecraft/server/index.d.ts:14051

Remarks ​

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

Throws ​

This property can throw when used.


typeId ​

readonly typeId: string

Defined in: @minecraft/server/index.d.ts:6240

Remarks ​

Identifier of the component.

Inherited from ​

ItemComponent.typeId


unbreakable ​

unbreakable: boolean

Defined in: @minecraft/server/index.d.ts:14061

Remarks ​

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

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


componentId ​

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

Defined in: @minecraft/server/index.d.ts:14062

Methods ​

getDamageChance() ​

getDamageChance(unbreakingEnchantmentLevel?): number

Defined in: @minecraft/server/index.d.ts:14079

Parameters ​

unbreakingEnchantmentLevel? ​

number

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 ​

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

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

Throws ​

This function can throw errors.


getDamageChanceRange() ​

getDamageChanceRange(): NumberRange

Defined in: @minecraft/server/index.d.ts:14090

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.

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

Throws ​

This function can throw errors.