QuestLink.java 9.2 KB

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