Script API - v1.21.130
    Preparing search index...

    Class ItemDurabilityComponent

    Advertisement

    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);
    }
    }
    }
    import { world } from "@minecraft/server";

    const player = world.getPlayers()[0];
    const inventory = player.getComponent("inventory");
    const slot = inventory.container.getSlot(player.selectedSlotIndex);
    let durabilityComp = slot.getItem().getComponent("durability");
    player.sendMessage(
    "Item Durability: " + (durabilityComp.maxDurability - durabilityComp.damage) + "/" + durabilityComp.maxDurability
    );

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    damage: number

    Returns the current damage level of this particular item.

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

    This property can't be read in early-execution 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.

    This property can't be read in early-execution mode.

    maxDurability: number

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

    This property can throw when used.

    This property can't be read in early-execution mode.

    typeId: string

    Identifier of the component.

    This property can't be read in early-execution mode.

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

    Methods

    • Parameters

      • OptionalunbreakingEnchantmentLevel: number

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

      Returns number

      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 read-only mode.

      This function can throw errors.

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

    • Returns NumberRange

      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 read-only mode.

      This function can throw errors.

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

    Advertisement