2
0

QuestState.java 39 KB

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