QuestLink.java 8.7 KB

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