Removing certain amount of items from inventory, including renamed items, damaged items etc.

Discussion in 'Plugin Development' started by MrMag518, Jul 8, 2013.

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

    MrMag518

    I am having a bit of trouble here, I am making a method which removes certain amount of items of one type, for an example emerald, from the inventory, but when I call the "inv.removeItem(stack, amount)" method, it won't remove renamed items, damaged items etc.

    Then I tried another method which was this:
    Code:java
    1.  
    2. int toRemove = amount;
    3.  
    4. for(int i = 0; i < toRemove; i++) {
    5. inv.remove(inv.first(getCurrentItem())); // material, not itemstack
    6. }
    7.  

    But that didn't do anything.

    Then I tried this, which was a total failure. (Probably just me derping)
    Code:java
    1.  
    2. int toRemove = amount;
    3.  
    4. for(int i = 0; i < toRemove; i++) {
    5. for(ItemStack is : inv.getContents()) {
    6. if(is != null) {
    7. if(is.getType() == getCurrentItem()) {
    8. inv.remove(is); // I need to call the inv.remove method, but I don't know how I would I would just remove 1 item of the itemstack instead of the whole itemstack.
    9. break;
    10. }
    11. }
    12. }
    13. }
    14.  


    I am always updating the inventory of the player after calling these methods.
     
  2. Offline

    MrMag518

  3. Offline

    Craftiii4

    This is from one of my plugins;

    Code:java
    1.  
    2. ItemMeta meta = null;
    3.  
    4. if (player.getItemInHand().hasItemMeta())
    5. meta = player.getItemInHand().getItemMeta();
    6.  
    7. ItemStack remove = new ItemStack(itemid, amountadding);
    8.  
    9.  
    10. if (subitemid != 0) {
    11. remove.setDurability((short) subitemid);
    12. }
    13.  
    14. if (player.getItemInHand().hasItemMeta()) {
    15. remove.setItemMeta(meta);
    16. }
    17.  
    18. player.getInventory().removeItem(remove);
    19. player.updateInventory();
    20.  


    Could do with some better editing, but it's laid-out like that because there are things going on between those lines.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
    MrMag518 likes this.
  4. Offline

    MrMag518

    Why didn't I think of that.. I'll try it, thanks!
     
Thread Status:
Not open for further replies.

Share This Page