Example #1
0
  /** Handle dialog */
  @Override
  public void onDialogSelect(int dialogId, final Player player, int questId) {

    Npc npc = getOwner();
    int targetObjectId = npc.getObjectId();

    if (QuestEngine.getInstance().onDialog(new QuestCookie(npc, player, questId, dialogId))) return;

    switch (dialogId) {
      case 2:
        PacketSendUtility.sendPacket(
            player,
            new SM_TRADELIST(
                npc,
                TradeService.getTradeListData().getTradeListTemplate(npc.getNpcId()),
                player.getPrices().getVendorBuyModifier(),
                player));
        break;
      case 3:
        PacketSendUtility.sendPacket(
            player,
            new SM_SELL_ITEM(
                targetObjectId,
                player.getPrices().getVendorSellModifier(player.getCommonData().getRace())));
        break;
      case 4:
        // stigma
        PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 1));
        break;
      case 5:
        // create legion
        if (MathUtil.isInRange(
            npc, player, 10)) // avoiding exploit with sending fake dialog_select packet
        {
          PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 2));
        } else {
          PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.LEGION_CREATE_TOO_FAR_FROM_NPC());
        }
        break;
      case 6:
        // disband legion
        if (MathUtil.isInRange(
            npc, player, 10)) // avoiding exploit with sending fake dialog_select packet
        {
          LegionService.getInstance().requestDisbandLegion(npc, player);
        } else {
          PacketSendUtility.sendPacket(
              player, SM_SYSTEM_MESSAGE.LEGION_DISPERSE_TOO_FAR_FROM_NPC());
        }
        break;
      case 7:
        // recreate legion
        if (MathUtil.isInRange(
            npc, player, 10)) // voiding exploit with sending fake client dialog_select
        // packet
        {
          LegionService.getInstance().recreateLegion(npc, player);
        } else {
          PacketSendUtility.sendPacket(
              player, SM_SYSTEM_MESSAGE.LEGION_DISPERSE_TOO_FAR_FROM_NPC());
        }
        break;
      case 20:
        // warehouse
        if (MathUtil.isInRange(
            npc, player, 10)) // voiding exploit with sending fake client dialog_select
        // packet
        {
          if (!RestrictionsManager.canUseWarehouse(player)) return;

          PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 26));
          WarehouseService.sendWarehouseInfo(player, true);
        }
        break;
      case 25:
        // TODO hotfix to prevent opening the legion wh when a quest returns false.
        break;
      case 27:
        // Consign trade?? npc karinerk, koorunerk
        PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 13));
        break;
      case 29:
        // soul healing
        final long expLost = player.getCommonData().getExpRecoverable();
        if (expLost == 0) player.getEffectController().removeEffect(8291);
        final double factor = (expLost < 1000000 ? 0.25 - (0.00000015 * expLost) : 0.1);
        final int price = (int) (expLost * factor * CustomConfig.SOULHEALING_PRICE_MULTIPLIER);

        RequestResponseHandler responseHandler =
            new RequestResponseHandler(npc) {
              @Override
              public void acceptRequest(Creature requester, Player responder) {
                if (player.getInventory().getKinahItem().getItemCount() >= price) {
                  PacketSendUtility.sendPacket(
                      player, SM_SYSTEM_MESSAGE.EXP(String.valueOf(expLost)));
                  PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.SOUL_HEALED());
                  player.getCommonData().resetRecoverableExp();
                  player.getInventory().decreaseKinah(price);
                  player.getEffectController().removeEffect(8291);
                } else {
                  PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.NOT_ENOUGH_KINAH(price));
                }
              }

              @Override
              public void denyRequest(Creature requester, Player responder) {
                // no message
              }
            };
        if (player.getCommonData().getExpRecoverable() > 0) {
          boolean result =
              player
                  .getResponseRequester()
                  .putRequest(SM_QUESTION_WINDOW.STR_SOUL_HEALING, responseHandler);
          if (result) {
            PacketSendUtility.sendPacket(
                player,
                new SM_QUESTION_WINDOW(
                    SM_QUESTION_WINDOW.STR_SOUL_HEALING, 0, String.valueOf(price)));
          }
        } else {
          PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.DONT_HAVE_RECOVERED_EXP());
        }
        break;
      case 30:
        switch (npc.getNpcId()) {
            // Triniel Arena (Enter)
          case 204089:
            TeleportService.teleportTo(player, 120010000, 1, 984f, 1543f, 222.1f, 0);
            ArenaService.getInstance().registerPlayerForTriniel(player);
            break;
            // Coliseum (Enter)
          case 203764:
            TeleportService.teleportTo(player, 110010000, 1, 1462.5f, 1326.1f, 564.1f, 0);
            ArenaService.getInstance().registerPlayerForColiseum(player);
            break;
            // Eracus Temple (Enter)
          case 203981:
            TeleportService.teleportTo(player, 210020000, 1, 439.3f, 422.2f, 274.3f, 0);
            break;
        }
        break;
      case 31:
        switch (npc.getNpcId()) {
            // Triniel Arena (Leave)
          case 204087:
            TeleportService.teleportTo(player, 120010000, 1, 1005.1f, 1528.9f, 222.1f, 0);
            ArenaService.getInstance().unregister(player);
            break;
            // Coliseum (Leave)
          case 203875:
            TeleportService.teleportTo(player, 110010000, 1, 1470.3f, 1343.5f, 563.7f, 21);
            ArenaService.getInstance().unregister(player);
            break;
            // Eracus Temple (Leave)
          case 203982:
            TeleportService.teleportTo(player, 210020000, 1, 446.2f, 431.1f, 274.5f, 0);
            break;
        }
        break;
      case 35:
        // Godstone socketing
        PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 21));
        break;
      case 36:
        // remove mana stone
        PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 20));
        break;
      case 37:
        // modify appearance
        PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 19));
        break;
      case 38:
        // flight and teleport
        TeleportService.showMap(player, targetObjectId, npc.getNpcId());
        break;
      case 39:
        // improve extraction
      case 40:
        // learn tailoring armor smithing etc...
        CraftSkillUpdateService.getInstance().learnSkill(player, npc);
        break;
      case 41:
        // expand cube
        CubeExpandService.expandCube(player, npc);
        break;
      case 42:
        WarehouseService.expandWarehouse(player, npc);
        break;
      case 47:
        // legion warehouse
        if (LegionConfig.LEGION_WAREHOUSE)
          if (MathUtil.isInRange(npc, player, 10))
            LegionService.getInstance().openLegionWarehouse(player);
        break;
      case 50:
        // WTF??? Quest dialog packet
        break;
      case 52:
        if (MathUtil.isInRange(
            npc, player, 10)) // avoiding exploit with sending fake dialog_select packet
        {
          PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 28));
        }
        break;
      case 53:
        // coin reward
        PacketSendUtility.sendPacket(
            player,
            new SM_MESSAGE(0, null, "This feature is not available yet", ChatType.ANNOUNCEMENTS));
        break;
      case 55:
      case 56:
        byte changesex = 0; // 0 plastic surgery, 1 gender switch
        byte check_ticket = 2; // 2 no ticket, 1 have ticket
        if (dialogId == 56) {
          // Gender Switch
          changesex = 1;
          if (player.getInventory().getItemCountByItemId(169660000) > 0
              || player.getInventory().getItemCountByItemId(169660001) > 0) check_ticket = 1;
        } else {
          // Plastic Surgery
          if (player.getInventory().getItemCountByItemId(169650000) > 0
              || player.getInventory().getItemCountByItemId(169650001) > 0) check_ticket = 1;
        }
        PacketSendUtility.sendPacket(
            player, new SM_PLASTIC_SURGERY(player, check_ticket, changesex));
        player.setEditMode(true);
        break;
      case 60:
        // armsfusion
        PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 29));
        break;
      case 61:
        // armsbreaking
        PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 30));
        break;
      case 64:
        // repurchase
        PacketSendUtility.sendPacket(player, new SM_REPURCHASE(npc, player));
        break;
      case 65:
        // adopt pet
        PacketSendUtility.sendPacket(player, new SM_PET(6));
        break;
      case 66:
        // surrender pet
        PacketSendUtility.sendPacket(player, new SM_PET(7));
        break;
      case 10000:
        // generic npc reply (most are teleporters)
        TeleporterTemplate template =
            DataManager.TELEPORTER_DATA.getTeleporterTemplate(npc.getNpcId());
        if (template != null) {
          TeleportLocation loc = template.getTeleLocIdData().getTelelocations().get(0);
          if (loc != null) {
            player.getInventory().decreaseKinah(loc.getPrice());
            TelelocationTemplate tlt =
                DataManager.TELELOCATION_DATA.getTelelocationTemplate(loc.getLocId());
            TeleportService.teleportTo(
                player, tlt.getMapId(), tlt.getX(), tlt.getY(), tlt.getZ(), 1000);
          }
        }
        break;
      default:
        if (questId > 0)
          PacketSendUtility.sendPacket(
              player, new SM_DIALOG_WINDOW(targetObjectId, dialogId, questId));
        else PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, dialogId));
        break;
    }
  }
