QuestLink.java 8.9 KB

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