remove item

Discussion in 'Plugin Development' started by DevManABCD, Jul 21, 2014.

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

    DevManABCD

    Code:java
    1. ItemStack cobble = new ItemStack(Material.COBBLESTONE, 400);
    2. p.getInventory().remove(cobble);
    3. p.updateInventory();
    4.  
    5.  


    Why this didnt remove any cobblestone?
     
  2. Offline

    Giraffeknee

    DevManABCD
    I'm fairly sure that you're trying to remove an item stack of 400 cobblestone which won't work unless they have a stack of 400 cobblestone.
     
  3. Offline

    stormneo7

    If you want to clear their inventory of cobblestone, you could use
    Code:java
    1. for(int i = 0; i < 36; i++){
    2. if(p.getInventory().getItem(i) != null && p.getInventory().getItem(i).getType() == Material.COBBLESTONE){
    3. p.getInventory().setItem(i, null);
    4. }
    5. }

    I'm not sure if there is a better way, I just woke up and this was the first idea that came to my head.
    What it does it it creates a loop that repeats 36 times. 36 is the size of the inventory of a player.
    It then checks the player's item in that slot to see if it's cobblestone.
    If it is, it'll set it to nothing.
    Hope this has helped.
     
  4. Offline

    Dragonphase

    stormneo7


    Code:java
    1. for(int i = 0; i < p.getInventory().getSize(); i++){
    2. if(p.getInventory().getItem(i) != null && p.getInventory().getItem(i).getType() == Material.COBBLESTONE){
    3. p.getInventory().setItem(i, null);
    4. }
    5. }


    Neater to use Inventory.getSize()
     
  5. Offline

    DevManABCD

    How to use it to remove 400xcobble?
     
  6. Offline

    Dragonphase

    DevManABCD

    Iterate through all items in the player's inventory. When you find cobblestone, remove it. Keep track of how much you have removed until you reach 400.
     
Thread Status:
Not open for further replies.

Share This Page