QuestState.java 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server 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 Server 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 com.l2jserver.gameserver.model.quest;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.Calendar;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  29. import com.l2jserver.gameserver.cache.HtmCache;
  30. import com.l2jserver.gameserver.enums.QuestSound;
  31. import com.l2jserver.gameserver.enums.QuestType;
  32. import com.l2jserver.gameserver.instancemanager.QuestManager;
  33. import com.l2jserver.gameserver.model.actor.L2Character;
  34. import com.l2jserver.gameserver.model.actor.L2Npc;
  35. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  36. import com.l2jserver.gameserver.model.events.AbstractScript;
  37. import com.l2jserver.gameserver.model.holders.ItemHolder;
  38. import com.l2jserver.gameserver.model.itemcontainer.Inventory;
  39. import com.l2jserver.gameserver.network.serverpackets.ExShowQuestMark;
  40. import com.l2jserver.gameserver.network.serverpackets.PlaySound;
  41. import com.l2jserver.gameserver.network.serverpackets.QuestList;
  42. import com.l2jserver.gameserver.network.serverpackets.TutorialCloseHtml;
  43. import com.l2jserver.gameserver.network.serverpackets.TutorialEnableClientEvent;
  44. import com.l2jserver.gameserver.network.serverpackets.TutorialShowHtml;
  45. import com.l2jserver.gameserver.network.serverpackets.TutorialShowQuestionMark;
  46. import com.l2jserver.gameserver.util.Util;
  47. /**
  48. * Quest state class.
  49. * @author Luis Arias
  50. */
  51. public final class QuestState
  52. {
  53. protected static final Logger _log = Logger.getLogger(QuestState.class.getName());
  54. /** The name of the quest of this QuestState */
  55. private final String _questName;
  56. /** The "owner" of this QuestState object */
  57. private final L2PcInstance _player;
  58. /** The current state of the quest */
  59. private byte _state;
  60. /** A map of key->value pairs containing the quest state variables and their values */
  61. private Map<String, String> _vars;
  62. /**
  63. * boolean flag letting QuestStateManager know to exit quest when cleaning up
  64. */
  65. private boolean _isExitQuestOnCleanUp = false;
  66. /**
  67. * Constructor of the QuestState. Creates the QuestState object and sets the player's progress of the quest to this QuestState.
  68. * @param quest the {@link Quest} object associated with the QuestState
  69. * @param player the owner of this {@link QuestState} object
  70. * @param state the initial state of the quest
  71. */
  72. public QuestState(Quest quest, L2PcInstance player, byte state)
  73. {
  74. _questName = quest.getName();
  75. _player = player;
  76. _state = state;
  77. player.setQuestState(this);
  78. }
  79. /**
  80. * @return the name of the quest of this QuestState
  81. */
  82. public String getQuestName()
  83. {
  84. return _questName;
  85. }
  86. /**
  87. * @return the {@link Quest} object of this QuestState
  88. */
  89. public Quest getQuest()
  90. {
  91. return QuestManager.getInstance().getQuest(_questName);
  92. }
  93. /**
  94. * @return the {@link L2PcInstance} object of the owner of this QuestState
  95. */
  96. public L2PcInstance getPlayer()
  97. {
  98. return _player;
  99. }
  100. /**
  101. * @return the current State of this QuestState
  102. * @see com.l2jserver.gameserver.model.quest.State
  103. */
  104. public byte getState()
  105. {
  106. return _state;
  107. }
  108. /**
  109. * @return {@code true} if the State of this QuestState is CREATED, {@code false} otherwise
  110. * @see com.l2jserver.gameserver.model.quest.State
  111. */
  112. public boolean isCreated()
  113. {
  114. return (_state == State.CREATED);
  115. }
  116. /**
  117. * @return {@code true} if the State of this QuestState is STARTED, {@code false} otherwise
  118. * @see com.l2jserver.gameserver.model.quest.State
  119. */
  120. public boolean isStarted()
  121. {
  122. return (_state == State.STARTED);
  123. }
  124. /**
  125. * @return {@code true} if the State of this QuestState is COMPLETED, {@code false} otherwise
  126. * @see com.l2jserver.gameserver.model.quest.State
  127. */
  128. public boolean isCompleted()
  129. {
  130. return (_state == State.COMPLETED);
  131. }
  132. /**
  133. * @param state the new state of the quest to set
  134. * @return {@code true} if state was changed, {@code false} otherwise
  135. * @see #setState(byte state, boolean saveInDb)
  136. * @see com.l2jserver.gameserver.model.quest.State
  137. */
  138. public boolean setState(byte state)
  139. {
  140. return setState(state, true);
  141. }
  142. /**
  143. * Change the state of this quest to the specified value.
  144. * @param state the new state of the quest to set
  145. * @param saveInDb if {@code true}, will save the state change in the database
  146. * @return {@code true} if state was changed, {@code false} otherwise
  147. * @see com.l2jserver.gameserver.model.quest.State
  148. */
  149. public boolean setState(byte state, boolean saveInDb)
  150. {
  151. if (_state == state)
  152. {
  153. return false;
  154. }
  155. final boolean newQuest = isCreated();
  156. _state = state;
  157. if (saveInDb)
  158. {
  159. if (newQuest)
  160. {
  161. Quest.createQuestInDb(this);
  162. }
  163. else
  164. {
  165. Quest.updateQuestInDb(this);
  166. }
  167. }
  168. _player.sendPacket(new QuestList());
  169. return true;
  170. }
  171. /**
  172. * Add parameter used in quests.
  173. * @param var String pointing out the name of the variable for quest
  174. * @param val String pointing out the value of the variable for quest
  175. * @return String (equal to parameter "val")
  176. */
  177. public String setInternal(String var, String val)
  178. {
  179. if (_vars == null)
  180. {
  181. _vars = new HashMap<>();
  182. }
  183. if (val == null)
  184. {
  185. val = "";
  186. }
  187. _vars.put(var, val);
  188. return val;
  189. }
  190. public String set(String var, int val)
  191. {
  192. return set(var, Integer.toString(val));
  193. }
  194. /**
  195. * Return value of parameter "val" after adding the couple (var,val) in class variable "vars".<br>
  196. * Actions:<br>
  197. * <ul>
  198. * <li>Initialize class variable "vars" if is null.</li>
  199. * <li>Initialize parameter "val" if is null</li>
  200. * <li>Add/Update couple (var,val) in class variable Map "vars"</li>
  201. * <li>If the key represented by "var" exists in Map "vars", the couple (var,val) is updated in the database.<br>
  202. * The key is known as existing if the preceding value of the key (given as result of function put()) is not null.<br>
  203. * If the key doesn't exist, the couple is added/created in the database</li>
  204. * <ul>
  205. * @param var String indicating the name of the variable for quest
  206. * @param val String indicating the value of the variable for quest
  207. * @return String (equal to parameter "val")
  208. */
  209. public String set(String var, String val)
  210. {
  211. if (_vars == null)
  212. {
  213. _vars = new HashMap<>();
  214. }
  215. if (val == null)
  216. {
  217. val = "";
  218. }
  219. String old = _vars.put(var, val);
  220. if (old != null)
  221. {
  222. Quest.updateQuestVarInDb(this, var, val);
  223. }
  224. else
  225. {
  226. Quest.createQuestVarInDb(this, var, val);
  227. }
  228. if ("cond".equals(var))
  229. {
  230. try
  231. {
  232. int previousVal = 0;
  233. try
  234. {
  235. previousVal = Integer.parseInt(old);
  236. }
  237. catch (Exception ex)
  238. {
  239. previousVal = 0;
  240. }
  241. setCond(Integer.parseInt(val), previousVal);
  242. }
  243. catch (Exception e)
  244. {
  245. _log.log(Level.WARNING, _player.getName() + ", " + getQuestName() + " cond [" + val + "] is not an integer. Value stored, but no packet was sent: " + e.getMessage(), e);
  246. }
  247. }
  248. return val;
  249. }
  250. /**
  251. * Internally handles the progression of the quest so that it is ready for sending appropriate packets to the client.<br>
  252. * <u><i>Actions :</i></u><br>
  253. * <ul>
  254. * <li>Check if the new progress number resets the quest to a previous (smaller) step.</li>
  255. * <li>If not, check if quest progress steps have been skipped.</li>
  256. * <li>If skipped, prepare the variable completedStateFlags appropriately to be ready for sending to clients.</li>
  257. * <li>If no steps were skipped, flags do not need to be prepared...</li>
  258. * <li>If the passed step resets the quest to a previous step, reset such that steps after the parameter are not considered, while skipped steps before the parameter, if any, maintain their info.</li>
  259. * </ul>
  260. * @param cond the current quest progress condition (0 - 31 including)
  261. * @param old the previous quest progress condition to check against
  262. */
  263. private void setCond(int cond, int old)
  264. {
  265. if (cond == old)
  266. {
  267. return;
  268. }
  269. int completedStateFlags = 0;
  270. // cond 0 and 1 do not need completedStateFlags. Also, if cond > 1, the 1st step must
  271. // always exist (i.e. it can never be skipped). So if cond is 2, we can still safely
  272. // assume no steps have been skipped.
  273. // Finally, more than 31 steps CANNOT be supported in any way with skipping.
  274. if ((cond < 3) || (cond > 31))
  275. {
  276. unset("__compltdStateFlags");
  277. }
  278. else
  279. {
  280. completedStateFlags = getInt("__compltdStateFlags");
  281. }
  282. // case 1: No steps have been skipped so far...
  283. if (completedStateFlags == 0)
  284. {
  285. // check if this step also doesn't skip anything. If so, no further work is needed
  286. // also, in this case, no work is needed if the state is being reset to a smaller value
  287. // in those cases, skip forward to informing the client about the change...
  288. // ELSE, if we just now skipped for the first time...prepare the flags!!!
  289. if (cond > (old + 1))
  290. {
  291. // set the most significant bit to 1 (indicates that there exist skipped states)
  292. // also, ensure that the least significant bit is an 1 (the first step is never skipped, no matter
  293. // what the cond says)
  294. completedStateFlags = 0x80000001;
  295. // since no flag had been skipped until now, the least significant bits must all
  296. // be set to 1, up until "old" number of bits.
  297. completedStateFlags |= ((1 << old) - 1);
  298. // now, just set the bit corresponding to the passed cond to 1 (current step)
  299. completedStateFlags |= (1 << (cond - 1));
  300. set("__compltdStateFlags", String.valueOf(completedStateFlags));
  301. }
  302. }
  303. // case 2: There were exist previously skipped steps
  304. // if this is a push back to a previous step, clear all completion flags ahead
  305. else if (cond < old)
  306. {
  307. // note, this also unsets the flag indicating that there exist skips
  308. completedStateFlags &= ((1 << cond) - 1);
  309. // now, check if this resulted in no steps being skipped any more
  310. if (completedStateFlags == ((1 << cond) - 1))
  311. {
  312. unset("__compltdStateFlags");
  313. }
  314. else
  315. {
  316. // set the most significant bit back to 1 again, to correctly indicate that this skips states.
  317. // also, ensure that the least significant bit is an 1 (the first step is never skipped, no matter
  318. // what the cond says)
  319. completedStateFlags |= 0x80000001;
  320. set("__compltdStateFlags", String.valueOf(completedStateFlags));
  321. }
  322. }
  323. // If this moves forward, it changes nothing on previously skipped steps.
  324. // Just mark this state and we are done.
  325. else
  326. {
  327. completedStateFlags |= (1 << (cond - 1));
  328. set("__compltdStateFlags", String.valueOf(completedStateFlags));
  329. }
  330. // send a packet to the client to inform it of the quest progress (step change)
  331. _player.sendPacket(new QuestList());
  332. final Quest q = getQuest();
  333. if (!q.isCustomQuest() && (cond > 0))
  334. {
  335. _player.sendPacket(new ExShowQuestMark(q.getId()));
  336. }
  337. }
  338. /**
  339. * Removes a quest variable from the list of existing quest variables.
  340. * @param var the name of the variable to remove
  341. * @return the previous value of the variable or {@code null} if none were found
  342. */
  343. public String unset(String var)
  344. {
  345. if (_vars == null)
  346. {
  347. return null;
  348. }
  349. String old = _vars.remove(var);
  350. if (old != null)
  351. {
  352. Quest.deleteQuestVarInDb(this, var);
  353. }
  354. return old;
  355. }
  356. /**
  357. * Insert (or update) in the database variables that need to stay persistent for this player after a reboot. This function is for storage of values that are not related to a specific quest but are global instead, i.e. can be used by any script.
  358. * @param var the name of the variable to save
  359. * @param value the value of the variable
  360. */
  361. // TODO: these methods should not be here, they could be used by other classes to save some variables, but they can't because they require to create a QuestState first.
  362. public final void saveGlobalQuestVar(String var, String value)
  363. {
  364. try (Connection con = ConnectionFactory.getInstance().getConnection();
  365. PreparedStatement ps = con.prepareStatement("REPLACE INTO character_quest_global_data (charId, var, value) VALUES (?, ?, ?)"))
  366. {
  367. ps.setInt(1, _player.getObjectId());
  368. ps.setString(2, var);
  369. ps.setString(3, value);
  370. ps.executeUpdate();
  371. }
  372. catch (Exception e)
  373. {
  374. _log.log(Level.WARNING, "Could not insert player's global quest variable: " + e.getMessage(), e);
  375. }
  376. }
  377. /**
  378. * Read from the database a previously saved variable for this quest.<br>
  379. * Due to performance considerations, this function should best be used only when the quest is first loaded.<br>
  380. * Subclasses of this class can define structures into which these loaded values can be saved.<br>
  381. * However, on-demand usage of this function throughout the script is not prohibited, only not recommended.<br>
  382. * Values read from this function were entered by calls to "saveGlobalQuestVar".
  383. * @param var the name of the variable whose value to get
  384. * @return the value of the variable or an empty string if the variable does not exist in the database
  385. */
  386. // TODO: these methods should not be here, they could be used by other classes to save some variables, but they can't because they require to create a QuestState first.
  387. public final String getGlobalQuestVar(String var)
  388. {
  389. String result = "";
  390. try (Connection con = ConnectionFactory.getInstance().getConnection();
  391. PreparedStatement ps = con.prepareStatement("SELECT value FROM character_quest_global_data WHERE charId = ? AND var = ?"))
  392. {
  393. ps.setInt(1, _player.getObjectId());
  394. ps.setString(2, var);
  395. try (ResultSet rs = ps.executeQuery())
  396. {
  397. if (rs.first())
  398. {
  399. result = rs.getString(1);
  400. }
  401. }
  402. }
  403. catch (Exception e)
  404. {
  405. _log.log(Level.WARNING, "Could not load player's global quest variable: " + e.getMessage(), e);
  406. }
  407. return result;
  408. }
  409. /**
  410. * Permanently delete a global quest variable from the database.
  411. * @param var the name of the variable to delete
  412. */
  413. public final void deleteGlobalQuestVar(String var)
  414. {
  415. try (Connection con = ConnectionFactory.getInstance().getConnection();
  416. PreparedStatement ps = con.prepareStatement("DELETE FROM character_quest_global_data WHERE charId = ? AND var = ?"))
  417. {
  418. ps.setInt(1, _player.getObjectId());
  419. ps.setString(2, var);
  420. ps.executeUpdate();
  421. }
  422. catch (Exception e)
  423. {
  424. _log.log(Level.WARNING, "could not delete player's global quest variable; charId = " + _player.getObjectId() + ", variable name = " + var + ". Exception: " + e.getMessage(), e);
  425. }
  426. }
  427. /**
  428. * @param var the name of the variable to get
  429. * @return the value of the variable from the list of quest variables
  430. */
  431. public String get(String var)
  432. {
  433. if (_vars == null)
  434. {
  435. return null;
  436. }
  437. return _vars.get(var);
  438. }
  439. /**
  440. * @param var the name of the variable to get
  441. * @return the integer value of the variable or 0 if the variable does not exist or its value is not an integer
  442. */
  443. public int getInt(String var)
  444. {
  445. if (_vars == null)
  446. {
  447. return 0;
  448. }
  449. final String variable = _vars.get(var);
  450. if ((variable == null) || variable.isEmpty())
  451. {
  452. return 0;
  453. }
  454. int varint = 0;
  455. try
  456. {
  457. varint = Integer.parseInt(variable);
  458. }
  459. catch (NumberFormatException nfe)
  460. {
  461. _log.log(Level.INFO, "Quest " + getQuestName() + ", method getInt(" + var + "), tried to parse a non-integer value (" + variable + "). Char Id: " + _player.getObjectId(), nfe);
  462. }
  463. return varint;
  464. }
  465. /**
  466. * Checks if the quest state progress ({@code cond}) is at the specified step.
  467. * @param condition the condition to check against
  468. * @return {@code true} if the quest condition is equal to {@code condition}, {@code false} otherwise
  469. * @see #getInt(String var)
  470. */
  471. public boolean isCond(int condition)
  472. {
  473. return (getInt("cond") == condition);
  474. }
  475. /**
  476. * Sets the quest state progress ({@code cond}) to the specified step.
  477. * @param value the new value of the quest state progress
  478. * @return this {@link QuestState} object
  479. * @see #set(String var, String val)
  480. * @see #setCond(int, boolean)
  481. */
  482. public QuestState setCond(int value)
  483. {
  484. if (isStarted())
  485. {
  486. set("cond", Integer.toString(value));
  487. }
  488. return this;
  489. }
  490. /**
  491. * @return the current quest progress ({@code cond})
  492. */
  493. public int getCond()
  494. {
  495. if (isStarted())
  496. {
  497. return getInt("cond");
  498. }
  499. return 0;
  500. }
  501. /**
  502. * Check if a given variable is set for this quest.
  503. * @param variable the variable to check
  504. * @return {@code true} if the variable is set, {@code false} otherwise
  505. * @see #get(String)
  506. * @see #getInt(String)
  507. * @see #getCond()
  508. */
  509. public boolean isSet(String variable)
  510. {
  511. return (get(variable) != null);
  512. }
  513. /**
  514. * Sets the quest state progress ({@code cond}) to the specified step.
  515. * @param value the new value of the quest state progress
  516. * @param playQuestMiddle if {@code true}, plays "ItemSound.quest_middle"
  517. * @return this {@link QuestState} object
  518. * @see #setCond(int value)
  519. * @see #set(String var, String val)
  520. */
  521. public QuestState setCond(int value, boolean playQuestMiddle)
  522. {
  523. if (!isStarted())
  524. {
  525. return this;
  526. }
  527. set("cond", String.valueOf(value));
  528. if (playQuestMiddle)
  529. {
  530. AbstractScript.playSound(_player, QuestSound.ITEMSOUND_QUEST_MIDDLE);
  531. }
  532. return this;
  533. }
  534. public QuestState setMemoState(int value)
  535. {
  536. set("memoState", String.valueOf(value));
  537. return this;
  538. }
  539. /**
  540. * @return the current Memo State
  541. */
  542. public int getMemoState()
  543. {
  544. if (isStarted())
  545. {
  546. return getInt("memoState");
  547. }
  548. return 0;
  549. }
  550. public boolean isMemoState(int memoState)
  551. {
  552. return (getInt("memoState") == memoState);
  553. }
  554. /**
  555. * Gets the memo state ex.
  556. * @param slot the slot where the value was saved
  557. * @return the memo state ex
  558. */
  559. public int getMemoStateEx(int slot)
  560. {
  561. if (isStarted())
  562. {
  563. return getInt("memoStateEx" + slot);
  564. }
  565. return 0;
  566. }
  567. /**
  568. * Sets the memo state ex.
  569. * @param slot the slot where the value will be saved
  570. * @param value the value
  571. * @return this QuestState
  572. */
  573. public QuestState setMemoStateEx(int slot, int value)
  574. {
  575. set("memoStateEx" + slot, String.valueOf(value));
  576. return this;
  577. }
  578. /**
  579. * Verifies if the given value is equal to the current memos state ex.
  580. * @param slot the slot where the value was saved
  581. * @param memoStateEx the value to verify
  582. * @return {@code true} if the values are equal, {@code false} otherwise
  583. */
  584. public boolean isMemoStateEx(int slot, int memoStateEx)
  585. {
  586. return (getMemoStateEx(slot) == memoStateEx);
  587. }
  588. /**
  589. * Add player to get notification of characters death
  590. * @param character the {@link L2Character} object of the character to get notification of death
  591. */
  592. public void addNotifyOfDeath(L2Character character)
  593. {
  594. if (!(character instanceof L2PcInstance))
  595. {
  596. return;
  597. }
  598. ((L2PcInstance) character).addNotifyQuestOfDeath(this);
  599. }
  600. // TODO: This all remains because of backward compatibility, should be cleared when all scripts are rewritten in java
  601. /**
  602. * Return the quantity of one sort of item hold by the player
  603. * @param itemId the Id of the item wanted to be count
  604. * @return long
  605. */
  606. public long getQuestItemsCount(int itemId)
  607. {
  608. return AbstractScript.getQuestItemsCount(_player, itemId);
  609. }
  610. /**
  611. * @param itemId the Id of the item required
  612. * @return true if item exists in player's inventory, false - if not
  613. */
  614. public boolean hasQuestItems(int itemId)
  615. {
  616. return AbstractScript.hasQuestItems(_player, itemId);
  617. }
  618. /**
  619. * @param itemIds list of items that are required
  620. * @return true if all items exists in player's inventory, false - if not
  621. */
  622. public boolean hasQuestItems(int... itemIds)
  623. {
  624. return AbstractScript.hasQuestItems(_player, itemIds);
  625. }
  626. /**
  627. * Return the level of enchantment on the weapon of the player(Done specifically for weapon SA's)
  628. * @param itemId Id of the item to check enchantment
  629. * @return int
  630. */
  631. public int getEnchantLevel(int itemId)
  632. {
  633. return AbstractScript.getEnchantLevel(_player, itemId);
  634. }
  635. /**
  636. * Give adena to the player
  637. * @param count
  638. * @param applyRates
  639. */
  640. public void giveAdena(long count, boolean applyRates)
  641. {
  642. giveItems(Inventory.ADENA_ID, count, applyRates ? 0 : 1);
  643. }
  644. /**
  645. * Give reward to player using multiplier's
  646. * @param item
  647. */
  648. public void rewardItems(ItemHolder item)
  649. {
  650. AbstractScript.rewardItems(_player, item);
  651. }
  652. /**
  653. * Give reward to player using multiplier's
  654. * @param itemId
  655. * @param count
  656. */
  657. public void rewardItems(int itemId, long count)
  658. {
  659. AbstractScript.rewardItems(_player, itemId, count);
  660. }
  661. /**
  662. * Give item/reward to the player
  663. * @param itemId
  664. * @param count
  665. */
  666. public void giveItems(int itemId, long count)
  667. {
  668. AbstractScript.giveItems(_player, itemId, count, 0);
  669. }
  670. public void giveItems(ItemHolder holder)
  671. {
  672. AbstractScript.giveItems(_player, holder.getId(), holder.getCount(), 0);
  673. }
  674. public void giveItems(int itemId, long count, int enchantlevel)
  675. {
  676. AbstractScript.giveItems(_player, itemId, count, enchantlevel);
  677. }
  678. public void giveItems(int itemId, long count, byte attributeId, int attributeLevel)
  679. {
  680. AbstractScript.giveItems(_player, itemId, count, attributeId, attributeLevel);
  681. }
  682. public boolean giveItemRandomly(int itemId, long amount, long limit, double dropChance, boolean playSound)
  683. {
  684. return AbstractScript.giveItemRandomly(_player, null, itemId, amount, amount, limit, dropChance, playSound);
  685. }
  686. public boolean giveItemRandomly(L2Npc npc, int itemId, long amount, long limit, double dropChance, boolean playSound)
  687. {
  688. return AbstractScript.giveItemRandomly(_player, npc, itemId, amount, amount, limit, dropChance, playSound);
  689. }
  690. public boolean giveItemRandomly(L2Npc npc, int itemId, long minAmount, long maxAmount, long limit, double dropChance, boolean playSound)
  691. {
  692. return AbstractScript.giveItemRandomly(_player, npc, itemId, minAmount, maxAmount, limit, dropChance, playSound);
  693. }
  694. // TODO: More radar functions need to be added when the radar class is complete.
  695. // BEGIN STUFF THAT WILL PROBABLY BE CHANGED
  696. public void addRadar(int x, int y, int z)
  697. {
  698. _player.getRadar().addMarker(x, y, z);
  699. }
  700. public void removeRadar(int x, int y, int z)
  701. {
  702. _player.getRadar().removeMarker(x, y, z);
  703. }
  704. public void clearRadar()
  705. {
  706. _player.getRadar().removeAllMarkers();
  707. }
  708. // END STUFF THAT WILL PROBABLY BE CHANGED
  709. /**
  710. * Remove items from player's inventory when talking to NPC in order to have rewards.<br>
  711. * Actions:<br>
  712. * <ul>
  713. * <li>Destroy quantity of items wanted</li>
  714. * <li>Send new inventory list to player</li>
  715. * </ul>
  716. * @param itemId Identifier of the item
  717. * @param count Quantity of items to destroy
  718. */
  719. public void takeItems(int itemId, long count)
  720. {
  721. AbstractScript.takeItems(_player, itemId, count);
  722. }
  723. /**
  724. * Send a packet in order to play a sound to the player.
  725. * @param sound the name of the sound to play
  726. */
  727. public void playSound(String sound)
  728. {
  729. AbstractScript.playSound(_player, sound);
  730. }
  731. /**
  732. * Send a packet in order to play a sound to the player.
  733. * @param sound the {@link QuestSound} object of the sound to play
  734. */
  735. public void playSound(QuestSound sound)
  736. {
  737. AbstractScript.playSound(_player, sound);
  738. }
  739. /**
  740. * Add XP and SP as quest reward
  741. * @param exp
  742. * @param sp
  743. */
  744. public void addExpAndSp(int exp, int sp)
  745. {
  746. AbstractScript.addExpAndSp(_player, exp, sp);
  747. }
  748. /**
  749. * @param loc
  750. * @return number of ticks from GameTimeController
  751. */
  752. public int getItemEquipped(int loc)
  753. {
  754. return AbstractScript.getItemEquipped(_player, loc);
  755. }
  756. /**
  757. * @return {@code true} if quest is to be exited on clean up by QuestStateManager, {@code false} otherwise
  758. */
  759. public final boolean isExitQuestOnCleanUp()
  760. {
  761. return _isExitQuestOnCleanUp;
  762. }
  763. /**
  764. * @param isExitQuestOnCleanUp {@code true} if quest is to be exited on clean up by QuestStateManager, {@code false} otherwise
  765. */
  766. public void setIsExitQuestOnCleanUp(boolean isExitQuestOnCleanUp)
  767. {
  768. _isExitQuestOnCleanUp = isExitQuestOnCleanUp;
  769. }
  770. /**
  771. * Start a timed event for a quest.<br>
  772. * Will call an event in onEvent/onAdvEvent.
  773. * @param name the name of the timer/event
  774. * @param time time in milliseconds till the event is executed
  775. */
  776. public void startQuestTimer(String name, long time)
  777. {
  778. getQuest().startQuestTimer(name, time, null, _player, false);
  779. }
  780. /**
  781. * Start a timed event for a quest.<br>
  782. * Will call an event in onEvent/onAdvEvent.
  783. * @param name the name of the timer/event
  784. * @param time time in milliseconds till the event is executed
  785. * @param npc the L2Npc associated with this event
  786. */
  787. public void startQuestTimer(String name, long time, L2Npc npc)
  788. {
  789. getQuest().startQuestTimer(name, time, npc, _player, false);
  790. }
  791. /**
  792. * Start a repeating timed event for a quest.<br>
  793. * Will call an event in onEvent/onAdvEvent.
  794. * @param name the name of the timer/event
  795. * @param time time in milliseconds till the event is executed/repeated
  796. */
  797. public void startRepeatingQuestTimer(String name, long time)
  798. {
  799. getQuest().startQuestTimer(name, time, null, _player, true);
  800. }
  801. /**
  802. * Start a repeating timed event for a quest.<br>
  803. * Will call an event in onEvent/onAdvEvent.
  804. * @param name the name of the timer/event
  805. * @param time time in milliseconds till the event is executed/repeated
  806. * @param npc the L2Npc associated with this event
  807. */
  808. public void startRepeatingQuestTimer(String name, long time, L2Npc npc)
  809. {
  810. getQuest().startQuestTimer(name, time, npc, _player, true);
  811. }
  812. /**
  813. * @param name the name of the QuestTimer required
  814. * @return the {@link QuestTimer} object with the specified name or {@code null} if it doesn't exist
  815. */
  816. public final QuestTimer getQuestTimer(String name)
  817. {
  818. return getQuest().getQuestTimer(name, null, _player);
  819. }
  820. // --- Spawn methods ---
  821. /**
  822. * Add a temporary spawn of the specified npc.<br>
  823. * Player's coordinates will be used for the spawn.
  824. * @param npcId the Id of the npc to spawn
  825. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  826. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  827. */
  828. public L2Npc addSpawn(int npcId)
  829. {
  830. return addSpawn(npcId, _player.getX(), _player.getY(), _player.getZ(), 0, false, 0, false);
  831. }
  832. /**
  833. * Add a temporary spawn of the specified npc.<br>
  834. * Player's coordinates will be used for the spawn.
  835. * @param npcId the Id of the npc to spawn
  836. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  837. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  838. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  839. */
  840. public L2Npc addSpawn(int npcId, int despawnDelay)
  841. {
  842. return addSpawn(npcId, _player.getX(), _player.getY(), _player.getZ(), 0, false, despawnDelay, false);
  843. }
  844. /**
  845. * Add a temporary spawn of the specified npc.
  846. * @param npcId the Id of the npc to spawn
  847. * @param x the X coordinate of the npc spawn location
  848. * @param y the Y coordinate of the npc spawn location
  849. * @param z the Z coordinate (height) of the npc spawn location
  850. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  851. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  852. */
  853. public L2Npc addSpawn(int npcId, int x, int y, int z)
  854. {
  855. return addSpawn(npcId, x, y, z, 0, false, 0, false);
  856. }
  857. /**
  858. * Add a temporary spawn of the specified npc.
  859. * @param npcId the Id of the npc to spawn
  860. * @param x the X coordinate of the npc spawn location
  861. * @param y the Y coordinate of the npc spawn location
  862. * @param z the Z coordinate (height) of the npc spawn location
  863. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  864. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  865. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  866. */
  867. public L2Npc addSpawn(int npcId, int x, int y, int z, int despawnDelay)
  868. {
  869. return addSpawn(npcId, x, y, z, 0, false, despawnDelay, false);
  870. }
  871. /**
  872. * Add a temporary spawn of the specified npc.
  873. * @param npcId the Id of the npc to spawn
  874. * @param cha the character whose coordinates will be used for the npc spawn
  875. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  876. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  877. */
  878. public L2Npc addSpawn(int npcId, L2Character cha)
  879. {
  880. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), true, 0, false);
  881. }
  882. /**
  883. * Add a temporary spawn of the specified npc.
  884. * @param npcId the Id of the npc to spawn
  885. * @param cha the character whose coordinates will be used for the npc spawn
  886. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  887. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  888. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  889. */
  890. public L2Npc addSpawn(int npcId, L2Character cha, int despawnDelay)
  891. {
  892. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), true, despawnDelay, false);
  893. }
  894. /**
  895. * Add a temporary spawn of the specified npc.
  896. * @param npcId the Id of the npc to spawn
  897. * @param cha the character whose coordinates will be used for the npc spawn
  898. * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
  899. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  900. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  901. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  902. */
  903. public L2Npc addSpawn(int npcId, L2Character cha, boolean randomOffset, int despawnDelay)
  904. {
  905. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), randomOffset, despawnDelay, false);
  906. }
  907. /**
  908. * Add a temporary spawn of the specified npc.
  909. * @param npcId the Id of the npc to spawn
  910. * @param x the X coordinate of the npc spawn location
  911. * @param y the Y coordinate of the npc spawn location
  912. * @param z the Z coordinate (height) of the npc spawn location
  913. * @param heading the heading of the npc
  914. * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
  915. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  916. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  917. * @see #addSpawn(int, int, int, int, int, boolean, int, boolean)
  918. */
  919. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay)
  920. {
  921. return addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, false);
  922. }
  923. /**
  924. * Add a temporary spawn of the specified npc.
  925. * @param npcId the Id of the npc to spawn
  926. * @param x the X coordinate of the npc spawn location
  927. * @param y the Y coordinate of the npc spawn location
  928. * @param z the Z coordinate (height) of the npc spawn location
  929. * @param heading the heading of the npc
  930. * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
  931. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  932. * @param isSummonSpawn if {@code true}, displays a summon animation on npc spawn (default: {@code false})
  933. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  934. * @see #addSpawn(int)
  935. * @see #addSpawn(int, int)
  936. * @see #addSpawn(int, L2Character)
  937. * @see #addSpawn(int, L2Character, int)
  938. * @see #addSpawn(int, int, int, int)
  939. * @see #addSpawn(int, L2Character, boolean, int)
  940. * @see #addSpawn(int, int, int, int, int)
  941. * @see #addSpawn(int, int, int, int, int, boolean, int)
  942. * @see #addSpawn(int, int, int, int, int, boolean, int, boolean)
  943. */
  944. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  945. {
  946. return AbstractScript.addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, isSummonSpawn);
  947. }
  948. /**
  949. * Send an HTML file to the specified player.
  950. * @param filename the name of the HTML file to show
  951. * @return the contents of the HTML file that was sent to the player
  952. * @see #showHtmlFile(String, L2Npc)
  953. * @see Quest#showHtmlFile(L2PcInstance, String)
  954. * @see Quest#showHtmlFile(L2PcInstance, String, L2Npc)
  955. */
  956. public String showHtmlFile(String filename)
  957. {
  958. return showHtmlFile(filename, null);
  959. }
  960. /**
  961. * Send an HTML file to the specified player.
  962. * @param filename the name of the HTML file to show
  963. * @param npc the NPC that is showing the HTML file
  964. * @return the contents of the HTML file that was sent to the player
  965. * @see Quest#showHtmlFile(L2PcInstance, String)
  966. * @see Quest#showHtmlFile(L2PcInstance, String, L2Npc)
  967. */
  968. public String showHtmlFile(String filename, L2Npc npc)
  969. {
  970. return getQuest().showHtmlFile(_player, filename, npc);
  971. }
  972. /**
  973. * Set condition to 1, state to STARTED and play the "ItemSound.quest_accept".<br>
  974. * Works only if state is CREATED and the quest is not a custom quest.
  975. * @return the newly created {@code QuestState} object
  976. */
  977. public QuestState startQuest()
  978. {
  979. if (isCreated() && !getQuest().isCustomQuest())
  980. {
  981. set("cond", "1");
  982. setState(State.STARTED);
  983. playSound(QuestSound.ITEMSOUND_QUEST_ACCEPT);
  984. }
  985. return this;
  986. }
  987. /**
  988. * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
  989. * If {@code type} is {@code QuestType.ONE_TIME}, also removes all other quest data associated with this quest.
  990. * @param type the {@link QuestType} of the quest
  991. * @return this {@link QuestState} object
  992. * @see #exitQuest(QuestType type, boolean playExitQuest)
  993. * @see #exitQuest(boolean repeatable)
  994. * @see #exitQuest(boolean repeatable, boolean playExitQuest)
  995. */
  996. public QuestState exitQuest(QuestType type)
  997. {
  998. switch (type)
  999. {
  1000. case DAILY:
  1001. {
  1002. exitQuest(false);
  1003. setRestartTime();
  1004. break;
  1005. }
  1006. // case ONE_TIME:
  1007. // case REPEATABLE:
  1008. default:
  1009. {
  1010. exitQuest(type == QuestType.REPEATABLE);
  1011. break;
  1012. }
  1013. }
  1014. return this;
  1015. }
  1016. /**
  1017. * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
  1018. * If {@code type} is {@code QuestType.ONE_TIME}, also removes all other quest data associated with this quest.
  1019. * @param type the {@link QuestType} of the quest
  1020. * @param playExitQuest if {@code true}, plays "ItemSound.quest_finish"
  1021. * @return this {@link QuestState} object
  1022. * @see #exitQuest(QuestType type)
  1023. * @see #exitQuest(boolean repeatable)
  1024. * @see #exitQuest(boolean repeatable, boolean playExitQuest)
  1025. */
  1026. public QuestState exitQuest(QuestType type, boolean playExitQuest)
  1027. {
  1028. exitQuest(type);
  1029. if (playExitQuest)
  1030. {
  1031. playSound(QuestSound.ITEMSOUND_QUEST_FINISH);
  1032. }
  1033. return this;
  1034. }
  1035. /**
  1036. * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
  1037. * If {@code repeatable} is set to {@code false}, also removes all other quest data associated with this quest.
  1038. * @param repeatable if {@code true}, deletes all data and variables of this quest, otherwise keeps them
  1039. * @return this {@link QuestState} object
  1040. * @see #exitQuest(QuestType type)
  1041. * @see #exitQuest(QuestType type, boolean playExitQuest)
  1042. * @see #exitQuest(boolean repeatable, boolean playExitQuest)
  1043. */
  1044. public QuestState exitQuest(boolean repeatable)
  1045. {
  1046. _player.removeNotifyQuestOfDeath(this);
  1047. if (!isStarted())
  1048. {
  1049. return this;
  1050. }
  1051. // Clean registered quest items
  1052. getQuest().removeRegisteredQuestItems(_player);
  1053. Quest.deleteQuestInDb(this, repeatable);
  1054. if (repeatable)
  1055. {
  1056. _player.delQuestState(getQuestName());
  1057. _player.sendPacket(new QuestList());
  1058. }
  1059. else
  1060. {
  1061. setState(State.COMPLETED);
  1062. }
  1063. _vars = null;
  1064. return this;
  1065. }
  1066. /**
  1067. * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
  1068. * If {@code repeatable} is set to {@code false}, also removes all other quest data associated with this quest.
  1069. * @param repeatable if {@code true}, deletes all data and variables of this quest, otherwise keeps them
  1070. * @param playExitQuest if {@code true}, plays "ItemSound.quest_finish"
  1071. * @return this {@link QuestState} object
  1072. * @see #exitQuest(QuestType type)
  1073. * @see #exitQuest(QuestType type, boolean playExitQuest)
  1074. * @see #exitQuest(boolean repeatable)
  1075. */
  1076. public QuestState exitQuest(boolean repeatable, boolean playExitQuest)
  1077. {
  1078. exitQuest(repeatable);
  1079. if (playExitQuest)
  1080. {
  1081. playSound(QuestSound.ITEMSOUND_QUEST_FINISH);
  1082. }
  1083. return this;
  1084. }
  1085. public void showQuestionMark(int number)
  1086. {
  1087. _player.sendPacket(new TutorialShowQuestionMark(number));
  1088. }
  1089. // TODO make tutorial voices the same as quest sounds
  1090. public void playTutorialVoice(String voice)
  1091. {
  1092. _player.sendPacket(new PlaySound(2, voice, 0, 0, _player.getX(), _player.getY(), _player.getZ()));
  1093. }
  1094. /**
  1095. * Used only in 255_Tutorial
  1096. * @param html
  1097. */
  1098. public void showTutorialHTML(String html)
  1099. {
  1100. String text = HtmCache.getInstance().getHtm(_player.getHtmlPrefix(), "data/scripts/quests/255_Tutorial/" + html);
  1101. if (text == null)
  1102. {
  1103. _log.warning("missing html page data/scripts/quests/255_Tutorial/" + html);
  1104. text = "<html><body>File data/scripts/quests/255_Tutorial/" + html + " not found or file is empty.</body></html>";
  1105. }
  1106. _player.sendPacket(new TutorialShowHtml(text));
  1107. }
  1108. /**
  1109. * Used only in 255_Tutorial
  1110. */
  1111. public void closeTutorialHtml()
  1112. {
  1113. _player.sendPacket(TutorialCloseHtml.STATIC_PACKET);
  1114. }
  1115. /**
  1116. * Used only in 255_Tutorial
  1117. * @param number
  1118. */
  1119. public void onTutorialClientEvent(int number)
  1120. {
  1121. _player.sendPacket(new TutorialEnableClientEvent(number));
  1122. }
  1123. /**
  1124. * Set the restart time for the daily quests.<br>
  1125. * The time is hardcoded at {@link Quest#getResetHour()} hours, {@link Quest#getResetMinutes()} minutes of the following day.<br>
  1126. * It can be overridden in scripts (quests).
  1127. */
  1128. public void setRestartTime()
  1129. {
  1130. final Calendar reDo = Calendar.getInstance();
  1131. if (reDo.get(Calendar.HOUR_OF_DAY) >= getQuest().getResetHour())
  1132. {
  1133. reDo.add(Calendar.DATE, 1);
  1134. }
  1135. reDo.set(Calendar.HOUR_OF_DAY, getQuest().getResetHour());
  1136. reDo.set(Calendar.MINUTE, getQuest().getResetMinutes());
  1137. set("restartTime", String.valueOf(reDo.getTimeInMillis()));
  1138. }
  1139. /**
  1140. * Check if a daily quest is available to be started over.
  1141. * @return {@code true} if the quest is available, {@code false} otherwise.
  1142. */
  1143. public boolean isNowAvailable()
  1144. {
  1145. final String val = get("restartTime");
  1146. return ((val == null) || !Util.isDigit(val)) || (Long.parseLong(val) <= System.currentTimeMillis());
  1147. }
  1148. }