/**
  * Get the slot of the given item.
  *
  * @param inventory inventory to search
  * @param searchItem item to search for
  * @return slot of the item if the inventory contains it, -1 otherwise.
  */
 public static Integer getSlotOf(Inventory inventory, ItemStack searchItem) {
   Map<Integer, ? extends ItemStack> items = inventory.all(searchItem);
   for (Map.Entry<Integer, ? extends ItemStack> item : items.entrySet()) {
     if (item.getValue().isSimilar(searchItem)) {
       return item.getKey();
     }
   }
   return -1;
 }
 /**
  * Get the slot which the named material sits in.
  *
  * @param inventory inventory to search
  * @param material material to search for
  * @param itemName name of the material to match
  * @return slot of the named-item if it exists in the inventory, -1 otherwise.
  */
 public static Integer getSlotOf(Inventory inventory, Material material, String itemName) {
   HashMap<Integer, ? extends ItemStack> items = inventory.all(material);
   for (Map.Entry<Integer, ? extends ItemStack> inventoryItem : items.entrySet()) {
     if (Items.nameContains(inventoryItem.getValue(), itemName)) {
       return inventoryItem.getKey();
     }
   }
   return -1;
 }
  /**
   * Retrieve the first slot that the given material is found in.
   *
   * @param inv inventory to search
   * @param mat material to search for
   * @return the slot of the searched material if it's available, -1 if not.
   */
  public static int getFirst(Inventory inv, Material mat) {
    Map<Integer, ? extends ItemStack> matchingItems = inv.all(mat);

    for (Map.Entry<Integer, ? extends ItemStack> itemEnty : matchingItems.entrySet()) {
      if (itemEnty.getValue().getType() == mat) {
        return itemEnty.getKey();
      }
    }

    return -1;
  }
  /**
   * Retrieve the first slot that the given item sits in.
   *
   * @param inv inventory to search
   * @param item item to search for
   * @return the first slot the item resides in if available, -1 otherwise.
   */
  public static int getFirst(Inventory inv, ItemStack item) {
    Map<Integer, ? extends ItemStack> matchingItems = inv.all(item.getType());

    for (Map.Entry<Integer, ? extends ItemStack> itemEnty : matchingItems.entrySet()) {
      if (itemEnty.getValue().isSimilar(item)) {
        return itemEnty.getKey();
      }
    }

    return -1;
  }
  /**
   * Retrieve all the slots in which a specific item resides. If any.
   *
   * @param inventory Inventory to search for the item in.
   * @param searchItem item to search for inside the inventory
   * @return A HashMap of Integers, each of which being a slot that the searched item resides in.
   */
  public static Set<Integer> getSlotsOf(Inventory inventory, ItemStack searchItem) {
    Set<Integer> itemSlots = new HashSet<>();

    Map<Integer, ? extends ItemStack> items = inventory.all(searchItem);
    for (Map.Entry<Integer, ? extends ItemStack> item : items.entrySet()) {
      if (item.getValue().isSimilar(searchItem)) {
        itemSlots.add(item.getKey());
      }
    }

    return itemSlots;
  }
  /**
   * Retrieve all the slots and associated amount of a specific item inside the inventory.
   *
   * @param inv inventory of which to search.
   * @param searchItem item to search for in the inventory.
   * @return A HashMap with the Slot (Integer) as a key, and Value being the count of items in that
   *     slot.
   */
  public static Map<Integer, Integer> getSlotsCount(Inventory inv, ItemStack searchItem) {
    Map<Integer, Integer> slotsCount = new HashMap<>();

    Map<Integer, ? extends ItemStack> items = inv.all(searchItem);

    for (Map.Entry<Integer, ? extends ItemStack> item : items.entrySet()) {
      if (item.getValue().isSimilar(searchItem)) {
        slotsCount.put(item.getKey(), item.getValue().getAmount());
      }
    }

    return slotsCount;
  }
  /**
   * Retrieve how many of the given item resides within the inventory.
   *
   * @param inv inventory to check
   * @param item item to search for within the inventory
   * @return amount of the searched items in the given inventory.
   */
  public static int getCount(Inventory inv, ItemStack item) {
    Map<Integer, ? extends ItemStack> matchingItems = inv.all(item);

    int count = 0;

    for (Map.Entry<Integer, ? extends ItemStack> itemEntry : matchingItems.entrySet()) {
      ItemStack invItem = itemEntry.getValue();
      if (!item.isSimilar(invItem)) {
        continue;
      }

      count += invItem.getAmount();
    }

    return count;
  }
  /**
   * Retrieve how many of the given material are in the inventory.
   *
   * @param inv inventory to check
   * @param mat material to search for
   * @return amount of items with the given material in the inventory.
   */
  public static int getCount(Inventory inv, Material mat) {
    Map<Integer, ? extends ItemStack> matchingItems = inv.all(mat);

    int count = 0;

    for (Map.Entry<Integer, ? extends ItemStack> itemEntry : matchingItems.entrySet()) {
      ItemStack item = itemEntry.getValue();
      if (item.getType() != mat) {
        continue;
      }

      count += item.getAmount();
    }

    return count;
  }
 private int getCommandPearlSlot(Player player, String args[], int pos) {
   if (args.length <= pos) {
     ItemStack item = player.getItemInHand();
     if (item.getType() != Material.ENDER_PEARL) {
       player.sendMessage("You must hold a pearl or supply the player's name to use this command");
       return -1;
     }
     if (pearls.getByItemStack(item) == null) {
       player.sendMessage("This is an ordinary ender pearl");
       return -1;
     }
     return player.getInventory().getHeldItemSlot();
   } else {
     PrisonPearl pp = pearls.getByImprisoned(args[pos]);
     if (pp != null) {
       Inventory inv = player.getInventory();
       for (Entry<Integer, ? extends ItemStack> entry : inv.all(Material.ENDER_PEARL).entrySet()) {
         if (entry.getValue().getDurability() == pp.getID()) return entry.getKey();
       }
     }
     player.sendMessage("You don't possess " + args[0] + "'s prison pearl");
     return -1;
   }
 }