#How to get specific inventory items
1 messages · Page 1 of 1 (latest)
What do you mean specific inventory items?
how
Well I think I kind a get you so here
Wdym how, you test for an amount of items, and clear all of that specific item, then run the command that is a scorebaord to give them money
I have a banned item system that works but idk how to modify it for testing for the amount of an item
like how do you want it to work
it'll be in a function that is called by a button in a UI
ok
i have one if you want it
function sell_menu(player) {
const sellMenu = new ActionFormData()
.title("§l§r§•§l§h[§uSolar §gNetwork §aSell §hMenu]")
.body('§•§l§hWelcome, §9${player.nameTag}§h. \nBalance: §a$${currentMoney}, ')
.button("§•§l§h-> §aSell §nItem §h<-") // Selection 0
.button("§•§l§h-> §aSell §aAll §h<-") // Selection 1
.button("§•§l§h-> §aSell §sInfo §h<-") // Selection 2
.button("§•§l§h-> §dMain §hMenu <-") // Selection 3
sellMenu.show(player).then(r => {
if (r.selection == 0) sell_item_menu(player);
if (r.selection == 1) sell_all_menu(player);
if (r.selection == 2) sell_info(player);
if (r.selection == 3) exit(player);
})
}
sell_item_menu is the function call
then inside of those, there will be more buttons with every item, which calls the sellDirt function
that is just an example
Send me yours in DMs
Send here
Ill add it in my “someone scripts” folder
@devout sundial
Im makign a Website that has codes for stuff like this
Could you send me the script though? I would like to see the script
Yo once I get on I’ll make those for you
summarize, how exactly do you want it?
the user would push a button in a ui, it will call a sellDirt function for example, that function should then get all of the dirt in the user's inventory, remove and give them money, in this example, 1 per dirt block
I was also thinking about making a sellAll function which does what I mentioned above, just for all the available blocks to sell
it seems simple, I just need to know how to get the amount of items of a certain user in a function
/**
* check item count
* @param {player} player player wants to get
* @param {itemId} itemId id of the item you want to get
* @example getItemCount(player, "minecraft:coal")
* @returns
*/
function getItemCount(player, itemId) {
if (!itemId) return 0;
let itemCount = 0;
const inventory = player.getComponent("inventory").container;
for (let i = 0; i < inventory.size; i++) {
const item = inventory.getItem(i);
if (item?.typeId == itemId) itemCount += item.amount;
}
return itemCount;
}
would I declare itemId in the getItemCount?
Or is that just to get the item, not to add anything to the user's balance?
It is just the function of scanning all types listed in the jaw and only returns the quantity
ah, so I would need a different function to sell the dirt, I would just multiply the itemCount, by the amount
What do you mean, "jaw"?
mean function
no, just getItemCount and * cost
right, in the sellDirt function
so what I could do, in the sellDirt function is make itemId = "minecraft:dirt", then call the get item, do a scoreboard line to add the money after the function is called
yep
Sweet, thank you!
You can take a look at this as a reference — I’m the one who wrote it.
oh interesting, I think I know what I want to do with my system though, thank you!
now if the getitem is in a sellfunction but that's all I need it for, I would only export the sellDirt functions?
you mean call it in other files?
yes, I know if I write the function in a sell_function.js file, I need to add export infront of sell dirt
then import the sell dirt
I'm just asking if I would have to import the get item function
separate it instead of together.
how so, just have the getitem in its own file?
export function getItemCount
then in sell dirt, just say
itemId = "minecraft:dirt
getItemCount(player)?
const coal = getItemCount(player, "minecraft:coal")
if (coal >= 64) {
//code
}
I'm starting to get lost lol
You can refer to this piece of code. I’ve written a simple version to help you visualize it more easily.
function getItemCount(player, itemId, remove = false) {
if (!itemId) return 0;
let count = 0;
const inventory = player.getComponent("inventory").container;
for (let i = 0; i < inventory.size; i++) {
const item = inventory.getItem(i);
if (item?.typeId === itemId) {
count += item.amount;
if (remove) inventory.setItem(i, undefined);
}
}
return count;
}
function specal(word) {
return word.split(":")[1]
.split("_")
.map(w => w.charAt(0).toUpperCase() + w.slice(1))
.join(" ");
}
function getScore(player) {
const ob = world.scoreboard.getObjective(dbI.Objective) ?? world.scoreboard.addObjective(dbI.Objective);
return {
ob,
score: ob.hasParticipant(player) ? ob.getScore(player) : 0
};
}
const dbI = {
deny: ["minecraft:coal"], //Block(s) in the list are prohibited.
cost: 5, //The price of each block.
Objective: "money", //Scoreboard name
ore: { //List of sellable blocks.
'minecraft:dirt': 1,
'minecraft:cobblestone': 2,
'minecraft:stone': 3,
'minecraft:cobbled_deepslate': 5,
'minecraft:deepslate': 7,
'minecraft:coal': 9,
'minecraft:raw_copper': 12,
'minecraft:raw_iron': 15,
'minecraft:raw_gold': 18,
'minecraft:redstone': 20,
'minecraft:lapis_lazuli': 25,
'minecraft:emerald': 30,
'minecraft:obsidian': 40,
'minecraft:diamond': 50,
'minecraft:ancient_debris': 70
}
};
I added some stuff but I can’t quite remember what
Yeah I changed it a lot