#I have a problem in my code

1 messages · Page 1 of 1 (latest)

thorny stream
#
    function sellshop() {
        Container.prototype.hasItem = function (typeId,amount = undefined){
            for (let i = 0; i < this.size; i++){
                const item = this.getItem(i)
                return amount === undefined ? item.typeId === typeId : item.typeId === typeId && item.amount === amount 
            }
            return false
        }
        const inv = player.getComponent('inventory').container
        const sellshop = new ActionFormData()
        sellshop.button("§l§7Stone")
        sellshop.button("§lCharbon")
        sellshop.button("§l§eFer")
        sellshop.button("§l§6Or")
        sellshop.button("§l§1Lapis")
        sellshop.button("§l§4Redstone")
        sellshop.button("§l§bDiamand")
        sellshop.button("§l§2Emeuraude")
        sellshop.button("§l§uNano")
        sellshop.button("§l§cRetour")
            sellshop.show(player).then(r => {
                        if (r.selection == 0) {
                            if (inv.hasItem('minecraft:stone')) {
                                player.sendMessage("vous avez de la stone")
                            }else {
                                player.sendMessage("Vous n'avez pas de stone")
                            }
                          }
                        if (r.selection == 9) {
                            Mainshop()
                          }
                })
            }
#

why does it mark that when I have stone in my inventory

autumn raven
#

you havnt specified what causes it to have stone inventory but im guessing it considers things such as andesite or diorite as stone. This is because they have the same typeId as stone

#

currently in gametest there isnt a way to differentiate between the permutation / data values of items

thorny stream
magic current
#

Doesn't that function only check the first slot because you return right after checking that?

thorny stream
#

thank you for that indeed

thorny stream
magic current
#

for what exactly?

thorny stream
#
        Container.prototype.hasItem = function (typeId,amount = undefined){
            for (let i = 0; i < this.size; i++){
                const item = this.getItem(i)
                return amount === undefined ? item.typeId === typeId : item.typeId === typeId && item.amount === amount 
            }
            return false
        }
        const inv = player.getComponent('inventory').container
#

for that

magic current
#

that function should go through all inventory slots, check if it's the specified item and if the amount is correct and if so return true, otherwise go to the next slot, right?

thorny stream
#

This could work if we can execute an action on each slot which checks the condition

magic current
#

Is this what you try to do?

Container.prototype.hasItem = function (typeId,amount = undefined){
  for (let i = 0; i < this.size; i++){
    const item = this.getItem(i)

    if (item.amount == amount && item.typeId == typeId) return true
  }
  return false
}

const inv = player.getComponent('inventory').container
thorny stream
#

in fact if you want I want to make a sales menu that means that when you click on the button above it sells all the specified item

#

my explanation may help you

#

@magic current

magic current
#

So you want to get the total amount of the specified item in someones inventory?

magic current
#

Do you mean something like this then?

Container.prototype.hasItem = function (typeId,amount = undefined){
let count = 0
  for (let i = 0; i < this.size; i++){
    const item = this.getItem(i)

    if (item.typeId == typeId) count += item.amount
  }
  return count
}

const inv = player.getComponent('inventory').container

const amount = inv.hasItem("minecraft:stone")
console.warn(amount)
thorny stream
#

when I run the code I get an error

#

should I add something in import minecraft server?

magic current
#

if (item?.typeId == typeId) count += item.amount

#

that happens when it tries to read the typeId of an empty stack

thorny stream
#

Oh ok

#

Thank you very much

magic current
#

np :3

thorny stream
#

you save me a lot of time

thorny stream
#

i have last question

#

why it doesn't clear the stone

#

@magic current

magic current
#

clear [player: target] [itemName: Item] [data: int] [maxCount: int]
You try to clear every stone with the id of the amount of stone

#

if you want to clear all of them just remove the 3rd parameter