#help

1 messages · Page 1 of 1 (latest)

fickle mango
#

I want to detect an item in the inventory and its quantity in an if, but it is not detecting the quantity

#

This is the code to detect

#
export function checkInventory(player, item, amount) {
  const inventory = player.getComponent('minecraft:inventory').container

  let items = []

  for (let slot = 0; slot < inventory.size; slot++) {
    let Item = inventory.getItem(slot)

    if (Item != undefined) {
      let ItemAmount = inventory.getItem(slot).amount

      let ItemInfo = {
        item: Item,
        amount: ItemAmount
      }
      items.push(ItemInfo)
    }
  }

  let results = []

  const found = items.forEach(i => {
    if (i.item.typeId == item) {
      results.push(i)
    }
  })

  let sum = 0

  results.forEach(i => {
    sum = sum + i.amount
  })

  let resultsSum = sum

  if (results != undefined) {
    if (resultsSum >= amount) return true
  } else return false
}
```js
#

And this is the if

#
    if (checkInventory(player, 'db:dll') == 2) {
```js
fresh bay
#

What is the error it throws console?

fickle mango
#

No error appears

fresh bay
#

And if you try this:

import { EntityInventoryComponent, ItemStack } from "@minecraft/server";
export function checkInventory(player, item, amount) {
    let testAmount = 0;
    const inv = player.getComponent(EntityInventoryComponent.componentId).container;
    for (let i = 0; i < inv.size; i++) {
        const itm = inv.getItem(i);
        const id = item instanceof ItemStack ? item.typeId : item;
        if (itm?.typeId == id)
            testAmount += itm.amount;
    }
    if (testAmount >= amount)
        return true;
    else
        return false;
}```
fickle mango
#

ok i will try

#

It didn't work, or it did

#

Basically it is setting 0

#

Independent of quantity

fresh bay
#

Yeah, you need the third parameter, the amount to test.

#

Oh wait

fickle mango
#

Hm

fickle mango
#

I changed the test number and it still set to 0

#

What would this third parameter look like in code?

#

I'm probably doing it wrong

fresh bay
#

Okay, the function returns true or false (ie 0 or 1). How are you using the function?

#

Okay, wait...

fickle mango
#

The if is in a system.Interval, And inside it has 7 ifs, each if performs a function depending on the amount of the item in the inventory

#

That is, if it has 2 quantities of the item, it executes one thing, if it has 3 it executes another

#

But regardless of the amount I put, it runs as if it were 0

#

Do you want me to send you the file to see?

fresh bay
#
export function checkInventory(player, item, amount = 0) {
    let testAmount = 0;
    const inv = player.getComponent(EntityInventoryComponent.componentId).container;
    for (let i = 0; i < inv.size; i++) {
        const itm = inv.getItem(i);
        const id = item instanceof ItemStack ? item.typeId : item;
        if (itm?.typeId == id)
            testAmount += itm.amount;
    }
    if (testAmount >= amount)
        return (amount == 0 ? true : testAmount);
    else
        return false;
}

const amount = 45;

checkInventory(player, "ns:item_id") //returns the amount
checkInventory(player, "ns:item_id", amount) //returns true or false.```
fickle mango
#

I'll try

fickle mango
#

I can't get it to work, I'm testing it here but it seems I'm putting it wrong

#

wait

#

What was supposed to return the amount is returning true

#

If you can take a look, I deleted the attempts I made now