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()
}
})
}
#I have a problem in my code
1 messages · Page 1 of 1 (latest)
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
you tell me then that it tests if I have a complete inventory of stone
Doesn't that function only check the first slot because you return right after checking that?
thank you for that indeed
So I would have to create a function just for that
for what exactly?
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
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?
This could work if we can execute an action on each slot which checks the condition
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
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
So you want to get the total amount of the specified item in someones inventory?
exactly
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)
when I run the code I get an error
should I add something in import minecraft server?
if (item?.typeId == typeId) count += item.amount
that happens when it tries to read the typeId of an empty stack
np :3
you save me a lot of time