Solved Testing for amount of a specific item in inventory

Discussion in 'Plugin Development' started by sirbikesalot, Jul 30, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    sirbikesalot

    I am making a plugin that has a GUI shop but I can not figure out how to check and see if a player has iron in their inventory. I need it to be able to also see how much iron they have and run a >= statement so if they have more than say 5 iron it will still let them buy the item.
    Code:
    else if (name.equals(ChatColor.RED + "Blocks"))
            {
                e.setCancelled(true);
                int slot = e.getSlot();
                if (slot < 0)
                {
                    return;
                }
                if (slot == 11) {
                    Material item;
                    item iron = event.getItem();
                    player.getInventory().addItem(new ItemStack(Material.STAINED_CLAY, 16));
                }
     
  2. Offline

    KarimAKL

    @sirbikesalot I don't remember if there's a method that does this but, one way to do it is looping through the inventory, then check if the item is iron, and then add the amount of iron in that slot to an integer (that's outside the loop), then check the amount after the loop.
     
  3. Offline

    Machine Maker

    Well you probably will need to loop through the Inventory contents and add up all the iron you find (because it could be more than 1 stack). Then just compare those values.
     
  4. Offline

    sirbikesalot

    How world I go about doing this? I am still pretty new at coding. This is my first plugin.

    This is the full code.
    Code:
    public class InventoryClickListener implements Listener {
        @EventHandler
        public void invDragEvent(InventoryDragEvent e)
        {
            Inventory inv = e.getInventory();
            String name = inv.getName();
            if ( name.equals(ChatColor.RED + "Shop") || name.equals(ChatColor.RED + "Blocks"))
            {
                e.setCancelled(true);
                return;
            }
         
        }
     
        @EventHandler
        public void invClickEvent(InventoryClickEvent e)
        {
            Inventory inv = e.getInventory();
            Player player = (Player) e.getWhoClicked();
            String name = inv.getName();
            if ( name.equals(ChatColor.RED + "Shop"))
            {
                e.setCancelled(true);
                int slot = e.getSlot();
                if (slot < 0)
                {
                    return;
                }
                if (slot == 1)
                {
                    BWUI.blocks(player);
                    return;
                }
                if (slot == 2)
                {
                    BWUI.armor(player);
                    return;
                }
                if (slot == 3)
                {
                    BWUI.tools(player);
                    return;
                }
                if (slot == 5)
                {
                    BWUI.weapons(player);
                    return;
                }
                if (slot == 6)
                {
                    BWUI.potions(player);
                    return;
                }
                if (slot == 7)
                {
                    BWUI.special(player);
                    return;
                }
                else if (slot == 4)
                {
                    player.sendMessage("Under construction!");
                    player.closeInventory();
                    return;
                }
                else
                {
                    return;
                }
             
            }
            else if (name.equals(ChatColor.RED + "Blocks"))
            {
                e.setCancelled(true);
                int slot = e.getSlot();
                if (slot < 0)
                {
                    return;
                }
                if (slot == 11) {
                    ItemStack iron = new ItemStack(Material.IRON_INGOT);
                    if(player.getInventory().getContents().contains(Material.IRON_INGOT)) {
                      
                    }
                    player.getInventory().addItem(new ItemStack(Material.STAINED_CLAY, 16));
                }
                if (slot == 53) {
                    BWUI.shop(player);
                    return;
                }
                else
                {
                    ItemStack item = inv.getItem(slot);
                    if (item.hasItemMeta() && item.getItemMeta().hasLore())
                    {
                        String command = ChatColor.stripColor(item.getItemMeta().getLore().get(0));
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
                        return;
                    }
                }
            }
     
  5. Offline

    Machine Maker

    Well, the PlayerInventory has a method getStorageContents() that returns an ItemStack[] that you can use a for loop to iterate over. Then you can use various methods on the ItemStack to get the type and the amount.
     
  6. Offline

    sirbikesalot

    nvm I figured it out. I just used this code here:
    Code:
    if (slot == 20) {
                    if(player.getInventory().contains(Material.IRON_INGOT, 10)) {
                        player.getInventory().removeItem(new ItemStack(Material.IRON_INGOT, 10));
                        player.getInventory().addItem(new ItemStack(Material.STAINED_CLAY, 32));
                    }
                    else {
                        player.sendMessage("You do not have enough iron for this!");
                    }
                }
    Thank you for the help!!!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 30, 2019
  7. Offline

    Machine Maker

    @sirbikesalot Please mark this thread as Solved if your issue has been resolved.
     
  8. Offline

    KarimAKL

    @sirbikesalot But that doesn't do what you wanted?
     
  9. Offline

    sirbikesalot

    It works perfectly for me!
     
  10. Offline

    KarimAKL

    @sirbikesalot You were asking for a way to see the amount of iron in their inventory, what you are doing now is just checking if they have at least a specified amount; you wouldn't be able to print the amount without doing the loop.
     
  11. Offline

    sirbikesalot

    @KarimAKL you are completely right. I am still learning to code and I guess I really meant to ask how to see if they have at least an amount of an item.
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page