Remove certain items from the PlayerInventory

Discussion in 'Plugin Development' started by mawserver, Jun 18, 2018.

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

    mawserver

    I've written a plugin that allows players to interact with certain objects to get certain items.
    Now I can't solve the last few lines of code - let me take what I need from the player's backpack
    Here is my code:
    Code:
    private Player usingplayer;
    
       public void takeitems(List<ItemStack> checklist){
    
            if(checklist==null)return;
    
            PlayerInventory inv = usingplayer.getInventory();
    
            for(int i =0;i<checklist.size();i++){
    
                ItemStack listslot = checklist.get(i);
    
                if(listslot==null)continue;
    
                int countbroad =checklist.get(i).getAmount();
    
             
    
                for(int i2 =0;i2<inv.getSize();i2++){
    
                ItemStack invslot = inv.getItem(i2);
    
                if(invslot == null)continue;
    
                if(invslot.isSimilar(listslot)){
    
                if(countbroad>=invslot.getAmount())
    
                {countbroad=countbroad-invslot.getAmount();inv.remove(i2);continue;}
    
                invslot.setAmount(invslot.getAmount()-countbroad);
    
                inv.setItem(i2, invslot);
    
                break;
    
                }
                }
    
    }
    usingplayer.updateInventory();
    }
    
    The variable checklist (In the"public void takeitems(List<ItemStack> checklist){.......")
    is a list of the itemstack that I want to take.

    Before this void,I've used another void to make sure there's enough item in the player's Inventory.(Pretty
    sure about this and had already tested in the game)

    The problem has been bothering me for hours.
    Thank you for your time.

     
  2. Offline

    MightyOne

    But what is the problem?
     
  3. Offline

    mawserver

    Oh..I made a terrible mistake


    This item is not actually taken away unless the amount of items in the player's backpack is exactly equal to the amount of items required.

    Even if the item is removed, the player needs to log in again to find that the item has disappeared
     
  4. Offline

    MightyOne

    AFAIK this is a bug that could not be solved directly til today. I guess you can use thw workaround Player#updateInventory()
     
  5. Offline

    mawserver

    Thank you for your answer.
    I believe this problem can be solved by code. I'm still exploring it
     
  6. Offline

    timtower Administrator Administrator Moderator

    He posted that code though. Try the method please.
     
    MightyOne likes this.
Thread Status:
Not open for further replies.

Share This Page