Getting Amount of items in a players inventory

Discussion in 'Spigot Plugin Development' started by WildPlaysGames, May 17, 2017.

  1. So, I'm making a sellall plugin. How is it possible to get an amount of a material that they have in their inventory?
    Code:
    Code (Text):
    if (e.getInventory().getName() == "§cBlocks Menu") {
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("§cStone")) {
                    if (e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
                        if (e.getWhoClicked().getInventory().contains(Material.STONE)) {
                            e.getWhoClicked().getInventory().all(Material.STONE).values();
    Is that correct?
     
  2. Code (Text):
    int items = 0;
    for(ItemStack item : p.getInventory().getContents()) {
    item++;
    }
    p.sendMessage("Total items: + items);
     
  3. Under the code I currently have?
     
  4. If you know java, this is obvious
     
  5. Oh yeah, right sorry.
     
  6. You mean
    Code (Text):
    items += item.getAmount();
    ...?
     
    • compare strings with equals
    • compare enums with equals operator (==)
    • use chatcolor enum when working with color codes in your project(s)
    Code (Java):

    String one = "example";
    String two = "example";

    if (one.equals(two)) {

    }

    if (Material.GRASS == Material.GRASS) { //enum value comparison example

    }
     
    you could also use Java 8 stream API for your problem.
    ex:

    Code (Java):

    Arrays.stream(inventory.getContents()).filter(itemStack -> itemStack.getType() == Material.GRASS).count(); //return long value
     
    #7 ChefJava, May 17, 2017
    Last edited: May 17, 2017
    • Like Like x 1