get total amount of a material in an inventory

Discussion in 'Plugin Development' started by LRFLEW, Jul 21, 2011.

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

    LRFLEW

    I'm working a shop-thingy-thing, and I'm trying to create a "sell all of this" command. How might I get the amount of said item in the player's inventory (I can't just use remove(Material m) because I need to know how much is being removed)
     
  2. Offline

    bleachisback

    use a for loop that loops through every slot of the inventory and checks if it is that material
     
  3. Offline

    LRFLEW

    I can do a for loop, but I don't have all day to calculate this, and want to try to avoid checking every value if I can.

    I do have this currently:
    Code:
    ItemStack[] isa = (ItemStack[]) player.getInventory().all(m).values().toArray();
    int amount=0;
    for (ItemStack i : isa) amount += i.getAmount();
     
  4. Offline

    bleachisback

    the all() method just uses a for loop in the same way, anyway, so it technically is taking more CPU. Going through a few hundred loops in one frame is like breathing for a server, don't worry about it.
     
    tips48 likes this.
  5. Offline

    LRFLEW

    ok, I'm recoding it now with Inventory.getItem(int index), but what is the largest amount that can fit into getItem()?
     
  6. Offline

    Crash

    Code:
    for(int i = 0; i < inventory.getSize(); ++i){
    
        ItemStack stack = inventory.getItem(i);
        //...
    
    }
     
  7. @LRFLEW

    could you post your finished code snipped? i'm having a similar problem atm

    thanks in advance
     
  8. Offline

    LRFLEW

    I actually did something silly with this.

    I used Inventory.removeItem( new ItemStack( Material, 2147483647 ) ), and it returns a HashMap where get(0) would return an ItemStack who's amount is 2147483647 - the amount removed. I found this to be the fastest way, as removing it is already a for loop (sort of), and it's easier to do this.

    and I used 2147483647 because it's the largest value an integer can hold, and it is pretty much impossible to have more that this.
     
Thread Status:
Not open for further replies.

Share This Page