QuestLink.java 8.8 KB

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