Removing 1 item from itemstack

Discussion in 'Plugin Development' started by Woobie, Oct 20, 2012.

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

    Woobie

    Anyone have any idea how to do this? I just cant figure it out, nothing seems to work for me :confused:
     
  2. Offline

    hutattedonmyarm

    Get the Itemstack, remember data, delete itemstack, give new itemstack with 1 less.
     
  3. Offline

    Woobie

    And how would I do that?
    I already tried a few things, but nothing works.
     
  4. Offline

    hutattedonmyarm

    I found a simpler solution:
    Get the itemstack, set the amount to 1, remove the itemstack from the inventory
    Code:
    ItemStack is = p.getInventory().getItem("blablabla");
    is.setAmount(1);
    p.getInventory().removeItem(is);
     
  5. Offline

    Drew1080

    Ill try give you an example,

    First set the material and name for the item stack

    Code:
    ItemStack pickaxe = new ItemStack(Material.WOOD_PICKAXE, 5);
    Then get the size of the item stack and - 1 from it

    Code:
    pickaxe.setAmount(pickaxe.getAmount() - 1);
    b.t.w None of the above code has been tested, so it should work but it may not.
     
  6. Offline

    Woobie

    Red line here
    Code:
    new ItemStack(Material.SULPHUR, 5);
    Code:
    The constructor ItemStack(Material, int) is undefined
    Already tried that earlier.. :/

    Red line here
    Code:
    getItem
    Tried "SULPHUR"
    And Item.SULPHUR
    And Material.SULPHUR

    Red lines:
    "SULPHUR" = The method getItem(int) in the type Inventory is not applicable for the arguments (String)
    Item.SULPHUR = The method getItem(int) in the type Inventory is not applicable for the arguments (Item)
    Material.SULPHUR = The method getItem(int) in the type Inventory is not applicable for the arguments (Material)

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

    hutattedonmyarm

    Try this:

    Code:
    ItemStack is = p.getInventory().getItem(Material.valueOf("Sulphur"));
    getItem expects the ID of the item

    I hope this works, otherwise you need another way to get the ID of the Material. I can't do much now since the Javadocs are down :(
     
  8. Offline

    Woobie

    Still red line under getItem
    Code:
    The method getItem(int) in the type Inventory is not applicable for the arguments (Material)
    Well thanks for help anyways.
     
  9. Offline

    hutattedonmyarm

    Or try:

    Code:
    Material mat = Material.matchMaterial("Sulphur");
    ItemStack is = new ItemStack (mat, 1);
    player.getInventory().removeItem(is);
    But you have to check if the player has enough of it.
    You just need to find the ID somehow...
     
  10. Offline

    Drew1080

    Try putting an integer for getItem like it wants you to and not an Item and Material. So for sulphur its 289.
     
  11. Offline

    Woobie

    No red lines yet, testing it atm.

    Got an error which I cant paste here becouse I cant copy anything from the console, and the server logs are glitched.

    "ArrayIndexOutOfBoundsException"
    "getItem<CraftInventory.java:44>"

    The thing is, I dont have line 44..

    Here is the code I have atm, someone requested a stick that shoots fireballs :D
    Code:
        @EventHandler
        public void onInteract(PlayerInteractEvent e){
            if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
            if(e.getPlayer().getItemInHand().getType() == (Material.STICK)){
                if(e.getPlayer().getInventory().contains(Material.SULPHUR)){
                Fireball fireball = e.getPlayer().launchProjectile(Fireball.class);
                fireball.setShooter(e.getPlayer());
                fireball.setYield(7.5f);
                ItemStack is = e.getPlayer().getInventory().getItem(287);
                e.getPlayer().getInventory().removeItem(is);
         
            } else {
                e.getPlayer().sendMessage(ChatColor.RED+ "You need 1 gunpowder to shoot fireballs");
            }
            }
        }
    }
    }
    It does send the message if I dont have gunpowder in my inventory, but if I have gunpowder in my inventory, and I shoot, it gives the ArrayIndexOutOfBounds error, and gunpowder is still in my inventory.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  12. Offline

    hakelimopu

    Here's a function I use whenever I need to remove a particular number of a particular MaterialData from a player's inventory.

    Code:
        @SuppressWarnings("deprecation")
        public static void removePlayerItemCount(Player thePlayer, MaterialData theMaterialData,
                int theCount) {
            Iterator<ItemStack> iterator = thePlayer.getInventory().iterator();
            while(iterator.hasNext()){
                ItemStack theItemStack = iterator.next();
                if(theItemStack==null) continue;
                if(theItemStack.getData().getItemType()==theMaterialData.getItemType() && theItemStack.getData().getData() == theMaterialData.getData()){
                    if(theItemStack.getAmount()<=theCount){
                        theCount -= theItemStack.getAmount();
                        thePlayer.getInventory().remove(theItemStack);
                    }else{
                        theItemStack.setAmount(theItemStack.getAmount()-theCount);
                        theCount = 0;
                    }
                }
            }
            thePlayer.updateInventory();
        }
     
  13. Offline

    Woobie

    How would I implement this into my code? :eek:
    Looks a bit confusing.
     
  14. Offline

    hakelimopu

    Actually, here's the whole class:

    Code:
    public class PlayerUtilities {
        public static int getPlayerItemCount(Player thePlayer,MaterialData theMaterialData) {
            int theResult = 0;
            Iterator<ItemStack> iterator = thePlayer.getInventory().iterator();
            while(iterator.hasNext()){
                ItemStack theItemStack = iterator.next();
                if(theItemStack==null) continue;
                if(theItemStack.getData().getItemType()==theMaterialData.getItemType() && theItemStack.getData().getData() == theMaterialData.getData()){
                    theResult += theItemStack.getAmount();
                }
            }
            return theResult;
        }
     
        @SuppressWarnings("deprecation")
        public static void removePlayerItemCount(Player thePlayer, MaterialData theMaterialData,
                int theCount) {
            Iterator<ItemStack> iterator = thePlayer.getInventory().iterator();
            while(iterator.hasNext() && theCount>0){
                ItemStack theItemStack = iterator.next();
                if(theItemStack==null) continue;
                if(theItemStack.getData().getItemType()==theMaterialData.getItemType() && theItemStack.getData().getData() == theMaterialData.getData()){
                    if(theItemStack.getAmount()<=theCount){
                        theCount -= theItemStack.getAmount();
                        thePlayer.getInventory().remove(theItemStack);
                    }else{
                        theItemStack.setAmount(theItemStack.getAmount()-theCount);
                        theCount = 0;
                    }
                }
            }
            thePlayer.updateInventory();
        }
     
    }
    
    As for how you would use it:

    Code:
    //thePlayer is obviously a Player retrieved from an event somewhere
    MaterialData theMaterialData = new MaterialData(Material.SULPHUR);
    if(getPlayerItemCount(thePlayer,theMaterialData)>=1){
        getPlayerItemCount(thePlayer,theMaterialData,1);
        //do whatever is costing 1 sulphur here
    }
     
  15. Offline

    Woobie

    Aah.. i'm so lost. Why does such a simple thing has to be so complicated? :/

    EDIT: hmm.. just came up with this, not sure if it works though
    Code:
    e.getPlayer().getInventory().getItem(289).setAmount(-1);
    EDIT2: Nope =/
     
  16. Offline

    Loogeh

    I just use this method (made by me)
    Code:
        public static void subtractAmount(Player player) {
            if(player.getItemInHand().getAmount() == 1) {
                player.setItemInHand(null);
            } else {
                player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1);
            }
        }
     
  17. Offline

    hutattedonmyarm

    Maybe:
    Code:
    e.getPlayer().getInventory().getItem(289).setAmount(e.getPlayer().getInventory().getItem(289).getAmount() - 1);
     
  18. Offline

    Woobie

    I could use this
    Code:
    ((ItemStack) e.getPlayer().getInventory()).setAmount((((ItemStack) e.getPlayer().getInventory()).getAmount() - 1));
    But how would I specify what item? I need to check the whole inventory, not just item in hand.

    Still same error =/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  19. Offline

    herpingdo

    I used this code for the exact plugin you are writing for the same thread. hasAmmo is true if the player has a SULPHUR.

    Code:JAVA
    1.  
    2. if (hasAmmo)
    3. {
    4. p.getInventory().removeItem(new ItemStack(Material.SULPHUR, 1));
    5. p.updateInventory();
    6. fireball(p);
    7. hasAmmo = false;
    8. }
     
  20. come on guys, this one is actually easy:
    define item first though.
    Code:
               
    if(item.getAmount() == 1){
                    event.getPlayer().getInventory().remove(item);
                }else item.setAmount(item.getAmount() - 1);
    
     
  21. Offline

    Woobie

    Wow thanks!
    Works perfectly
     
Thread Status:
Not open for further replies.

Share This Page