QuestLink.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package handlers.bypasshandlers;
  16. import java.util.List;
  17. import javolution.util.FastList;
  18. import com.l2jserver.Config;
  19. import com.l2jserver.gameserver.cache.HtmCache;
  20. import com.l2jserver.gameserver.handler.IBypassHandler;
  21. import com.l2jserver.gameserver.instancemanager.QuestManager;
  22. import com.l2jserver.gameserver.model.actor.L2Character;
  23. import com.l2jserver.gameserver.model.actor.L2Npc;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import com.l2jserver.gameserver.model.quest.Quest;
  26. import com.l2jserver.gameserver.model.quest.QuestState;
  27. import com.l2jserver.gameserver.model.quest.State;
  28. import com.l2jserver.gameserver.network.SystemMessageId;
  29. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  30. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  31. import com.l2jserver.util.StringUtil;
  32. public class QuestLink implements IBypassHandler
  33. {
  34. private static final String[] COMMANDS =
  35. {
  36. "Quest"
  37. };
  38. public boolean useBypass(String command, L2PcInstance activeChar, L2Character target)
  39. {
  40. if (!(target instanceof L2Npc))
  41. return false;
  42. String quest = "";
  43. try
  44. {
  45. quest = command.substring(5).trim();
  46. }
  47. catch (IndexOutOfBoundsException ioobe)
  48. {
  49. }
  50. if (quest.length() == 0)
  51. showQuestWindow(activeChar, (L2Npc)target);
  52. else
  53. showQuestWindow(activeChar, (L2Npc)target, quest);
  54. return true;
  55. }
  56. /**
  57. * Open a choose quest window on client with all quests available of the L2NpcInstance.<BR><BR>
  58. *
  59. * <B><U> Actions</U> :</B><BR><BR>
  60. * <li>Send a Server->Client NpcHtmlMessage containing the text of the L2NpcInstance to the L2PcInstance </li><BR><BR>
  61. *
  62. * @param player The L2PcInstance that talk with the L2NpcInstance
  63. * @param quests The table containing quests of the L2NpcInstance
  64. *
  65. */
  66. public static void showQuestChooseWindow(L2PcInstance player, L2Npc npc, Quest[] quests)
  67. {
  68. final StringBuilder sb = StringUtil.startAppend(150,
  69. "<html><body>"
  70. );
  71. for (Quest q : quests)
  72. {
  73. StringUtil.append(sb,
  74. "<a action=\"bypass -h npc_",
  75. String.valueOf(npc.getObjectId()),
  76. "_Quest ",
  77. q.getName(),
  78. "\">[",
  79. q.getDescr()
  80. );
  81. QuestState qs = player.getQuestState(q.getScriptName());
  82. if (qs != null)
  83. {
  84. if (qs.getState() == State.STARTED && qs.getInt("cond") > 0)
  85. sb.append(" (In Progress)");
  86. else if (qs.getState() == State.COMPLETED)
  87. sb.append(" (Done)");
  88. }
  89. sb.append("]</a><br>");
  90. }
  91. sb.append("</body></html>");
  92. // Send a Server->Client packet NpcHtmlMessage to the L2PcInstance in order to display the message of the L2NpcInstance
  93. npc.insertObjectIdAndShowChatWindow(player, sb.toString());
  94. }
  95. /**
  96. * Open a quest window on client with the text of the L2NpcInstance.<BR><BR>
  97. *
  98. * <B><U> Actions</U> :</B><BR><BR>
  99. * <li>Get the text of the quest state in the folder data/scripts/quests/questId/stateId.htm </li>
  100. * <li>Send a Server->Client NpcHtmlMessage containing the text of the L2NpcInstance to the L2PcInstance </li>
  101. * <li>Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the client wait another packet </li><BR><BR>
  102. *
  103. * @param player The L2PcInstance that talk with the L2NpcInstance
  104. * @param questId The Identifier of the quest to display the message
  105. *
  106. */
  107. public static void showQuestWindow(L2PcInstance player, L2Npc npc, String questId)
  108. {
  109. String content = null;
  110. Quest q = QuestManager.getInstance().getQuest(questId);
  111. // Get the state of the selected quest
  112. QuestState qs = player.getQuestState(questId);
  113. if (q != null)
  114. {
  115. if ((q.getQuestIntId() >= 1 && q.getQuestIntId() < 20000) && (player.getWeightPenalty() >= 3 || !player.isInventoryUnder80(true)))
  116. {
  117. player.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.INVENTORY_LESS_THAN_80_PERCENT));
  118. return;
  119. }
  120. if (qs == null)
  121. {
  122. if (q.getQuestIntId() >= 1 && q.getQuestIntId() < 20000)
  123. {
  124. if (player.getAllActiveQuests().length > 40) // if too many ongoing quests, don't show window and send message
  125. {
  126. player.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.TOO_MANY_QUESTS));
  127. return;
  128. }
  129. }
  130. // check for start point
  131. Quest[] qlst = npc.getTemplate().getEventQuests(Quest.QuestEventType.QUEST_START);
  132. if (qlst != null && qlst.length > 0)
  133. {
  134. for (Quest temp : qlst)
  135. {
  136. if (temp == q)
  137. {
  138. qs = q.newQuestState(player);
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. else
  146. content = Quest.getNoQuestMsg(player); // no quests found
  147. if (qs != null)
  148. {
  149. // If the quest is alreday started, no need to show a window
  150. if (!qs.getQuest().notifyTalk(npc, qs))
  151. return;
  152. questId = qs.getQuest().getName();
  153. String stateId = State.getStateName(qs.getState());
  154. String path = "data/scripts/quests/" + questId + "/" + stateId + ".htm";
  155. content = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), path); //TODO path for quests html
  156. if (Config.DEBUG)
  157. {
  158. if (content != null)
  159. {
  160. _log.fine("Showing quest window for quest " + questId + " html path: " + path);
  161. }
  162. else
  163. {
  164. _log.fine("File not exists for quest " + questId + " html path: " + path);
  165. }
  166. }
  167. }
  168. // Send a Server->Client packet NpcHtmlMessage to the L2PcInstance in order to display the message of the L2NpcInstance
  169. if (content != null)
  170. npc.insertObjectIdAndShowChatWindow(player, content);
  171. // Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the client wait another packet
  172. player.sendPacket(ActionFailed.STATIC_PACKET);
  173. }
  174. /**
  175. * Collect awaiting quests/start points and display a QuestChooseWindow (if several available) or QuestWindow.<BR><BR>
  176. *
  177. * @param player The L2PcInstance that talk with the L2NpcInstance
  178. *
  179. */
  180. public static void showQuestWindow(L2PcInstance player, L2Npc npc)
  181. {
  182. // collect awaiting quests and start points
  183. List<Quest> options = new FastList<Quest>();
  184. QuestState[] awaits = player.getQuestsForTalk(npc.getTemplate().npcId);
  185. Quest[] starts = npc.getTemplate().getEventQuests(Quest.QuestEventType.QUEST_START);
  186. // Quests are limited between 1 and 999 because those are the quests that are supported by the client.
  187. // By limiting them there, we are allowed to create custom quests at higher IDs without interfering
  188. if (awaits != null)
  189. {
  190. for (QuestState x : awaits)
  191. {
  192. if (!options.contains(x.getQuest()))
  193. if ((x.getQuest().getQuestIntId() > 0) && (x.getQuest().getQuestIntId() < 20000))
  194. options.add(x.getQuest());
  195. }
  196. }
  197. if (starts != null)
  198. {
  199. for (Quest x : starts)
  200. {
  201. if (!options.contains(x))
  202. if ((x.getQuestIntId() > 0) && (x.getQuestIntId() < 20000))
  203. options.add(x);
  204. }
  205. }
  206. // Display a QuestChooseWindow (if several quests are available) or QuestWindow
  207. if (options.size() > 1)
  208. {
  209. showQuestChooseWindow(player, npc, options.toArray(new Quest[options.size()]));
  210. }
  211. else if (options.size() == 1)
  212. {
  213. showQuestWindow(player, npc, options.get(0).getName());
  214. }
  215. else
  216. {
  217. showQuestWindow(player, npc, "");
  218. }
  219. }
  220. public String[] getBypassList()
  221. {
  222. return COMMANDS;
  223. }
  224. }