public boolean upgradePearl(Inventory inv, PrisonPearl pp) {
   final String prisoner = pp.getImprisonedName();
   ItemStack is = new ItemStack(Material.ENDER_PEARL, 1, pp.getID());
   int pearlslot = inv.first(is);
   if (pearlslot < 0) {
     // If the pearl has been converted, first won't return it here
     // as the metadata doesn't match.
     return false;
   }
   ItemStack existing_is = inv.getItem(pearlslot);
   if (existing_is != null) {
     ItemMeta existing_meta = existing_is.getItemMeta();
     if (existing_meta != null) {
       String existing_name = existing_meta.getDisplayName();
       if (existing_name != null && existing_name.compareTo(prisoner) == 0) {
         return true;
       }
     }
   }
   ItemMeta im = is.getItemMeta();
   // Rename pearl to that of imprisoned player
   im.setDisplayName(prisoner);
   List<String> lore = new ArrayList<String>();
   lore.add(prisoner + " is held within this pearl");
   // Given enchantment effect
   // Durability used because it doesn't affect pearl behaviour
   im.addEnchant(Enchantment.DURABILITY, 1, true);
   im.setLore(lore);
   is.setItemMeta(im);
   is.removeEnchantment(Enchantment.DURABILITY);
   inv.clear(pearlslot);
   inv.setItem(pearlslot, is);
   return true;
 }
 public boolean upgradePearl(Inventory inv, PrisonPearl pp) {
   final UUID prisonerId = pp.getImprisonedId();
   final String prisoner;
   if (plugin.isNameLayerLoaded()) prisoner = NameAPI.getCurrentName(pp.getImprisonedId());
   else prisoner = Bukkit.getOfflinePlayer(prisonerId).getName();
   ItemStack is = new ItemStack(Material.ENDER_PEARL, 1);
   for (ItemStack existing_is : inv.getContents()) {
     if (existing_is == null || existing_is.getType() != Material.ENDER_PEARL) continue;
     int pearlslot = inv.first(existing_is);
     if (existing_is != null) {
       existing_is.setDurability((short) 0);
       ItemMeta existing_meta = existing_is.getItemMeta();
       if (existing_meta != null) {
         String existing_name = existing_meta.getDisplayName();
         List<String> lore = existing_meta.getLore();
         if (existing_name != null
             && prisoner != null
             && existing_name.compareTo(prisoner) == 0
             && lore != null
             && lore.size() == 3) {
           // This check says all existing stuff is there so return true.
           return true;
         } else if (existing_name != null
             && prisoner != null
             && existing_name.compareTo(prisoner) != 0)
           // If we don't have the right pearl keep looking.
           continue;
         else if (existing_name == null)
           // This pearl can't even be right so just return.
           return true;
       }
     }
     ItemMeta im = is.getItemMeta();
     // Rename pearl to that of imprisoned player
     im.setDisplayName(prisoner);
     List<String> lore = new ArrayList<String>();
     lore.add(prisoner + " is held within this pearl");
     lore.add("UUID: " + pp.getImprisonedId().toString());
     lore.add("Unique: " + pp.getUniqueIdentifier());
     // Given enchantment effect
     // Durability used because it doesn't affect pearl behaviour
     im.addEnchant(Enchantment.DURABILITY, 1, true);
     im.setLore(lore);
     is.setItemMeta(im);
     inv.clear(pearlslot);
     inv.setItem(pearlslot, is);
     return true;
   }
   return false;
 }
  public static void replaceItem(Inventory inventory, ItemStack stack) {
    int first = inventory.first(stack.getType());

    if (first == -1) inventory.addItem(stack);
    else inventory.setItem(first, stack);
  }