Removing Items

Discussion in 'Plugin Development' started by CraftCreeper6, Apr 19, 2014.

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

    CraftCreeper6

    Hello! So I need to remove items from a players inventory (ONLY 10) I have tried:
    p.getInventory().remove(Material.DIAMOND, 10);
    But I get this:
    The method remove(int) in the type Inventory is not applicable for the arguments (Material, int)

    Please help D: Any help appreciated!
     
  2. Offline

    TheMcScavenger

    Code:
    if(p.getInventory().contains(new ItemStack(Material.DIAMOND, 10))){
        p.getInventory().remove(new ItemStack(Material.DIAMOND, 10));
    }
     
  3. Offline

    CraftCreeper6

    TheMcScavenger
    Thanks! Ill test it now and get back to you. :)

    TheMcScavenger
    It does not seem to be taking the diamonds from me :/

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

    TheMcScavenger

    Do you have the 10 diamonds in your inventory?
     
  5. Offline

    CraftCreeper6

  6. Offline

    Konkz

    PHP:
    if (p.getItemInHand().getAmount() <= 1) {
      
    p.setItemInHand(null);
     } else {
      
    p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
     }
     
  7. Offline

    Th3Br1x

    But that would only work, if the player hold diamonds in hand - not the solution CraftCreeper6 want, i think.

    Can you show us your whole class please, CraftCreeper?
     
  8. Offline

    Konkz

    I know it does. I never said I will spoon feed him the whole code so I gave him a frame - he has to only
    change few things.
     
    Th3Br1x likes this.
  9. Offline

    TheMcScavenger

    How about:

    Code:
    if(p.getInventory().contains(Material.DIAMOND)){
        ItemStack[] inv = p.getInventory().getContents();
        for(ItemStack stack : inv){
            if(stack.getType().equals(Material.DIAMOND) && stack.getAmount() >= 10){
                if(stack.getAmount() > 10){
                    stack.setAmount(stack.getAmount() - 10);
                }
                else if(stack.getAmount() == 10){
                    p.getInventory().remove(stack);
                }
            }
        }
    }
     
  10. Offline

    CraftCreeper6

    TheMcScavenger
    This works perfectly, it takes the diamonds but every time I run the command now I get an Internal Error:
    Stacktrace:
    Caused by: java.lang.NullPointerException
    at me.CraftCreeper6.All.Main.onCommand(Main.java:124) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    ... 13 more

    So of course it's a NullPointerExeption but how am I getting this? Line 124:
    if(stack.getType().equals(Material.DIAMOND) && stack.getAmount() >= 10){

    Any help appreciated :D
     
  11. Offline

    TheMcScavenger

    Try adding a null check:

    Code:
    if(p.getInventory().contains(Material.DIAMOND)){
        ItemStack[] inv = p.getInventory().getContents();
        for(ItemStack stack : inv){
            if(stack.getType() != null && stack.getType().equals(Material.DIAMOND) && stack.getAmount() >= 10){
                if(stack.getAmount() > 10){
                    stack.setAmount(stack.getAmount() - 10);
                }
                else if(stack.getAmount() == 10){
                    p.getInventory().remove(stack);
                }
            }
        }
    }
     
  12. Offline

    CraftCreeper6

    TheMcScavenger
    I am not sure why it is not working D:
    But.. Could you possibly lead my through importing vault and taking money from accounts, that's if you know how of course. If not then maybe there is a thread that I cannot find? (I searched and searched...)
    :D
     
  13. Offline

    MOMOTHEREAL

    Don't forget to p.updateInventory() :)
     
  14. Offline

    CraftCreeper6

    MOMOTHEREAL
    Don't think your getting it LOL
    I am getting Internal Error for null even after updating it :p
     
  15. Offline

    TheMcScavenger

    Never needed it, don't need it now.

    Hmmm, odd. I'll have another look at it later if nobody else can figure it out.
     
Thread Status:
Not open for further replies.

Share This Page