Not removing itemstack from inventory.

Discussion in 'Plugin Development' started by elementalgodz11, May 11, 2014.

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

    elementalgodz11

    I am trying to remove an item from a players inventory, like so:
    Code:
    ItemStack stack = new ItemStack(plugin.getMaterialUtil().getPotion(args[1]));
    player.getInventory().removeItem(stack);
    The getPotion() method:
    Code:java
    1. public ItemStack getPotion(String potionType) {
    2.  
    3. boolean isSplash = false;
    4. boolean isExtended = false;
    5.  
    6. if (isPotion(potionType)) {
    7.  
    8. if (potionType.endsWith("s")) {
    9.  
    10. isSplash = true;
    11.  
    12. }
    13.  
    14. if (potionType.endsWith("e")) {
    15.  
    16. isExtended = true;
    17.  
    18. }
    19.  
    20. Potion potion = new Potion(PotionType.POISON);
    21. potion.getEffects().clear();
    22. potion.setSplash(isSplash);
    23. potion.setType(getPotionType(potionType));
    24. potion.setLevel(getPotionLevel(potionType));
    25. potion.setHasExtendedDuration(isExtended);
    26.  
    27. ItemStack potionItem = potion.toItemStack(getPotionLevel(potionType));
    28.  
    29. return potionItem;
    30.  
    31. }
    32.  
    33. return null;
    34.  
    35. }


    Now to debug this, I temporarily changed the removeItem to addItem, and it gives me this item:
    a.png

    Where as the ItemStack I am trying to remove is this:
    b.png

    The only difference between the two, is one is named 373:34 and one is named 373:8226

    I believe this may be a problem with setting the data, but I don't know.

    If anyone could help I'd greatly appreciate it.

    Turns out, setting the durability of the item stack works:
    Code:
    potionItem.setDurability((short) 8226);
    For a swiftness II potion, for example.

    However, checking each different short value for every potion type will be tedious, would there perhaps be an easier way?

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

    Xyplo

    Just search their inventory for the itemMeta of the item (potion). set that slot to air and update the inventory... There are many ways you could do this...
     
  3. Offline

    Krotass

    Try this
    Code:
    p.getInventory().getItemInHand().setAmount(p.getInventory().getItemInHand().getAmount()-1);

     
  4. Offline

    elementalgodz11

    Krotass, that wouldn't work because the amount is varied depending on an argument and the item can be located on any slot on the inventory.

    Xyplo
    I've tried this, but I just get a null pointer when comparing the two itemmetas.
    Code:java
    1. ItemStack potion = stack;
    2. potion.setAmount(Double.valueOf(args[1]);
    3.  
    4. ItemMeta potionMeta = potion.getItemMeta();
    5.  
    6. for (ItemStack all : player.getInventory().getContents()) {
    7.  
    8. if (all.hasItemMeta() && all.getItemMeta().equals(potionMeta)) {
    9.  
    10. potionMeta.equals(all.getItemMeta());
    11.  
    12. }
    13.  
    14. }
    15.  
    16. player.getInventory().addItem(potion);
     
  5. Offline

    Xyplo

    Have you even set an ItemMeta for the potion? It doesn't look like it.
     
  6. Offline

    elementalgodz11

    I'm using this to get the itemstack:
    ItemStack potion = stack;

    The 'stack' itemstack is equal to this:
    ItemStack stack = plugin.getMaterialUtil().getPotion(args[1]);

    That getPotion itemstack method returns the itemstack I showed in a snippet of my first post:
    Method (open)

    Code:java
    1. public ItemStack getPotion(String potionType) {
    2.  
    3. boolean isSplash = false;
    4. boolean isExtended = false;
    5.  
    6. if (isPotion(potionType)) {
    7.  
    8. if (potionType.endsWith("s")) {
    9.  
    10. isSplash = true;
    11.  
    12. }
    13.  
    14. if (potionType.endsWith("e")) {
    15.  
    16. isExtended = true;
    17.  
    18. }
    19.  
    20. Potion potion = new Potion(PotionType.POISON);
    21. potion.getEffects().clear();
    22. potion.setSplash(isSplash);
    23. potion.setType(getPotionType(potionType));
    24. potion.setLevel(getPotionLevel(potionType));
    25. potion.setHasExtendedDuration(isExtended);
    26.  
    27. ItemStack potionItem = potion.toItemStack(getPotionLevel(potionType));
    28.  
    29. return potionItem;
    30.  
    31. }
    32.  
    33. return null;
    34.  
    35. }




    I think I'm setting the itemmeta in that,.

    Xyplo
     
Thread Status:
Not open for further replies.

Share This Page