#How to get specific inventory items

1 messages · Page 1 of 1 (latest)

tidal citrus
#

I need help trying to figure out how to make a sell system, I currently have no code as I don't know where to start

light bane
#

What do you mean specific inventory items?

light bane
light bane
tidal citrus
# devout sundial how

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

devout sundial
#

like how do you want it to work

tidal citrus
#

it'll be in a function that is called by a button in a UI

tidal citrus
#
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

tidal citrus
copper lichen
#

Ill add it in my “someone scripts” folder

tidal citrus
#

@devout sundial

devout sundial
tidal citrus
light bane
golden minnow
#

summarize, how exactly do you want it?

tidal citrus
#

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

golden minnow
# tidal citrus I was also thinking about making a `sellAll` function which does what I mentione...
/**
 * 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;
}
tidal citrus
#

would I declare itemId in the getItemCount?

#

Or is that just to get the item, not to add anything to the user's balance?

golden minnow
#

It is just the function of scanning all types listed in the jaw and only returns the quantity

tidal citrus
#

ah, so I would need a different function to sell the dirt, I would just multiply the itemCount, by the amount

golden minnow
#

mean function

golden minnow
tidal citrus
#

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

tidal citrus
#

Sweet, thank you!

golden minnow
# light bane

You can take a look at this as a reference — I’m the one who wrote it.

tidal citrus
#

oh interesting, I think I know what I want to do with my system though, thank you!

tidal citrus
golden minnow
tidal citrus
#

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

golden minnow
#

separate it instead of together.

tidal citrus
#

how so, just have the getitem in its own file?

golden minnow
#
export function getItemCount
tidal citrus
#

then in sell dirt, just say
itemId = "minecraft:dirt
getItemCount(player)?

golden minnow
#
const coal = getItemCount(player, "minecraft:coal")
if (coal >= 64) {
//code
}
tidal citrus
#

I'm starting to get lost lol

golden minnow
#

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
  }
};
light bane
#

I added some stuff but I can’t quite remember what

golden minnow
light bane