/** {@inheritDoc} */
  @Override
  protected void runImpl() {
    Player activePlayer = getConnection().getActivePlayer();

    Iterator<Player> it = World.getInstance().getPlayersIterator();

    List<Player> matches = new ArrayList<Player>(MAX_RESULTS);

    if (activePlayer != null && activePlayer.getLevel() < 10) {
      sendPacket(SM_SYSTEM_MESSAGE.LEVEL_NOT_ENOUGH_FOR_SEARCH("10"));
      return;
    }
    while (it.hasNext() && matches.size() < MAX_RESULTS) {
      Player player = it.next();
      if (!player.isSpawned()) continue;
      else if (player.getFriendList().getStatus() == Status.OFFLINE) continue;
      else if (lfgOnly == 1 && !player.isLookingForGroup()) continue;
      else if (!name.isEmpty() && !player.getName().toLowerCase().contains(name.toLowerCase()))
        continue;
      else if (minLevel != 0xFF && player.getLevel() < minLevel) continue;
      else if (maxLevel != 0xFF && player.getLevel() > maxLevel) continue;
      else if (classMask > 0 && (player.getPlayerClass().getMask() & classMask) == 0) continue;
      else if (region > 0 && player.getActiveRegion().getMapId() != region) continue;
      else if ((player.getCommonData().getRace() != activePlayer.getCommonData().getRace())
          && (CustomConfig.FACTIONS_SEARCH_MODE == false)) continue;
      else
      // This player matches criteria
      {
        matches.add(player);
      }
    }

    sendPacket(new SM_PLAYER_SEARCH(matches, region));
  }
 public void startIronWallRegistration() {
   registerAvailable = true;
   startUregisterIronWallTask();
   Iterator<Player> iter = World.getInstance().getPlayersIterator();
   while (iter.hasNext()) {
     Player player = iter.next();
     if (player.getLevel() > minlevel && player.getLevel() < maxlevel) {
       PacketSendUtility.sendPacket(
           player, new SM_AUTO_GROUP(maskId, SM_AUTO_GROUP.wnd_EntryIcon));
       // You may participate in the Iron Wall Warfront.
       PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_MSG_INSTANCE_OPEN_BASTION_WAR);
     }
   }
 }
 public boolean canPlayerJoin(Player player) {
   if (registerAvailable
       && player.getLevel() > minlevel
       && player.getLevel() < maxlevel
       && !hasCoolDown(player)
       && !isInInstance(player)) {
     return true;
   }
   return false;
 }
  /**
   * @param defeated
   * @param winner
   * @return Points Lost in PvP Death
   */
  public static int calculatePvPApLost(Player defeated, Player winner) {
    int pointsLost =
        Math.round(
            defeated.getAbyssRank().getRank().getPointsLost()
                * defeated.getRates().getApPlayerLossRate());

    // Level penalty calculation
    int difference = winner.getLevel() - defeated.getLevel();

    if (difference > 4) {
      pointsLost = Math.round(pointsLost * 0.1f);
    } else {
      switch (difference) {
        case 3:
          pointsLost = Math.round(pointsLost * 0.85f);
          break;
        case 4:
          pointsLost = Math.round(pointsLost * 0.65f);
          break;
      }
    }
    return pointsLost;
  }
  public static int calculatePvpDpGained(Player defeated, int maxRank, int maxLevel) {
    int pointsGained = 0;

    // base values
    int baseDp = 1064;
    int dpPerRank = 57;

    // adjust by rank
    pointsGained = (defeated.getAbyssRank().getRank().getId() - maxRank) * dpPerRank + baseDp;

    // adjust by level
    pointsGained = StatFunctions.adjustPvpDpGained(pointsGained, defeated.getLevel(), maxLevel);

    return pointsGained;
  }
  /**
   * @param defeated
   * @param winner
   * @return XP Points Gained in PvP Kill TODO: Find the correct formula.
   */
  public static int calculatePvpXpGained(Player defeated, int maxRank, int maxLevel) {
    int pointsGained = 5000;

    // Level penalty calculation
    int difference = maxLevel - defeated.getLevel();

    if (difference > 4) {
      pointsGained = Math.round(pointsGained * 0.1f);
    } else if (difference < -3) {
      pointsGained = Math.round(pointsGained * 1.3f);
    } else {
      switch (difference) {
        case 3:
          pointsGained = Math.round(pointsGained * 0.85f);
          break;
        case 4:
          pointsGained = Math.round(pointsGained * 0.65f);
          break;
        case -2:
          pointsGained = Math.round(pointsGained * 1.1f);
          break;
        case -3:
          pointsGained = Math.round(pointsGained * 1.2f);
          break;
      }
    }

    // Abyss rank penalty calculation
    int winnerAbyssRank = maxRank;
    int defeatedAbyssRank = defeated.getAbyssRank().getRank().getId();
    int abyssRankDifference = winnerAbyssRank - defeatedAbyssRank;

    if (winnerAbyssRank <= 7 && abyssRankDifference > 0) {
      float penaltyPercent = abyssRankDifference * 0.05f;

      pointsGained -= Math.round(pointsGained * penaltyPercent);
    }

    return pointsGained;
  }