Example #2
0
  public static boolean questFinish(QuestCookie env, int reward) {
    Player player = env.getPlayer();
    synchronized (player) {
      int id = env.getQuestId();

      QuestState qs = player.getQuestStateList().getQuestState(id);
      if (qs == null || qs.getStatus() != QuestStatus.REWARD) return false;

      QuestTemplate template = questsData.getQuestById(id);
      Storage inventory = player.getInventory();

      BonusTemplate bonusTemplate = bonusData.getBonusInfoByQuestId(id);
      List<SimpleCheckItemBonus> appliedBonuses = null;

      if (bonusTemplate != null) {
        int failedChecks = 0;
        QuestTemplate questTemplate = questsData.getQuestById(env.getQuestId());
        // if collectItems not null, the bonus for SimpleCheckItemBonus classes
        // was already applied, then skip them.
        boolean useCheckItems = questTemplate.getCollectItems() == null;

        List<AbstractInventoryBonus> bi = bonusTemplate.getItemBonuses();

        for (int i = 0; i < bi.size(); i++) {
          AbstractInventoryBonus bonus = bi.get(i);
          HandlerResult result = QuestEngine.getInstance().onBonusApply(env, i, bonus);
          if (result == HandlerResult.FAILED)
            continue; // bonus can not be applied (reason: not that bonus or failed)

          if (bonus instanceof SimpleCheckItemBonus) {
            if (!useCheckItems) continue;

            SimpleCheckItemBonus scb = (SimpleCheckItemBonus) bonus;
            if (!scb.canApply(player, env.getQuestId())) {
              // inventory full or other reasons
              failedChecks++;
              break;
            }

            if (appliedBonuses == null) appliedBonuses = new ArrayList<SimpleCheckItemBonus>();
            appliedBonuses.add(scb);
          } else {
            bonus.apply(player, null);
          }
        }
        if (failedChecks > 0) return false;
        else if (appliedBonuses != null) {
          for (SimpleCheckItemBonus scb : appliedBonuses) {
            player.getInventory().removeFromBagByItemId(scb.getCheckedItemId(), scb.getCount());
            scb.apply(player, null);
          }
        }
      }

      Rewards rewards = null;
      if (reward < template.getRewards().size()) rewards = template.getRewards().get(reward);
      else rewards = new Rewards();

      Rewards extRewards = null;
      if (template.getExtRewards() != null
          && qs.getCompleteCount() == template.getMaxRepeatCount() - 1)
        extRewards = template.getExtRewards().get(0);

      List<QuestItems> questItems = new ArrayList<QuestItems>();
      questItems.addAll(rewards.getRewardItem());
      if (extRewards != null) questItems.addAll(extRewards.getRewardItem());

      int dialogId = env.getDialogId();
      if (dialogId != 17 && dialogId != 0) {
        if (template.isUseClassReward() == 1) {
          QuestItems classRewardItem = null;
          PlayerClass playerClass = player.getCommonData().getPlayerClass();
          switch (playerClass) {
            case ASSASSIN:
              classRewardItem = template.getAssassinSelectableReward().get(dialogId - 8);
              break;
            case CHANTER:
              classRewardItem = template.getChanterSelectableReward().get(dialogId - 8);
              break;
            case CLERIC:
              classRewardItem = template.getPriestSelectableReward().get(dialogId - 8);
              break;
            case GLADIATOR:
              classRewardItem = template.getFighterSelectableReward().get(dialogId - 8);
              break;
            case RANGER:
              classRewardItem = template.getRangerSelectableReward().get(dialogId - 8);
              break;
            case SORCERER:
              classRewardItem = template.getWizardSelectableReward().get(dialogId - 8);
              break;
            case SPIRIT_MASTER:
              classRewardItem = template.getElementalistSelectableReward().get(dialogId - 8);
              break;
            case TEMPLAR:
              classRewardItem = template.getKnightSelectableReward().get(dialogId - 8);
              break;
          }
          if (classRewardItem != null) questItems.add(classRewardItem);
        } else {
          QuestItems selectebleRewardItem = null;
          if (rewards != null && !rewards.getSelectableRewardItem().isEmpty()) {
            selectebleRewardItem = rewards.getSelectableRewardItem().get(dialogId - 8);
            if (selectebleRewardItem != null) questItems.add(selectebleRewardItem);
          }
          if (extRewards != null && !extRewards.getSelectableRewardItem().isEmpty()) {
            selectebleRewardItem = extRewards.getSelectableRewardItem().get(dialogId - 8);
            if (selectebleRewardItem != null) questItems.add(selectebleRewardItem);
          }
        }
      }
      if (ItemService.addItems(player, questItems)) {
        if (rewards.getGold() != null) {
          inventory.increaseKinah((player.getRates().getQuestKinahRate() * rewards.getGold()));
        }
        if (rewards.getExp() != null && !player.isNoExperienceGain()) {
          int rewardExp = (player.getRates().getQuestXpRate() * rewards.getExp());
          if (rewardExp > Integer.MAX_VALUE || rewardExp < 0) rewardExp = Integer.MAX_VALUE;
          player.getCommonData().addExp(rewardExp);
        }

        if (rewards.getTitle() != null) {
          player.getTitleList().addTitle(rewards.getTitle());
        }

        if (rewards.getRewardAbyssPoint() != null) {
          player.getCommonData().addAp(rewards.getRewardAbyssPoint());
        }

        if (rewards.getExtendInventory() != null) {
          if (rewards.getExtendInventory() == 1) CubeExpandService.expand(player);
          else if (rewards.getExtendInventory() == 2) WarehouseService.expand(player);
        }

        if (rewards.getExtendStigma() != null) {
          PlayerCommonData pcd = player.getCommonData();
          pcd.setAdvencedStigmaSlotSize(pcd.getAdvencedStigmaSlotSize() + 1);
          PacketSendUtility.sendPacket(
              player, new SM_CUBE_UPDATE(player, 6, pcd.getAdvencedStigmaSlotSize()));
        }

        if (extRewards != null) {
          if (extRewards.getGold() != null) {
            inventory.increaseKinah((player.getRates().getQuestKinahRate() * extRewards.getGold()));
          }
          if (extRewards.getExp() != null) {
            int rewardExp = (player.getRates().getQuestXpRate() * extRewards.getExp());
            player.getCommonData().addExp(rewardExp);
          }

          if (extRewards.getTitle() != null) {
            player.getTitleList().addTitle(extRewards.getTitle());
          }
        }

        // remove all worker list item if finished.
        QuestWorkItems qwi = questsData.getQuestById(id).getQuestWorkItems();

        if (qwi != null) {
          long count = 0;
          for (QuestItems qi : qwi.getQuestWorkItem()) {
            if (qi != null) {
              count = player.getInventory().getItemCountByItemId(qi.getItemId());
              if (count > 0)
                if (!player.getInventory().removeFromBagByItemId(qi.getItemId(), count))
                  return false;
            }
          }
        }

        QuestEngine.getInstance().onQuestFinish(env);
        qs.setStatus(QuestStatus.COMPLETE);
        // save the rewardNo which is used to check quest start conditions
        qs.setQuestVarById(0, reward + 1);
        qs.setCompliteCount(qs.getCompleteCount() + 1);
        qs.setQuestVarById(0, reward + 1);
        PacketSendUtility.sendPacket(
            player, new SM_QUEST_ACCEPTED(2, id, qs.getStatus(), qs.getQuestVars().getQuestVars()));
        player.getController().updateNearbyQuests();
        QuestEngine.getInstance().onLvlUp(env);
        GuildService.getInstance().deleteDaily(player, id);
        return true;
      }
      return true;
    }
  }