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

Hierarchy (View Summary)

Constructors

Properties

damage: number

Returns the current damage level of this particular item.

This property can't be edited in read-only mode.

isValid: boolean

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.

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'

Methods