Solved Check material in player inventory

Discussion in 'Plugin Development' started by Pella, Aug 14, 2017.

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

    Pella

    Hello everyone,

    is it possible to check if in player inventory is material (wood, 20) - 20x wood?

    Thank you
     
  2. @Pella

    Yes it is. When an item is in the inventory it is called an ItemStack. You want to loop through the player's inventory and check if one of the slots contains an itemstack of that specific material and quantity.
    Code:
    Player player = (Player) event.getWhoClicked();
      
    Inventory inv = player.getInventory();
    
    for(int i = 0; i < inv.getSize(); i++) {
        ItemStack is = inv.getItem(i);
          
        if(is != null) {
            // if an itemstack is null it is empty
          
            if(is.getType() == Material.WOOD) {
                if(is.getAmount() >= 20) {
                    // player does have 20 wood
                 }
             }
        }
    }
     
    Last edited: Aug 14, 2017
  3. Offline

    AdamDev

    @RaxiCax
    You don't have to loop through the whole entire inventory. The most easy and simple way is like this:
    Code:
    Player p = (Player) e.getWhoClicked();
    
    ItemStack itemstack = new ItemStack(Material.WOOD,20);
    
    if(!(p.getInventory().contains(itemstack)) {
        // Send them something saying that they don't have enough
       return;
    } else {
          //Remove the item from the inventory
          //Send them a message that it was complete
        return;
    }
    

    @Pella
    Hope this helps.

    Edit:
    You could use @RaxiCax 's method of your searching for multiple items.
     
    Last edited: Aug 14, 2017
  4. @AdamDev

    I just looked inside the Bukkit DOC and you're right. I haven't really looked into the contains, but it only checks for a material and a quantity. Shorter version indeed.
     
  5. Offline

    AdamDev

    @RaxiCax
    I thank you for your understanding and I just made a edit to my last post if you didn't see it.
     
  6. Offline

    Pella

    @AdamDev
    Hi,
    are you sure with .contains? I am trying to on block click call event
    public void onPlayerInteract(PlayerInteractEvent event)

    and in this fuction delete if exist 20x wood
     
  7. Offline

    AdamDev

    @Pella
    Are you sure that your using the right .contains? There are many in .contains, you should use the one that says .contains - ItemStack

    Edit: And can you please send the code you have right now?
     
  8. Offline

    Pella

    @AdamDev
    Here you there

    Code:
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event)
    {
            Player player = event.getPlayer();
            Block b = event.getClickedBlock();
            Action action = event.getAction();
            Sign sign = (Sign) b.getState();
            if (action == Action.LEFT_CLICK_BLOCK && b != null && (b.getType() == Material.SIGN_POST || b.getType() == Material.SIGN || b.getType() == Material.WALL_SIGN))
            {
                  Material material = Material.getMaterial(17);
                       
                  ItemStack itemstack = new ItemStack(material, 20);
                       
                  if(!(player.getInventory().contains(itemstack)))
                  {
                          player.getInventory().remove(new ItemStack(material, 20));
                          player.sendMessage("Material has been removed");             
                  }
                  else
                  {
                         player.sendMessage("No material");
                  }
           }
    }
    
    but not working, can you help me ? Thank you

    Solved by my self:

    player.getInventory().removeItem(new ItemStack(material, 20));

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 14, 2017
Thread Status:
Not open for further replies.

Share This Page