QuestState.java 38 KB

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