Solved Take the quantity of the item in the inventory?

Discussion in 'Spigot Plugin Development' started by Giorgino, Feb 17, 2020.

  1. Hi everyone, does anyone know how I can take the quantity of the item in the inventory? For example, check how many iron_ingot are in the inventory.
  2. ItemStack#getAmount()

    edit: Oh you mean in an inventory
    Code (Java):
    final Map<Integer,ItemStack> map = inventory.all(Material.WHATEVER);
    int count = 0; // alternatively use some fancy streams
    for(ItemStack stack: map.values()) {
        count += stack.getAmount();
    #2 Schottky, Feb 17, 2020
    Last edited: Feb 17, 2020
    • Winner Winner x 1
  3. Well if you want to get all of the iron ingots in their whole inventory, just loop through the inventory for (ItemStack i : Player.getInventory().getContents()) and whenever the ItemStack is Material.IRON_INGOT get the amount and add it to an int
    • Winner Winner x 1
  4. Thanks so much. Since I want to avoid creating another post, can I ask you by chance how it is possible to create an animated effect on a title of an item? For example, the title of the item changes color every second and creates this animated effect.
  5. Set a new title whenever you want to update
    • Winner Winner x 1
  6. You can do that with ItemMeta ( and Stack.setItemMeta(ItemMeta);

    To do it every second you can use the BukkitRunnable api (
    BukkitRunnable exapmle:
    Code (Text):

    new BukkitRunnable() {
      public void run() {
        // code to do
    }.runTaskTimer(instance of your main class, delay in ticks as a long, iteration of the task in ticks);
    This uses runTaskTimer, which runs your task repeatedly on an iteration of a certain amount of time, there's also stuff like runTaskDelay to run a task after a certain amount of time, runTaskAsychrounously which runs the task on a different thread (for stuff like interacting with the internet, which will freeze the main thread if not run async)

    I hope this answered your question, sorry if it was a bit overdone lol

    EDIT: Also, every second is 20 ticks
    • Winner Winner x 1
  7. A very rudimentary way of doing it would be:
    Code (Java):
    int count = 0;
    for(ItemStack item : player.getInventory.getContents()){


    Count += item.getAmount();


    Or at the very least, something along those lines
    • Winner Winner x 1