How to find x item in player inv and remove it if it exists

Discussion in 'Plugin Development' started by Crafted Evil, Jan 14, 2014.

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

    Crafted Evil

    I've been looking all over for a way on how I could get an item in a player inventory and if it exists then minus by lets say 5. Anyone know how I would do this?
     
  2. Offline

    Ch4t4r

    When you have an exact instance of that item do this: Loop trough all items in the inventory (for (ItemStack item:p.getInventory ()) if that item equals your instance decrease it as you like.
     
  3. p.getInventory.clear(new ItemStack(Material.WOOD, 1)); replace the WOOD with the material that you wan't.
    Btw. I didn't check the code, so idk if it will works
     
  4. Offline

    Deleted user

  5. Offline

    MrMag518

    This is how I would have done it:

    Code:java
    1.  
    2. for(ItemStack is : p.getInventory().getContents()) {
    3. if(is != null && is.getType() == Material.APPLE) {
    4. if(is.getAmount() <= 5) {
    5. p.getInventory().remove(is);
    6. } else {
    7. is.setAmount(is.getAmount() - 5);
    8. }
    9. }
    10. }
    11.  
     
  6. Offline

    Deleted user

    bramhaag
    Your code would only clear their inventory if they had one wood. Mine checks for a specific type of material
     
Thread Status:
Not open for further replies.

Share This Page