@ascentbv/ts-common - v1.0.123
    Preparing search index...

    Class InventoryUtilAbstract

    Index

    Constructors

    Methods

    • Gets the equipment component from an entity if it exists.

      Parameters

      • entity: Entity

        The entity to query.

      Returns EntityEquippableComponent | undefined

      The entity's equippable component, or undefined.

      const equipment = InventoryUtil.getEquipment(player);
      if (equipment) {
      const helmet = equipment.getEquipmentSlot(EquipmentSlot.Head).getItem();
      }
    • Gets a container from an entity or container instance.

      Parameters

      Returns Container | undefined

      The container instance, or undefined if none exists.

      const container = InventoryUtil.getContainer(player);
      if (container) console.log(container.size);
    • Gets the currently selected hotbar slot of a player.

      Parameters

      • player: Player

        The player to query.

      Returns ContainerSlot | undefined

      The selected slot or undefined.

      const slot = InventoryUtil.getSelectedSlot(player);
      console.log(slot?.getItem()?.typeId);
    • Gets the currently selected item in a player's hand.

      Parameters

      • player: Player

        The player to query.

      Returns ItemStack | undefined

      The selected ItemStack or undefined.

      const item = InventoryUtil.getSelectedItem(player);
      if (item) console.log(`Player is holding ${item.typeId}`);
    • Sets the currently selected item in a player's hand. Respects creative mode - does nothing in creative.

      Parameters

      • player: Player

        The player to modify.

      • OptionalnewItem: ItemStack

        The new ItemStack to set (or undefined to clear).

      Returns void

      InventoryUtil.setSelectedItem(player, new ItemStack("minecraft:diamond_sword"));
      InventoryUtil.setSelectedItem(player, undefined); // Clear slot
    • Gets the mainhand equipment slot.

      Parameters

      • entity: Entity

        The entity to query.

      Returns ContainerSlot | undefined

      The mainhand ContainerSlot or undefined.

      const handSlot = InventoryUtil.getMainhandSlot(player);
      if (handSlot) handSlot.setItem(newItem);
    • Gets the currently equipped mainhand item.

      Parameters

      • entity: Entity

        The entity to query.

      Returns ItemStack | undefined

      The mainhand ItemStack or undefined.

      const mainhand = InventoryUtil.getMainhandItem(player);
      if (mainhand) console.log(mainhand.typeId);
    • Sets the mainhand equipment item.

      Parameters

      • entity: Entity

        The entity to modify.

      • Optionalitem: ItemStack

        The ItemStack to set (or undefined to clear).

      Returns void

      InventoryUtil.setMainhandItem(player, new ItemStack("minecraft:diamond_sword"));
      InventoryUtil.setMainhandItem(player, undefined); // Clear mainhand
    • Gets the currently equipped offhand item.

      Parameters

      Returns ItemStack | undefined

      The offhand ItemStack or undefined.

      const offhand = InventoryUtil.getOffhandItem(player);
      if (offhand) console.log(offhand.typeId);
    • Gets all items from a container or entity's inventory.

      Parameters

      Returns ItemStack[]

      An array of ItemStacks or undefined for empty slots.

      const items = InventoryUtil.getContainerItems(player);
      console.log(items.length);
    • Gets all items from a container or entity's inventory.

      Parameters

      • entity: Entity | Container

        Entity or container to check.

      • ordered: false

        If true, includes empty slots as undefined to preserve order.

      Returns ItemStack[]

      An array of ItemStacks or undefined for empty slots.

      const items = InventoryUtil.getContainerItems(player);
      console.log(items.length);
    • Gets all items from a container or entity's inventory.

      Parameters

      • entity: Entity | Container

        Entity or container to check.

      • ordered: true

        If true, includes empty slots as undefined to preserve order.

      Returns (ItemStack | undefined)[]

      An array of ItemStacks or undefined for empty slots.

      const items = InventoryUtil.getContainerItems(player);
      console.log(items.length);
    • Counts the total number of a specific item in a container or entity's inventory.

      Parameters

      • entity: Entity | Container

        Entity or container to check.

      • typeId: string

        Item typeId (e.g. minecraft:apple).

      Returns number

      Total item count.

      const apples = InventoryUtil.getItemTotal(player, "minecraft:apple");
      console.log(`Player has ${apples} apples`);
    • Consumes (removes one) of the player's selected item. Optionally spawns a return item at their location.

      Parameters

      • player: Player

        The player.

      • OptionalreturnItem: ItemStack

        Optional item to drop as replacement.

      • Optionalamount: number

        the amount to consume from their selected item, defaults to 1

      Returns void

      InventoryUtil.consumeSelectedItem(player, new ItemStack("minecraft:bucket"));
      
    • Reduces an ItemStack's amount by a given amount.

      Parameters

      • item: ItemStack

        The ItemStack to reduce.

      • amount: number = 1

        Amount to reduce by (default 1).

      Returns ItemStack | undefined

      The updated stack, or undefined if amount drops to 0.

      const newStack = InventoryUtil.reduceItemStack(item, 2);
      
    • Removes a single item by ID from a container or entity's inventory.

      Parameters

      Returns boolean

      True if an item was removed.

      const removed = InventoryUtil.removeSingleItem(player, "minecraft:arrow");
      
    • Removes an amount of an item by ID from a container or entity's inventory. It can fail if you require there to exist that amount of the item.

      Parameters

      • entity: Entity | Container

        Entity or Container.

      • itemId: string

        Item typeId.

      • itemAmount: number
      • requireAll: boolean = false

        If we need there to be at least the total amount of the item.

      Returns boolean

      True if all items were removed.

    • Removes an array of item types from a container or entity's inventory. It can fail if there is not enough of the item in the inventory

      Parameters

      • entity: Entity | Container

        Entity or Container.

      • items: { itemId: string; amount: number }[]

        An array of item types and amounts to remove from the player

      • requireAll: boolean = false

        The container must contain all of the items to remove them. Fails if any item amount is not enough

      Returns boolean

      True if all items were removed.

    • Gives an ItemStack to an entity's inventory, dropping any leftovers.

      Parameters

      • entity: Entity

        Target entity.

      • itemStack: ItemStack

        ItemStack to give.

      • lockMode: ItemLockMode = ItemLockMode.none

        Optional lock mode.

      • keepOnDeath: boolean = false

        Should item persist on death.

      Returns void

      InventoryUtil.giveItemStackToEntity(player, new ItemStack("minecraft:diamond", 5));
      
    • Creates a new ItemStack and gives it to an entity.

      Parameters

      • entity: Entity

        Target entity.

      • item: string

        Item typeId.

      • amount: number = 1

        Amount to give (default 1).

      • lockMode: ItemLockMode = ItemLockMode.none

        Optional lock mode.

      • keepOnDeath: boolean = false

        Should item persist on death.

      Returns void

    • Checks if an entity has an item matching the predicate.

      Parameters

      • entity: Entity

        Entity to check.

      • predicate: (item: ItemStack) => boolean

        Predicate function to match items.

      Returns boolean

      True if an item matches.

      const hasBow = InventoryUtil.hasItem(player, item => item.typeId === "minecraft:bow");
      
    • Consumes (removes one) of the first matching item in equipment or inventory.

      Parameters

      • entity: Entity

        Entity to modify.

      • predicate: (item: ItemStack) => boolean

        Predicate to match items.

      Returns boolean

      True if an item was consumed.

      const consumed = InventoryUtil.consumeItem(player, item => item.typeId === "minecraft:apple");
      if (consumed) console.log("A single apple was consumed from inventory or equipment");
    • Returns the slots that contains the item up to the amount specified. This can overshoot the amount of items.

      Parameters

      • entity: Entity | Container

        Entity or Container.

      • itemId: string

        Item typeId.

      • itemAmount: number
      • requireAll: boolean = false

        Only returns an array if they are enough items. Defaults to false

      Returns ContainerSlot[]

      True if all items were removed.

    • Checks if an entity or container has a completely empty inventory.

      Parameters

      Returns boolean

      True if all inventory slots are empty.

      const isEmpty = InventoryUtil.isInventoryEmpty(player);
      if (isEmpty) console.log("No items in inventory");