QuestState.java 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /*
  2. * Copyright (C) 2004-2014 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.L2DatabaseFactory;
  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 FastMap "vars"</li>
  201. * <li>If the key represented by "var" exists in FastMap "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 = L2DatabaseFactory.getInstance().getConnection();
  365. PreparedStatement statement = con.prepareStatement("REPLACE INTO character_quest_global_data (charId, var, value) VALUES (?, ?, ?)"))
  366. {
  367. statement.setInt(1, _player.getObjectId());
  368. statement.setString(2, var);
  369. statement.setString(3, value);
  370. statement.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 = L2DatabaseFactory.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 = L2DatabaseFactory.getInstance().getConnection();
  416. PreparedStatement statement = con.prepareStatement("DELETE FROM character_quest_global_data WHERE charId = ? AND var = ?"))
  417. {
  418. statement.setInt(1, _player.getObjectId());
  419. statement.setString(2, var);
  420. statement.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 memoStateEx the value to verify
  581. * @return {@code true} if the values are equal, {@code false} otherwise
  582. */
  583. public boolean isMemoStateEx(int memoStateEx)
  584. {
  585. return (getInt("memoStateEx") == memoStateEx);
  586. }
  587. /**
  588. * Add player to get notification of characters death
  589. * @param character the {@link L2Character} object of the character to get notification of death
  590. */
  591. public void addNotifyOfDeath(L2Character character)
  592. {
  593. if (!(character instanceof L2PcInstance))
  594. {
  595. return;
  596. }
  597. ((L2PcInstance) character).addNotifyQuestOfDeath(this);
  598. }
  599. // TODO: This all remains because of backward compatibility, should be cleared when all scripts are rewritten in java
  600. /**
  601. * Return the quantity of one sort of item hold by the player
  602. * @param itemId the Id of the item wanted to be count
  603. * @return long
  604. */
  605. public long getQuestItemsCount(int itemId)
  606. {
  607. return AbstractScript.getQuestItemsCount(_player, itemId);
  608. }
  609. /**
  610. * @param itemId the Id of the item required
  611. * @return true if item exists in player's inventory, false - if not
  612. */
  613. public boolean hasQuestItems(int itemId)
  614. {
  615. return AbstractScript.hasQuestItems(_player, itemId);
  616. }
  617. /**
  618. * @param itemIds list of items that are required
  619. * @return true if all items exists in player's inventory, false - if not
  620. */
  621. public boolean hasQuestItems(int... itemIds)
  622. {
  623. return AbstractScript.hasQuestItems(_player, itemIds);
  624. }
  625. /**
  626. * Return the level of enchantment on the weapon of the player(Done specifically for weapon SA's)
  627. * @param itemId Id of the item to check enchantment
  628. * @return int
  629. */
  630. public int getEnchantLevel(int itemId)
  631. {
  632. return AbstractScript.getEnchantLevel(_player, itemId);
  633. }
  634. /**
  635. * Give adena to the player
  636. * @param count
  637. * @param applyRates
  638. */
  639. public void giveAdena(long count, boolean applyRates)
  640. {
  641. giveItems(Inventory.ADENA_ID, count, applyRates ? 0 : 1);
  642. }
  643. /**
  644. * Give reward to player using multiplier's
  645. * @param item
  646. */
  647. public void rewardItems(ItemHolder item)
  648. {
  649. AbstractScript.rewardItems(_player, item);
  650. }
  651. /**
  652. * Give reward to player using multiplier's
  653. * @param itemId
  654. * @param count
  655. */
  656. public void rewardItems(int itemId, long count)
  657. {
  658. AbstractScript.rewardItems(_player, itemId, count);
  659. }
  660. /**
  661. * Give item/reward to the player
  662. * @param itemId
  663. * @param count
  664. */
  665. public void giveItems(int itemId, long count)
  666. {
  667. AbstractScript.giveItems(_player, itemId, count, 0);
  668. }
  669. public void giveItems(ItemHolder holder)
  670. {
  671. AbstractScript.giveItems(_player, holder.getId(), holder.getCount(), 0);
  672. }
  673. public void giveItems(int itemId, long count, int enchantlevel)
  674. {
  675. AbstractScript.giveItems(_player, itemId, count, enchantlevel);
  676. }
  677. public void giveItems(int itemId, long count, byte attributeId, int attributeLevel)
  678. {
  679. AbstractScript.giveItems(_player, itemId, count, attributeId, attributeLevel);
  680. }
  681. public boolean giveItemRandomly(int itemId, long amount, long limit, double dropChance, boolean playSound)
  682. {
  683. return AbstractScript.giveItemRandomly(_player, null, itemId, amount, amount, limit, dropChance, playSound);
  684. }
  685. public boolean giveItemRandomly(L2Npc npc, int itemId, long amount, long limit, double dropChance, boolean playSound)
  686. {
  687. return AbstractScript.giveItemRandomly(_player, npc, itemId, amount, amount, limit, dropChance, playSound);
  688. }
  689. public boolean giveItemRandomly(L2Npc npc, int itemId, long minAmount, long maxAmount, long limit, double dropChance, boolean playSound)
  690. {
  691. return AbstractScript.giveItemRandomly(_player, npc, itemId, minAmount, maxAmount, limit, dropChance, playSound);
  692. }
  693. // TODO: More radar functions need to be added when the radar class is complete.
  694. // BEGIN STUFF THAT WILL PROBABLY BE CHANGED
  695. public void addRadar(int x, int y, int z)
  696. {
  697. _player.getRadar().addMarker(x, y, z);
  698. }
  699. public void removeRadar(int x, int y, int z)
  700. {
  701. _player.getRadar().removeMarker(x, y, z);
  702. }
  703. public void clearRadar()
  704. {
  705. _player.getRadar().removeAllMarkers();
  706. }
  707. // END STUFF THAT WILL PROBABLY BE CHANGED
  708. /**
  709. * Remove items from player's inventory when talking to NPC in order to have rewards.<br>
  710. * Actions:<br>
  711. * <ul>
  712. * <li>Destroy quantity of items wanted</li>
  713. * <li>Send new inventory list to player</li>
  714. * </ul>
  715. * @param itemId Identifier of the item
  716. * @param count Quantity of items to destroy
  717. */
  718. public void takeItems(int itemId, long count)
  719. {
  720. AbstractScript.takeItems(_player, itemId, count);
  721. }
  722. /**
  723. * Send a packet in order to play a sound to the player.
  724. * @param sound the name of the sound to play
  725. */
  726. public void playSound(String sound)
  727. {
  728. AbstractScript.playSound(_player, sound);
  729. }
  730. /**
  731. * Send a packet in order to play a sound to the player.
  732. * @param sound the {@link QuestSound} object of the sound to play
  733. */
  734. public void playSound(QuestSound sound)
  735. {
  736. AbstractScript.playSound(_player, sound);
  737. }
  738. /**
  739. * Add XP and SP as quest reward
  740. * @param exp
  741. * @param sp
  742. */
  743. public void addExpAndSp(int exp, int sp)
  744. {
  745. AbstractScript.addExpAndSp(_player, exp, sp);
  746. }
  747. /**
  748. * @param loc
  749. * @return number of ticks from GameTimeController
  750. */
  751. public int getItemEquipped(int loc)
  752. {
  753. return AbstractScript.getItemEquipped(_player, loc);
  754. }
  755. /**
  756. * @return {@code true} if quest is to be exited on clean up by QuestStateManager, {@code false} otherwise
  757. */
  758. public final boolean isExitQuestOnCleanUp()
  759. {
  760. return _isExitQuestOnCleanUp;
  761. }
  762. /**
  763. * @param isExitQuestOnCleanUp {@code true} if quest is to be exited on clean up by QuestStateManager, {@code false} otherwise
  764. */
  765. public void setIsExitQuestOnCleanUp(boolean isExitQuestOnCleanUp)
  766. {
  767. _isExitQuestOnCleanUp = isExitQuestOnCleanUp;
  768. }
  769. /**
  770. * Start a timed event for a quest.<br>
  771. * Will call an event in onEvent/onAdvEvent.
  772. * @param name the name of the timer/event
  773. * @param time time in milliseconds till the event is executed
  774. */
  775. public void startQuestTimer(String name, long time)
  776. {
  777. getQuest().startQuestTimer(name, time, null, _player, false);
  778. }
  779. /**
  780. * Start a timed event for a quest.<br>
  781. * Will call an event in onEvent/onAdvEvent.
  782. * @param name the name of the timer/event
  783. * @param time time in milliseconds till the event is executed
  784. * @param npc the L2Npc associated with this event
  785. */
  786. public void startQuestTimer(String name, long time, L2Npc npc)
  787. {
  788. getQuest().startQuestTimer(name, time, npc, _player, false);
  789. }
  790. /**
  791. * Start a repeating timed event for a quest.<br>
  792. * Will call an event in onEvent/onAdvEvent.
  793. * @param name the name of the timer/event
  794. * @param time time in milliseconds till the event is executed/repeated
  795. */
  796. public void startRepeatingQuestTimer(String name, long time)
  797. {
  798. getQuest().startQuestTimer(name, time, null, _player, true);
  799. }
  800. /**
  801. * Start a repeating timed event for a quest.<br>
  802. * Will call an event in onEvent/onAdvEvent.
  803. * @param name the name of the timer/event
  804. * @param time time in milliseconds till the event is executed/repeated
  805. * @param npc the L2Npc associated with this event
  806. */
  807. public void startRepeatingQuestTimer(String name, long time, L2Npc npc)
  808. {
  809. getQuest().startQuestTimer(name, time, npc, _player, true);
  810. }
  811. /**
  812. * @param name the name of the QuestTimer required
  813. * @return the {@link QuestTimer} object with the specified name or {@code null} if it doesn't exist
  814. */
  815. public final QuestTimer getQuestTimer(String name)
  816. {
  817. return getQuest().getQuestTimer(name, null, _player);
  818. }
  819. // --- Spawn methods ---
  820. /**
  821. * Add a temporary spawn of the specified npc.<br>
  822. * Player's coordinates will be used for the spawn.
  823. * @param npcId the Id of the npc to spawn
  824. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  825. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  826. */
  827. public L2Npc addSpawn(int npcId)
  828. {
  829. return addSpawn(npcId, _player.getX(), _player.getY(), _player.getZ(), 0, false, 0, false);
  830. }
  831. /**
  832. * Add a temporary spawn of the specified npc.<br>
  833. * Player's coordinates will be used for the spawn.
  834. * @param npcId the Id of the npc to spawn
  835. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  836. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  837. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  838. */
  839. public L2Npc addSpawn(int npcId, int despawnDelay)
  840. {
  841. return addSpawn(npcId, _player.getX(), _player.getY(), _player.getZ(), 0, false, despawnDelay, false);
  842. }
  843. /**
  844. * Add a temporary spawn of the specified npc.
  845. * @param npcId the Id of the npc to spawn
  846. * @param x the X coordinate of the npc spawn location
  847. * @param y the Y coordinate of the npc spawn location
  848. * @param z the Z coordinate (height) of the npc spawn location
  849. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  850. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  851. */
  852. public L2Npc addSpawn(int npcId, int x, int y, int z)
  853. {
  854. return addSpawn(npcId, x, y, z, 0, false, 0, false);
  855. }
  856. /**
  857. * Add a temporary spawn of the specified npc.
  858. * @param npcId the Id of the npc to spawn
  859. * @param x the X coordinate of the npc spawn location
  860. * @param y the Y coordinate of the npc spawn location
  861. * @param z the Z coordinate (height) of the npc spawn location
  862. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  863. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  864. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  865. */
  866. public L2Npc addSpawn(int npcId, int x, int y, int z, int despawnDelay)
  867. {
  868. return addSpawn(npcId, x, y, z, 0, false, despawnDelay, false);
  869. }
  870. /**
  871. * Add a temporary spawn of the specified npc.
  872. * @param npcId the Id of the npc to spawn
  873. * @param cha the character whose coordinates will be used for the npc spawn
  874. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  875. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  876. */
  877. public L2Npc addSpawn(int npcId, L2Character cha)
  878. {
  879. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), true, 0, false);
  880. }
  881. /**
  882. * Add a temporary spawn of the specified npc.
  883. * @param npcId the Id of the npc to spawn
  884. * @param cha the character whose coordinates will be used for the npc spawn
  885. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  886. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  887. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  888. */
  889. public L2Npc addSpawn(int npcId, L2Character cha, int despawnDelay)
  890. {
  891. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), true, despawnDelay, false);
  892. }
  893. /**
  894. * Add a temporary spawn of the specified npc.
  895. * @param npcId the Id of the npc to spawn
  896. * @param cha the character whose coordinates will be used for the npc spawn
  897. * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
  898. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  899. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  900. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  901. */
  902. public L2Npc addSpawn(int npcId, L2Character cha, boolean randomOffset, int despawnDelay)
  903. {
  904. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), randomOffset, despawnDelay, false);
  905. }
  906. /**
  907. * Add a temporary spawn of the specified npc.
  908. * @param npcId the Id of the npc to spawn
  909. * @param x the X coordinate of the npc spawn location
  910. * @param y the Y coordinate of the npc spawn location
  911. * @param z the Z coordinate (height) of the npc spawn location
  912. * @param heading the heading of the npc
  913. * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
  914. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  915. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  916. * @see #addSpawn(int, int, int, int, int, boolean, int, boolean)
  917. */
  918. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay)
  919. {
  920. return addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, false);
  921. }
  922. /**
  923. * Add a temporary spawn of the specified npc.
  924. * @param npcId the Id of the npc to spawn
  925. * @param x the X coordinate of the npc spawn location
  926. * @param y the Y coordinate of the npc spawn location
  927. * @param z the Z coordinate (height) of the npc spawn location
  928. * @param heading the heading of the npc
  929. * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
  930. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  931. * @param isSummonSpawn if {@code true}, displays a summon animation on npc spawn (default: {@code false})
  932. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  933. * @see #addSpawn(int)
  934. * @see #addSpawn(int, int)
  935. * @see #addSpawn(int, L2Character)
  936. * @see #addSpawn(int, L2Character, int)
  937. * @see #addSpawn(int, int, int, int)
  938. * @see #addSpawn(int, L2Character, boolean, int)
  939. * @see #addSpawn(int, int, int, int, int)
  940. * @see #addSpawn(int, int, int, int, int, boolean, int)
  941. * @see #addSpawn(int, int, int, int, int, boolean, int, boolean)
  942. */
  943. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  944. {
  945. return AbstractScript.addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, isSummonSpawn);
  946. }
  947. /**
  948. * Send an HTML file to the specified player.
  949. * @param filename the name of the HTML file to show
  950. * @return the contents of the HTML file that was sent to the player
  951. * @see #showHtmlFile(String, L2Npc)
  952. * @see Quest#showHtmlFile(L2PcInstance, String)
  953. * @see Quest#showHtmlFile(L2PcInstance, String, L2Npc)
  954. */
  955. public String showHtmlFile(String filename)
  956. {
  957. return showHtmlFile(filename, null);
  958. }
  959. /**
  960. * Send an HTML file to the specified player.
  961. * @param filename the name of the HTML file to show
  962. * @param npc the NPC that is showing the HTML file
  963. * @return the contents of the HTML file that was sent to the player
  964. * @see Quest#showHtmlFile(L2PcInstance, String)
  965. * @see Quest#showHtmlFile(L2PcInstance, String, L2Npc)
  966. */
  967. public String showHtmlFile(String filename, L2Npc npc)
  968. {
  969. return getQuest().showHtmlFile(_player, filename, npc);
  970. }
  971. /**
  972. * Set condition to 1, state to STARTED and play the "ItemSound.quest_accept".<br>
  973. * Works only if state is CREATED and the quest is not a custom quest.
  974. * @return the newly created {@code QuestState} object
  975. */
  976. public QuestState startQuest()
  977. {
  978. if (isCreated() && !getQuest().isCustomQuest())
  979. {
  980. set("cond", "1");
  981. setState(State.STARTED);
  982. playSound(QuestSound.ITEMSOUND_QUEST_ACCEPT);
  983. }
  984. return this;
  985. }
  986. /**
  987. * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
  988. * If {@code type} is {@code QuestType.ONE_TIME}, also removes all other quest data associated with this quest.
  989. * @param type the {@link QuestType} of the quest
  990. * @return this {@link QuestState} object
  991. * @see #exitQuest(QuestType type, boolean playExitQuest)
  992. * @see #exitQuest(boolean repeatable)
  993. * @see #exitQuest(boolean repeatable, boolean playExitQuest)
  994. */
  995. public QuestState exitQuest(QuestType type)
  996. {
  997. switch (type)
  998. {
  999. case DAILY:
  1000. {
  1001. exitQuest(false);
  1002. setRestartTime();
  1003. break;
  1004. }
  1005. // case ONE_TIME:
  1006. // case REPEATABLE:
  1007. default:
  1008. {
  1009. exitQuest(type == QuestType.REPEATABLE);
  1010. break;
  1011. }
  1012. }
  1013. return this;
  1014. }
  1015. /**
  1016. * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
  1017. * If {@code type} is {@code QuestType.ONE_TIME}, also removes all other quest data associated with this quest.
  1018. * @param type the {@link QuestType} of the quest
  1019. * @param playExitQuest if {@code true}, plays "ItemSound.quest_finish"
  1020. * @return this {@link QuestState} object
  1021. * @see #exitQuest(QuestType type)
  1022. * @see #exitQuest(boolean repeatable)
  1023. * @see #exitQuest(boolean repeatable, boolean playExitQuest)
  1024. */
  1025. public QuestState exitQuest(QuestType type, boolean playExitQuest)
  1026. {
  1027. exitQuest(type);
  1028. if (playExitQuest)
  1029. {
  1030. playSound(QuestSound.ITEMSOUND_QUEST_FINISH);
  1031. }
  1032. return this;
  1033. }
  1034. /**
  1035. * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
  1036. * If {@code repeatable} is set to {@code false}, also removes all other quest data associated with this quest.
  1037. * @param repeatable if {@code true}, deletes all data and variables of this quest, otherwise keeps them
  1038. * @return this {@link QuestState} object
  1039. * @see #exitQuest(QuestType type)
  1040. * @see #exitQuest(QuestType type, boolean playExitQuest)
  1041. * @see #exitQuest(boolean repeatable, boolean playExitQuest)
  1042. */
  1043. public QuestState exitQuest(boolean repeatable)
  1044. {
  1045. _player.removeNotifyQuestOfDeath(this);
  1046. if (!isStarted())
  1047. {
  1048. return this;
  1049. }
  1050. // Clean registered quest items
  1051. getQuest().removeRegisteredQuestItems(_player);
  1052. Quest.deleteQuestInDb(this, repeatable);
  1053. if (repeatable)
  1054. {
  1055. _player.delQuestState(getQuestName());
  1056. _player.sendPacket(new QuestList());
  1057. }
  1058. else
  1059. {
  1060. setState(State.COMPLETED);
  1061. }
  1062. _vars = null;
  1063. return this;
  1064. }
  1065. /**
  1066. * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
  1067. * If {@code repeatable} is set to {@code false}, also removes all other quest data associated with this quest.
  1068. * @param repeatable if {@code true}, deletes all data and variables of this quest, otherwise keeps them
  1069. * @param playExitQuest if {@code true}, plays "ItemSound.quest_finish"
  1070. * @return this {@link QuestState} object
  1071. * @see #exitQuest(QuestType type)
  1072. * @see #exitQuest(QuestType type, boolean playExitQuest)
  1073. * @see #exitQuest(boolean repeatable)
  1074. */
  1075. public QuestState exitQuest(boolean repeatable, boolean playExitQuest)
  1076. {
  1077. exitQuest(repeatable);
  1078. if (playExitQuest)
  1079. {
  1080. playSound(QuestSound.ITEMSOUND_QUEST_FINISH);
  1081. }
  1082. return this;
  1083. }
  1084. public void showQuestionMark(int number)
  1085. {
  1086. _player.sendPacket(new TutorialShowQuestionMark(number));
  1087. }
  1088. // TODO make tutorial voices the same as quest sounds
  1089. public void playTutorialVoice(String voice)
  1090. {
  1091. _player.sendPacket(new PlaySound(2, voice, 0, 0, _player.getX(), _player.getY(), _player.getZ()));
  1092. }
  1093. /**
  1094. * Used only in 255_Tutorial
  1095. * @param html
  1096. */
  1097. public void showTutorialHTML(String html)
  1098. {
  1099. String text = HtmCache.getInstance().getHtm(_player.getHtmlPrefix(), "data/scripts/quests/255_Tutorial/" + html);
  1100. if (text == null)
  1101. {
  1102. _log.warning("missing html page data/scripts/quests/255_Tutorial/" + html);
  1103. text = "<html><body>File data/scripts/quests/255_Tutorial/" + html + " not found or file is empty.</body></html>";
  1104. }
  1105. _player.sendPacket(new TutorialShowHtml(text));
  1106. }
  1107. /**
  1108. * Used only in 255_Tutorial
  1109. */
  1110. public void closeTutorialHtml()
  1111. {
  1112. _player.sendPacket(TutorialCloseHtml.STATIC_PACKET);
  1113. }
  1114. /**
  1115. * Used only in 255_Tutorial
  1116. * @param number
  1117. */
  1118. public void onTutorialClientEvent(int number)
  1119. {
  1120. _player.sendPacket(new TutorialEnableClientEvent(number));
  1121. }
  1122. /**
  1123. * Set the restart time for the daily quests.<br>
  1124. * The time is hardcoded at {@link Quest#getResetHour()} hours, {@link Quest#getResetMinutes()} minutes of the following day.<br>
  1125. * It can be overridden in scripts (quests).
  1126. */
  1127. public void setRestartTime()
  1128. {
  1129. final Calendar reDo = Calendar.getInstance();
  1130. if (reDo.get(Calendar.HOUR_OF_DAY) >= getQuest().getResetHour())
  1131. {
  1132. reDo.add(Calendar.DATE, 1);
  1133. }
  1134. reDo.set(Calendar.HOUR_OF_DAY, getQuest().getResetHour());
  1135. reDo.set(Calendar.MINUTE, getQuest().getResetMinutes());
  1136. set("restartTime", String.valueOf(reDo.getTimeInMillis()));
  1137. }
  1138. /**
  1139. * Check if a daily quest is available to be started over.
  1140. * @return {@code true} if the quest is available, {@code false} otherwise.
  1141. */
  1142. public boolean isNowAvailable()
  1143. {
  1144. final String val = get("restartTime");
  1145. return ((val == null) || !Util.isDigit(val)) || (Long.parseLong(val) <= System.currentTimeMillis());
  1146. }
  1147. }