#help
1 messages · Page 1 of 1 (latest)
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
What is the error it throws console?
No error appears
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;
}```
ok i will try
It didn't work, or it did
Basically it is setting 0
Independent of quantity
Hm
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
Okay, the function returns true or false (ie 0 or 1). How are you using the function?
Okay, wait...
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?
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.```
I'll try