QuestLink.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2004-2014 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.events.EventType;
  30. import com.l2jserver.gameserver.model.events.listeners.AbstractEventListener;
  31. import com.l2jserver.gameserver.model.quest.Quest;
  32. import com.l2jserver.gameserver.model.quest.QuestState;
  33. import com.l2jserver.gameserver.model.quest.State;
  34. import com.l2jserver.gameserver.network.SystemMessageId;
  35. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  36. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  37. import com.l2jserver.util.StringUtil;
  38. public class QuestLink implements IBypassHandler
  39. {
  40. private static final String[] COMMANDS =
  41. {
  42. "Quest"
  43. };
  44. @Override
  45. public boolean useBypass(String command, L2PcInstance activeChar, L2Character target)
  46. {
  47. String quest = "";
  48. try
  49. {
  50. quest = command.substring(5).trim();
  51. }
  52. catch (IndexOutOfBoundsException ioobe)
  53. {
  54. }
  55. if (quest.length() == 0)
  56. {
  57. showQuestWindow(activeChar, (L2Npc) target);
  58. }
  59. else
  60. {
  61. int questNameEnd = quest.indexOf(" ");
  62. if (questNameEnd == -1)
  63. {
  64. showQuestWindow(activeChar, (L2Npc) target, quest);
  65. }
  66. else
  67. {
  68. activeChar.processQuestEvent(quest.substring(0, questNameEnd), quest.substring(questNameEnd).trim());
  69. }
  70. }
  71. return true;
  72. }
  73. /**
  74. * Open a choose quest window on client with all quests available of the L2NpcInstance.<br>
  75. * <b><u>Actions</u>:</b><br>
  76. * <li>Send a Server->Client NpcHtmlMessage containing the text of the L2NpcInstance to the L2PcInstance</li>
  77. * @param player The L2PcInstance that talk with the L2NpcInstance
  78. * @param npc The table containing quests of the L2NpcInstance
  79. * @param quests
  80. */
  81. public static void showQuestChooseWindow(L2PcInstance player, L2Npc npc, Quest[] quests)
  82. {
  83. final StringBuilder sb = StringUtil.startAppend(150, "<html><body>");
  84. String state = "";
  85. int questId = -1;
  86. for (Quest q : quests)
  87. {
  88. if (q == null)
  89. {
  90. continue;
  91. }
  92. StringUtil.append(sb, "<a action=\"bypass -h npc_", String.valueOf(npc.getObjectId()), "_Quest ", q.getName(), "\">[");
  93. final QuestState qs = player.getQuestState(q.getScriptName());
  94. if ((qs == null) || qs.isCreated())
  95. {
  96. state = q.isCustomQuest() ? "" : "01";
  97. }
  98. else if (qs.isStarted())
  99. {
  100. state = q.isCustomQuest() ? " (In Progress)" : "02";
  101. }
  102. else if (qs.isCompleted())
  103. {
  104. state = q.isCustomQuest() ? " (Done)" : "03";
  105. }
  106. if (q.isCustomQuest())
  107. {
  108. StringUtil.append(sb, q.getDescr(), state);
  109. }
  110. else
  111. {
  112. questId = q.getId();
  113. if (q.getId() > 10000)
  114. {
  115. questId -= 5000;
  116. }
  117. else if (questId == 146)
  118. {
  119. questId = 640;
  120. }
  121. StringUtil.append(sb, "<fstring>", String.valueOf(questId), state, "</fstring>");
  122. }
  123. sb.append("]</a><br>");
  124. }
  125. sb.append("</body></html>");
  126. // Send a Server->Client packet NpcHtmlMessage to the L2PcInstance in order to display the message of the L2NpcInstance
  127. npc.insertObjectIdAndShowChatWindow(player, sb.toString());
  128. }
  129. /**
  130. * Open a quest window on client with the text of the L2NpcInstance.<br>
  131. * <b><u>Actions</u>:</b><br>
  132. * <ul>
  133. * <li>Get the text of the quest state in the folder data/scripts/quests/questId/stateId.htm</li>
  134. * <li>Send a Server->Client NpcHtmlMessage containing the text of the L2NpcInstance to the L2PcInstance</li>
  135. * <li>Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the client wait another packet</li>
  136. * </ul>
  137. * @param player the L2PcInstance that talk with the {@code npc}
  138. * @param npc the L2NpcInstance that chats with the {@code player}
  139. * @param questId the Id of the quest to display the message
  140. */
  141. public static void showQuestWindow(L2PcInstance player, L2Npc npc, String questId)
  142. {
  143. String content = null;
  144. Quest q = QuestManager.getInstance().getQuest(questId);
  145. // Get the state of the selected quest
  146. QuestState qs = player.getQuestState(questId);
  147. if (q != null)
  148. {
  149. if (((q.getId() >= 1) && (q.getId() < 20000)) && ((player.getWeightPenalty() >= 3) || !player.isInventoryUnder90(true)))
  150. {
  151. player.sendPacket(SystemMessageId.INVENTORY_LESS_THAN_80_PERCENT);
  152. return;
  153. }
  154. if (qs == null)
  155. {
  156. if ((q.getId() >= 1) && (q.getId() < 20000))
  157. {
  158. // Too many ongoing quests.
  159. if (player.getAllActiveQuests().length > 40)
  160. {
  161. final NpcHtmlMessage html = new NpcHtmlMessage(npc.getObjectId());
  162. html.setFile(player.getHtmlPrefix(), "data/html/fullquest.html");
  163. player.sendPacket(html);
  164. return;
  165. }
  166. }
  167. // check for start point
  168. for (AbstractEventListener listener : npc.getListeners(EventType.ON_NPC_QUEST_START))
  169. {
  170. if (listener.getOwner() instanceof Quest)
  171. {
  172. final Quest quest = (Quest) listener.getOwner();
  173. if (quest == q)
  174. {
  175. qs = q.newQuestState(player);
  176. break;
  177. }
  178. }
  179. }
  180. }
  181. }
  182. else
  183. {
  184. content = Quest.getNoQuestMsg(player); // no quests found
  185. }
  186. if ((q != null) && (qs != null))
  187. {
  188. // If the quest is already started, no need to show a window
  189. if (!q.notifyTalk(npc, player))
  190. {
  191. return;
  192. }
  193. questId = q.getName();
  194. String stateId = State.getStateName(qs.getState());
  195. String path = "data/scripts/quests/" + questId + "/" + stateId + ".htm";
  196. content = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), path); // TODO path for quests html
  197. if (Config.DEBUG)
  198. {
  199. if (content != null)
  200. {
  201. _log.fine("Showing quest window for quest " + questId + " html path: " + path);
  202. }
  203. else
  204. {
  205. _log.fine("File not exists for quest " + questId + " html path: " + path);
  206. }
  207. }
  208. }
  209. // Send a Server->Client packet NpcHtmlMessage to the L2PcInstance in order to display the message of the L2NpcInstance
  210. if (content != null)
  211. {
  212. npc.insertObjectIdAndShowChatWindow(player, content);
  213. }
  214. // Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the client wait another packet
  215. player.sendPacket(ActionFailed.STATIC_PACKET);
  216. }
  217. /**
  218. * Collect awaiting quests/start points and display a QuestChooseWindow (if several available) or QuestWindow.
  219. * @param player the L2PcInstance that talk with the {@code npc}.
  220. * @param npc the L2NpcInstance that chats with the {@code player}.
  221. */
  222. public static void showQuestWindow(L2PcInstance player, L2Npc npc)
  223. {
  224. // collect awaiting quests and start points
  225. List<Quest> options = new FastList<>();
  226. QuestState[] awaits = player.getQuestsForTalk(npc.getTemplate().getId());
  227. // Quests are limited between 1 and 999 because those are the quests that are supported by the client.
  228. // By limiting them there, we are allowed to create custom quests at higher IDs without interfering
  229. if (awaits != null)
  230. {
  231. for (QuestState x : awaits)
  232. {
  233. if (!options.contains(x.getQuest()))
  234. {
  235. if ((x.getQuest().getId() > 0) && (x.getQuest().getId() < 20000))
  236. {
  237. options.add(x.getQuest());
  238. }
  239. }
  240. }
  241. }
  242. for (AbstractEventListener listener : npc.getListeners(EventType.ON_NPC_QUEST_START))
  243. {
  244. if (listener.getOwner() instanceof Quest)
  245. {
  246. final Quest quest = (Quest) listener.getOwner();
  247. if (!options.contains(quest) && (quest.getId() > 0) && (quest.getId() < 20000))
  248. {
  249. options.add(quest);
  250. }
  251. }
  252. }
  253. // Display a QuestChooseWindow (if several quests are available) or QuestWindow
  254. if (options.size() > 1)
  255. {
  256. showQuestChooseWindow(player, npc, options.toArray(new Quest[options.size()]));
  257. }
  258. else if (options.size() == 1)
  259. {
  260. showQuestWindow(player, npc, options.get(0).getName());
  261. }
  262. else
  263. {
  264. showQuestWindow(player, npc, "");
  265. }
  266. }
  267. @Override
  268. public String[] getBypassList()
  269. {
  270. return COMMANDS;
  271. }
  272. }