2
0

Quest.java 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.model.quest;
  16. import java.io.PrintWriter;
  17. import java.io.StringWriter;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.util.Collection;
  21. import java.util.Map;
  22. import java.util.concurrent.locks.ReentrantReadWriteLock;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import javolution.util.FastList;
  26. import javolution.util.FastMap;
  27. import net.sf.l2j.Config;
  28. import net.sf.l2j.L2DatabaseFactory;
  29. import net.sf.l2j.gameserver.ThreadPoolManager;
  30. import net.sf.l2j.gameserver.cache.HtmCache;
  31. import net.sf.l2j.gameserver.datatables.NpcTable;
  32. import net.sf.l2j.gameserver.instancemanager.QuestManager;
  33. import net.sf.l2j.gameserver.model.L2Character;
  34. import net.sf.l2j.gameserver.model.L2Object;
  35. import net.sf.l2j.gameserver.model.L2Party;
  36. import net.sf.l2j.gameserver.model.L2Skill;
  37. import net.sf.l2j.gameserver.model.L2Spawn;
  38. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  39. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  40. import net.sf.l2j.gameserver.network.serverpackets.ActionFailed;
  41. import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  42. import net.sf.l2j.gameserver.scripting.ManagedScript;
  43. import net.sf.l2j.gameserver.scripting.ScriptManager;
  44. import net.sf.l2j.gameserver.templates.L2NpcTemplate;
  45. import net.sf.l2j.util.Rnd;
  46. /**
  47. * @author Luis Arias
  48. *
  49. */
  50. public class Quest extends ManagedScript
  51. {
  52. protected static final Logger _log = Logger.getLogger(Quest.class.getName());
  53. /** HashMap containing events from String value of the event */
  54. private static Map<String, Quest> _allEventsS = new FastMap<String, Quest>();
  55. /** HashMap containing lists of timers from the name of the timer */
  56. private Map<String, FastList<QuestTimer>> _allEventTimers = new FastMap<String, FastList<QuestTimer>>();
  57. private final ReentrantReadWriteLock _rwLock = new ReentrantReadWriteLock();
  58. private final int _questId;
  59. private final String _name;
  60. private final String _descr;
  61. private final byte _initialState = State.CREATED;
  62. // NOTE: questItemIds will be overridden by child classes. Ideally, it should be
  63. // protected instead of public. However, quest scripts written in Jython will
  64. // have trouble with protected, as Jython only knows private and public...
  65. // In fact, protected will typically be considered private thus breaking the scripts.
  66. // Leave this as public as a workaround.
  67. public int[] questItemIds = null;
  68. /**
  69. * Return collection view of the values contains in the allEventS
  70. * @return Collection<Quest>
  71. */
  72. public static Collection<Quest> findAllEvents()
  73. {
  74. return _allEventsS.values();
  75. }
  76. /**
  77. * (Constructor)Add values to class variables and put the quest in HashMaps.
  78. * @param questId : int pointing out the ID of the quest
  79. * @param name : String corresponding to the name of the quest
  80. * @param descr : String for the description of the quest
  81. */
  82. public Quest(int questId, String name, String descr)
  83. {
  84. _questId = questId;
  85. _name = name;
  86. _descr = descr;
  87. if (questId != 0)
  88. {
  89. QuestManager.getInstance().addQuest(Quest.this);
  90. }
  91. else
  92. {
  93. _allEventsS.put(name, this);
  94. }
  95. init_LoadGlobalData();
  96. }
  97. /**
  98. * The function init_LoadGlobalData is, by default, called by the constructor of all quests.
  99. * Children of this class can implement this function in order to define what variables
  100. * to load and what structures to save them in. By default, nothing is loaded.
  101. */
  102. protected void init_LoadGlobalData()
  103. {
  104. }
  105. /**
  106. * The function saveGlobalData is, by default, called at shutdown, for all quests, by the QuestManager.
  107. * Children of this class can implement this function in order to convert their structures
  108. * into <var, value> tuples and make calls to save them to the database, if needed.
  109. * By default, nothing is saved.
  110. */
  111. public void saveGlobalData()
  112. {
  113. }
  114. public static enum QuestEventType
  115. {
  116. ON_FIRST_TALK(false), // control the first dialog shown by NPCs when they are clicked (some quests must override the default npc action)
  117. QUEST_START(true), // onTalk action from start npcs
  118. ON_TALK(true), // onTalk action from npcs participating in a quest
  119. ON_ATTACK(true), // onAttack action triggered when a mob gets attacked by someone
  120. ON_KILL(true), // onKill action triggered when a mob gets killed.
  121. ON_SPAWN(true), // onSpawn action triggered when an NPC is spawned or respawned.
  122. ON_SKILL_SEE(true), // NPC or Mob saw a person casting a skill (regardless what the target is).
  123. ON_FACTION_CALL(true), // NPC or Mob saw a person casting a skill (regardless what the target is).
  124. ON_AGGRO_RANGE_ENTER(true), // a person came within the Npc/Mob's range
  125. ON_SPELL_FINISHED(true); // on spell finished action when npc finish casting skill
  126. // control whether this event type is allowed for the same npc template in multiple quests
  127. // or if the npc must be registered in at most one quest for the specified event
  128. private boolean _allowMultipleRegistration;
  129. QuestEventType(boolean allowMultipleRegistration)
  130. {
  131. _allowMultipleRegistration = allowMultipleRegistration;
  132. }
  133. public boolean isMultipleRegistrationAllowed()
  134. {
  135. return _allowMultipleRegistration;
  136. }
  137. }
  138. /**
  139. * Return ID of the quest
  140. * @return int
  141. */
  142. public int getQuestIntId()
  143. {
  144. return _questId;
  145. }
  146. /**
  147. * Add a new QuestState to the database and return it.
  148. * @param player
  149. * @return QuestState : QuestState created
  150. */
  151. public QuestState newQuestState(L2PcInstance player)
  152. {
  153. QuestState qs = new QuestState(this, player, getInitialState());
  154. Quest.createQuestInDb(qs);
  155. return qs;
  156. }
  157. /**
  158. * Return initial state of the quest
  159. * @return State
  160. */
  161. public byte getInitialState()
  162. {
  163. return _initialState;
  164. }
  165. /**
  166. * Return name of the quest
  167. * @return String
  168. */
  169. public String getName()
  170. {
  171. return _name;
  172. }
  173. /**
  174. * Return description of the quest
  175. * @return String
  176. */
  177. public String getDescr()
  178. {
  179. return _descr;
  180. }
  181. /**
  182. * Add a timer to the quest, if it doesn't exist already
  183. * @param name: name of the timer (also passed back as "event" in onAdvEvent)
  184. * @param time: time in ms for when to fire the timer
  185. * @param npc: npc associated with this timer (can be null)
  186. * @param player: player associated with this timer (can be null)
  187. */
  188. public void startQuestTimer(String name, long time, L2NpcInstance npc, L2PcInstance player)
  189. {
  190. startQuestTimer(name, time, npc, player, false);
  191. }
  192. /**
  193. * Add a timer to the quest, if it doesn't exist already. If the timer is repeatable,
  194. * it will auto-fire automatically, at a fixed rate, until explicitly canceled.
  195. * @param name: name of the timer (also passed back as "event" in onAdvEvent)
  196. * @param time: time in ms for when to fire the timer
  197. * @param npc: npc associated with this timer (can be null)
  198. * @param player: player associated with this timer (can be null)
  199. * @param repeatable: indicates if the timer is repeatable or one-time.
  200. */
  201. public void startQuestTimer(String name, long time, L2NpcInstance npc, L2PcInstance player, boolean repeating)
  202. {
  203. // Add quest timer if timer doesn't already exist
  204. FastList<QuestTimer> timers = getQuestTimers(name);
  205. // no timer exists with the same name, at all
  206. if (timers == null)
  207. {
  208. timers = new FastList<QuestTimer>();
  209. timers.add(new QuestTimer(this, name, time, npc, player, repeating));
  210. _allEventTimers.put(name, timers);
  211. }
  212. // a timer with this name exists, but may not be for the same set of npc and player
  213. else
  214. {
  215. // if there exists a timer with this name, allow the timer only if the [npc, player] set is unique
  216. // nulls act as wildcards
  217. if (getQuestTimer(name, npc, player) == null)
  218. {
  219. try
  220. {
  221. _rwLock.writeLock().lock();
  222. timers.add(new QuestTimer(this, name, time, npc, player, repeating));
  223. }
  224. finally
  225. {
  226. _rwLock.writeLock().unlock();
  227. }
  228. }
  229. }
  230. // ignore the startQuestTimer in all other cases (timer is already started)
  231. }
  232. public QuestTimer getQuestTimer(String name, L2NpcInstance npc, L2PcInstance player)
  233. {
  234. FastList<QuestTimer> qt = getQuestTimers(name);
  235. if (qt == null || qt.isEmpty())
  236. return null;
  237. try
  238. {
  239. _rwLock.readLock().lock();
  240. for (QuestTimer timer : qt)
  241. {
  242. if (timer != null)
  243. {
  244. if (timer.isMatch(this, name, npc, player))
  245. return timer;
  246. }
  247. }
  248. }
  249. finally
  250. {
  251. _rwLock.readLock().unlock();
  252. }
  253. return null;
  254. }
  255. private FastList<QuestTimer> getQuestTimers(String name)
  256. {
  257. return _allEventTimers.get(name);
  258. }
  259. public void cancelQuestTimers(String name)
  260. {
  261. FastList<QuestTimer> timers = getQuestTimers(name);
  262. if (timers == null)
  263. return;
  264. try
  265. {
  266. _rwLock.writeLock().lock();
  267. for (QuestTimer timer : timers)
  268. {
  269. if (timer != null)
  270. {
  271. timer.cancel();
  272. }
  273. }
  274. }
  275. finally
  276. {
  277. _rwLock.writeLock().unlock();
  278. }
  279. }
  280. public void cancelQuestTimer(String name, L2NpcInstance npc, L2PcInstance player)
  281. {
  282. QuestTimer timer = getQuestTimer(name, npc, player);
  283. if (timer != null)
  284. timer.cancel();
  285. }
  286. public void removeQuestTimer(QuestTimer timer)
  287. {
  288. if (timer == null)
  289. return;
  290. FastList<QuestTimer> timers = getQuestTimers(timer.getName());
  291. if (timers == null)
  292. return;
  293. try
  294. {
  295. _rwLock.writeLock().lock();
  296. timers.remove(timer);
  297. }
  298. finally
  299. {
  300. _rwLock.writeLock().unlock();
  301. }
  302. }
  303. // these are methods to call from java
  304. public final boolean notifyAttack(L2NpcInstance npc, L2PcInstance attacker, int damage, boolean isPet)
  305. {
  306. String res = null;
  307. try
  308. {
  309. res = onAttack(npc, attacker, damage, isPet);
  310. }
  311. catch (Exception e)
  312. {
  313. return showError(attacker, e);
  314. }
  315. return showResult(attacker, res);
  316. }
  317. public final boolean notifyDeath(L2Character killer, L2Character victim, QuestState qs)
  318. {
  319. String res = null;
  320. try
  321. {
  322. res = onDeath(killer, victim, qs);
  323. }
  324. catch (Exception e)
  325. {
  326. return showError(qs.getPlayer(), e);
  327. }
  328. return showResult(qs.getPlayer(), res);
  329. }
  330. public final boolean notifySpellFinished(L2NpcInstance instance, L2PcInstance player, L2Skill skill)
  331. {
  332. String res = null;
  333. try
  334. {
  335. res = onSpellFinished(instance, player, skill);
  336. }
  337. catch (Exception e)
  338. {
  339. return showError(player, e);
  340. }
  341. return showResult(player, res);
  342. }
  343. public final boolean notifySpawn(L2NpcInstance npc)
  344. {
  345. try
  346. {
  347. onSpawn(npc);
  348. }
  349. catch (Exception e)
  350. {
  351. _log.log(Level.WARNING, "", e);
  352. return true;
  353. }
  354. return false;
  355. }
  356. public final boolean notifyEvent(String event, L2NpcInstance npc, L2PcInstance player)
  357. {
  358. String res = null;
  359. try
  360. {
  361. res = onAdvEvent(event, npc, player);
  362. }
  363. catch (Exception e)
  364. {
  365. return showError(player, e);
  366. }
  367. return showResult(player, res);
  368. }
  369. public final boolean notifyKill(L2NpcInstance npc, L2PcInstance killer, boolean isPet)
  370. {
  371. String res = null;
  372. try
  373. {
  374. res = onKill(npc, killer, isPet);
  375. }
  376. catch (Exception e)
  377. {
  378. return showError(killer, e);
  379. }
  380. return showResult(killer, res);
  381. }
  382. public final boolean notifyTalk(L2NpcInstance npc, QuestState qs)
  383. {
  384. String res = null;
  385. try
  386. {
  387. res = onTalk(npc, qs.getPlayer());
  388. }
  389. catch (Exception e)
  390. {
  391. return showError(qs.getPlayer(), e);
  392. }
  393. qs.getPlayer().setLastQuestNpcObject(npc.getObjectId());
  394. return showResult(qs.getPlayer(), res);
  395. }
  396. // override the default NPC dialogs when a quest defines this for the given NPC
  397. public final boolean notifyFirstTalk(L2NpcInstance npc, L2PcInstance player)
  398. {
  399. String res = null;
  400. try
  401. {
  402. res = onFirstTalk(npc, player);
  403. }
  404. catch (Exception e)
  405. {
  406. return showError(player, e);
  407. }
  408. // if the quest returns text to display, display it.
  409. if (res != null && res.length() > 0)
  410. return showResult(player, res);
  411. // else tell the player that
  412. else
  413. player.sendPacket(ActionFailed.STATIC_PACKET);
  414. // note: if the default html for this npc needs to be shown, onFirstTalk should
  415. // call npc.showChatWindow(player) and then return null.
  416. return true;
  417. }
  418. public class tmpOnSkillSee implements Runnable
  419. {
  420. private L2NpcInstance _npc;
  421. private L2PcInstance _caster;
  422. private L2Skill _skill;
  423. private L2Object[] _targets;
  424. private boolean _isPet;
  425. public tmpOnSkillSee(L2NpcInstance npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
  426. {
  427. _npc = npc;
  428. _caster = caster;
  429. _skill = skill;
  430. _targets = targets;
  431. _isPet = isPet;
  432. }
  433. public void run()
  434. {
  435. String res = null;
  436. try
  437. {
  438. res = onSkillSee(_npc, _caster, _skill, _targets, _isPet);
  439. }
  440. catch (Exception e)
  441. {
  442. showError(_caster, e);
  443. }
  444. showResult(_caster, res);
  445. }
  446. }
  447. public final boolean notifySkillSee(L2NpcInstance npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
  448. {
  449. ThreadPoolManager.getInstance().executeAi(new tmpOnSkillSee(npc, caster, skill, targets, isPet));
  450. return true;
  451. }
  452. public final boolean notifyFactionCall(L2NpcInstance npc, L2NpcInstance caller, L2PcInstance attacker, boolean isPet)
  453. {
  454. String res = null;
  455. try
  456. {
  457. res = onFactionCall(npc, caller, attacker, isPet);
  458. }
  459. catch (Exception e)
  460. {
  461. return showError(attacker, e);
  462. }
  463. return showResult(attacker, res);
  464. }
  465. public final boolean notifyAggroRangeEnter(L2NpcInstance npc, L2PcInstance player, boolean isPet)
  466. {
  467. String res = null;
  468. try
  469. {
  470. res = onAggroRangeEnter(npc, player, isPet);
  471. }
  472. catch (Exception e)
  473. {
  474. return showError(player, e);
  475. }
  476. return showResult(player, res);
  477. }
  478. // these are methods that java calls to invoke scripts
  479. public String onAttack(L2NpcInstance npc, L2PcInstance attacker, int damage, boolean isPet)
  480. {
  481. return null;
  482. }
  483. public String onDeath(L2Character killer, L2Character victim, QuestState qs)
  484. {
  485. if (killer instanceof L2NpcInstance)
  486. return onAdvEvent("", (L2NpcInstance) killer, qs.getPlayer());
  487. else
  488. return onAdvEvent("", null, qs.getPlayer());
  489. }
  490. public String onAdvEvent(String event, L2NpcInstance npc, L2PcInstance player)
  491. {
  492. // if not overridden by a subclass, then default to the returned value of the simpler (and older) onEvent override
  493. // if the player has a state, use it as parameter in the next call, else return null
  494. QuestState qs = player.getQuestState(getName());
  495. if (qs != null)
  496. return onEvent(event, qs);
  497. return null;
  498. }
  499. public String onEvent(String event, QuestState qs)
  500. {
  501. return null;
  502. }
  503. public String onKill(L2NpcInstance npc, L2PcInstance killer, boolean isPet)
  504. {
  505. return null;
  506. }
  507. public String onTalk(L2NpcInstance npc, L2PcInstance talker)
  508. {
  509. return null;
  510. }
  511. public String onFirstTalk(L2NpcInstance npc, L2PcInstance player)
  512. {
  513. return null;
  514. }
  515. public String onSkillSee(L2NpcInstance npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
  516. {
  517. return null;
  518. }
  519. public String onSpellFinished(L2NpcInstance npc, L2PcInstance player, L2Skill skill)
  520. {
  521. return null;
  522. }
  523. public String onSpawn(L2NpcInstance npc)
  524. {
  525. return null;
  526. }
  527. public String onFactionCall(L2NpcInstance npc, L2NpcInstance caller, L2PcInstance attacker, boolean isPet)
  528. {
  529. return null;
  530. }
  531. public String onAggroRangeEnter(L2NpcInstance npc, L2PcInstance player, boolean isPet)
  532. {
  533. return null;
  534. }
  535. /**
  536. * Show message error to player who has an access level greater than 0
  537. * @param player : L2PcInstance
  538. * @param t : Throwable
  539. * @return boolean
  540. */
  541. public boolean showError(L2PcInstance player, Throwable t)
  542. {
  543. _log.log(Level.WARNING, this.getScriptFile().getAbsolutePath(), t);
  544. if (player.getAccessLevel().isGm())
  545. {
  546. StringWriter sw = new StringWriter();
  547. PrintWriter pw = new PrintWriter(sw);
  548. t.printStackTrace(pw);
  549. pw.close();
  550. String res = "<html><body><title>Script error</title>" + sw.toString() + "</body></html>";
  551. return showResult(player, res);
  552. }
  553. return false;
  554. }
  555. /**
  556. * Show a message to player.<BR><BR>
  557. * <U><I>Concept : </I></U><BR>
  558. * 3 cases are managed according to the value of the parameter "res" :<BR>
  559. * <LI><U>"res" ends with string ".html" :</U> an HTML is opened in order to be shown in a dialog box</LI>
  560. * <LI><U>"res" starts with "<html>" :</U> the message hold in "res" is shown in a dialog box</LI>
  561. * <LI><U>otherwise :</U> the message held in "res" is shown in chat box</LI>
  562. * @param qs : QuestState
  563. * @param res : String pointing out the message to show at the player
  564. * @return boolean
  565. */
  566. public boolean showResult(L2PcInstance player, String res)
  567. {
  568. if (res == null || res.equals(""))
  569. return true;
  570. if (res.endsWith(".htm"))
  571. {
  572. showHtmlFile(player, res);
  573. }
  574. else if (res.startsWith("<html>"))
  575. {
  576. NpcHtmlMessage npcReply = new NpcHtmlMessage(5);
  577. npcReply.setHtml(res);
  578. npcReply.replace("%playername%", player.getName());
  579. player.sendPacket(npcReply);
  580. player.sendPacket(ActionFailed.STATIC_PACKET);
  581. }
  582. else
  583. {
  584. player.sendMessage(res);
  585. }
  586. return false;
  587. }
  588. /**
  589. * Add quests to the L2PCInstance of the player.<BR><BR>
  590. * <U><I>Action : </U></I><BR>
  591. * Add state of quests, drops and variables for quests in the HashMap _quest of L2PcInstance
  592. * @param player : Player who is entering the world
  593. */
  594. public final static void playerEnter(L2PcInstance player)
  595. {
  596. java.sql.Connection con = null;
  597. try
  598. {
  599. // Get list of quests owned by the player from database
  600. con = L2DatabaseFactory.getInstance().getConnection();
  601. PreparedStatement statement;
  602. PreparedStatement invalidQuestData = con.prepareStatement("DELETE FROM character_quests WHERE charId=? and name=?");
  603. PreparedStatement invalidQuestDataVar = con.prepareStatement("delete FROM character_quests WHERE charId=? and name=? and var=?");
  604. statement = con.prepareStatement("SELECT name,value FROM character_quests WHERE charId=? AND var=?");
  605. statement.setInt(1, player.getObjectId());
  606. statement.setString(2, "<state>");
  607. ResultSet rs = statement.executeQuery();
  608. while (rs.next())
  609. {
  610. // Get ID of the quest and ID of its state
  611. String questId = rs.getString("name");
  612. String statename = rs.getString("value");
  613. // Search quest associated with the ID
  614. Quest q = QuestManager.getInstance().getQuest(questId);
  615. if (q == null)
  616. {
  617. _log.finer("Unknown quest " + questId + " for player " + player.getName());
  618. if (Config.AUTODELETE_INVALID_QUEST_DATA)
  619. {
  620. invalidQuestData.setInt(1, player.getObjectId());
  621. invalidQuestData.setString(2, questId);
  622. invalidQuestData.executeUpdate();
  623. }
  624. continue;
  625. }
  626. // Create a new QuestState for the player that will be added to the player's list of quests
  627. new QuestState(q, player, State.getStateId(statename));
  628. }
  629. rs.close();
  630. invalidQuestData.close();
  631. statement.close();
  632. // Get list of quests owned by the player from the DB in order to add variables used in the quest.
  633. statement = con.prepareStatement("SELECT name,var,value FROM character_quests WHERE charId=? AND var<>?");
  634. statement.setInt(1, player.getObjectId());
  635. statement.setString(2, "<state>");
  636. rs = statement.executeQuery();
  637. while (rs.next())
  638. {
  639. String questId = rs.getString("name");
  640. String var = rs.getString("var");
  641. String value = rs.getString("value");
  642. // Get the QuestState saved in the loop before
  643. QuestState qs = player.getQuestState(questId);
  644. if (qs == null)
  645. {
  646. _log.finer("Lost variable " + var + " in quest " + questId + " for player " + player.getName());
  647. if (Config.AUTODELETE_INVALID_QUEST_DATA)
  648. {
  649. invalidQuestDataVar.setInt(1, player.getObjectId());
  650. invalidQuestDataVar.setString(2, questId);
  651. invalidQuestDataVar.setString(3, var);
  652. invalidQuestDataVar.executeUpdate();
  653. }
  654. continue;
  655. }
  656. // Add parameter to the quest
  657. qs.setInternal(var, value);
  658. }
  659. rs.close();
  660. invalidQuestDataVar.close();
  661. statement.close();
  662. }
  663. catch (Exception e)
  664. {
  665. _log.log(Level.WARNING, "could not insert char quest:", e);
  666. }
  667. finally
  668. {
  669. try
  670. {
  671. con.close();
  672. }
  673. catch (Exception e)
  674. {
  675. }
  676. }
  677. // events
  678. for (String name : _allEventsS.keySet())
  679. {
  680. player.processQuestEvent(name, "enter");
  681. }
  682. }
  683. /**
  684. * Insert (or Update) in the database variables that need to stay persistant for this quest after a reboot.
  685. * This function is for storage of values that do not related to a specific player but are
  686. * global for all characters. For example, if we need to disable a quest-gatekeeper until
  687. * a certain time (as is done with some grand-boss gatekeepers), we can save that time in the DB.
  688. * @param var : String designating the name of the variable for the quest
  689. * @param value : String designating the value of the variable for the quest
  690. */
  691. public final void saveGlobalQuestVar(String var, String value)
  692. {
  693. java.sql.Connection con = null;
  694. try
  695. {
  696. con = L2DatabaseFactory.getInstance().getConnection();
  697. PreparedStatement statement;
  698. statement = con.prepareStatement("REPLACE INTO quest_global_data (quest_name,var,value) VALUES (?,?,?)");
  699. statement.setString(1, getName());
  700. statement.setString(2, var);
  701. statement.setString(3, value);
  702. statement.executeUpdate();
  703. statement.close();
  704. }
  705. catch (Exception e)
  706. {
  707. _log.log(Level.WARNING, "could not insert global quest variable:", e);
  708. }
  709. finally
  710. {
  711. try
  712. {
  713. con.close();
  714. }
  715. catch (Exception e)
  716. {
  717. }
  718. }
  719. }
  720. /**
  721. * Read from the database a previously saved variable for this quest.
  722. * Due to performance considerations, this function should best be used only when the quest is first loaded.
  723. * Subclasses of this class can define structures into which these loaded values can be saved.
  724. * However, on-demand usage of this function throughout the script is not prohibited, only not recommended.
  725. * Values read from this function were entered by calls to "saveGlobalQuestVar"
  726. * @param var : String designating the name of the variable for the quest
  727. * @return String : String representing the loaded value for the passed var, or an empty string if the var was invalid
  728. */
  729. public final String loadGlobalQuestVar(String var)
  730. {
  731. String result = "";
  732. java.sql.Connection con = null;
  733. try
  734. {
  735. con = L2DatabaseFactory.getInstance().getConnection();
  736. PreparedStatement statement;
  737. statement = con.prepareStatement("SELECT value FROM quest_global_data WHERE quest_name = ? AND var = ?");
  738. statement.setString(1, getName());
  739. statement.setString(2, var);
  740. ResultSet rs = statement.executeQuery();
  741. if (rs.first())
  742. result = rs.getString(1);
  743. rs.close();
  744. statement.close();
  745. }
  746. catch (Exception e)
  747. {
  748. _log.log(Level.WARNING, "could not load global quest variable:", e);
  749. }
  750. finally
  751. {
  752. try
  753. {
  754. con.close();
  755. }
  756. catch (Exception e)
  757. {
  758. }
  759. }
  760. return result;
  761. }
  762. /**
  763. * Permanently delete from the database a global quest variable that was previously saved for this quest.
  764. * @param var : String designating the name of the variable for the quest
  765. */
  766. public final void deleteGlobalQuestVar(String var)
  767. {
  768. java.sql.Connection con = null;
  769. try
  770. {
  771. con = L2DatabaseFactory.getInstance().getConnection();
  772. PreparedStatement statement;
  773. statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ? AND var = ?");
  774. statement.setString(1, getName());
  775. statement.setString(2, var);
  776. statement.executeUpdate();
  777. statement.close();
  778. }
  779. catch (Exception e)
  780. {
  781. _log.log(Level.WARNING, "could not delete global quest variable:", e);
  782. }
  783. finally
  784. {
  785. try
  786. {
  787. con.close();
  788. }
  789. catch (Exception e)
  790. {
  791. }
  792. }
  793. }
  794. /**
  795. * Permanently delete from the database all global quest variables that was previously saved for this quest.
  796. */
  797. public final void deleteAllGlobalQuestVars()
  798. {
  799. java.sql.Connection con = null;
  800. try
  801. {
  802. con = L2DatabaseFactory.getInstance().getConnection();
  803. PreparedStatement statement;
  804. statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ?");
  805. statement.setString(1, getName());
  806. statement.executeUpdate();
  807. statement.close();
  808. }
  809. catch (Exception e)
  810. {
  811. _log.log(Level.WARNING, "could not delete global quest variables:", e);
  812. }
  813. finally
  814. {
  815. try
  816. {
  817. con.close();
  818. }
  819. catch (Exception e)
  820. {
  821. }
  822. }
  823. }
  824. /**
  825. * Insert in the database the quest for the player.
  826. * @param qs : QuestState pointing out the state of the quest
  827. * @param var : String designating the name of the variable for the quest
  828. * @param value : String designating the value of the variable for the quest
  829. */
  830. public static void createQuestVarInDb(QuestState qs, String var, String value)
  831. {
  832. java.sql.Connection con = null;
  833. try
  834. {
  835. con = L2DatabaseFactory.getInstance().getConnection();
  836. PreparedStatement statement;
  837. statement = con.prepareStatement("INSERT INTO character_quests (charId,name,var,value) VALUES (?,?,?,?)");
  838. statement.setInt(1, qs.getPlayer().getObjectId());
  839. statement.setString(2, qs.getQuestName());
  840. statement.setString(3, var);
  841. statement.setString(4, value);
  842. statement.executeUpdate();
  843. statement.close();
  844. }
  845. catch (Exception e)
  846. {
  847. _log.log(Level.WARNING, "could not insert char quest:", e);
  848. }
  849. finally
  850. {
  851. try
  852. {
  853. con.close();
  854. }
  855. catch (Exception e)
  856. {
  857. }
  858. }
  859. }
  860. /**
  861. * Update the value of the variable "var" for the quest.<BR><BR>
  862. * <U><I>Actions :</I></U><BR>
  863. * The selection of the right record is made with :
  864. * <LI>charId = qs.getPlayer().getObjectID()</LI>
  865. * <LI>name = qs.getQuest().getName()</LI>
  866. * <LI>var = var</LI>
  867. * <BR><BR>
  868. * The modification made is :
  869. * <LI>value = parameter value</LI>
  870. * @param qs : Quest State
  871. * @param var : String designating the name of the variable for quest
  872. * @param value : String designating the value of the variable for quest
  873. */
  874. public static void updateQuestVarInDb(QuestState qs, String var, String value)
  875. {
  876. java.sql.Connection con = null;
  877. try
  878. {
  879. con = L2DatabaseFactory.getInstance().getConnection();
  880. PreparedStatement statement;
  881. statement = con.prepareStatement("UPDATE character_quests SET value=? WHERE charId=? AND name=? AND var = ?");
  882. statement.setString(1, value);
  883. statement.setInt(2, qs.getPlayer().getObjectId());
  884. statement.setString(3, qs.getQuestName());
  885. statement.setString(4, var);
  886. statement.executeUpdate();
  887. statement.close();
  888. }
  889. catch (Exception e)
  890. {
  891. _log.log(Level.WARNING, "could not update char quest:", e);
  892. }
  893. finally
  894. {
  895. try
  896. {
  897. con.close();
  898. }
  899. catch (Exception e)
  900. {
  901. }
  902. }
  903. }
  904. /**
  905. * Delete a variable of player's quest from the database.
  906. * @param qs : object QuestState pointing out the player's quest
  907. * @param var : String designating the variable characterizing the quest
  908. */
  909. public static void deleteQuestVarInDb(QuestState qs, String var)
  910. {
  911. java.sql.Connection con = null;
  912. try
  913. {
  914. con = L2DatabaseFactory.getInstance().getConnection();
  915. PreparedStatement statement;
  916. statement = con.prepareStatement("DELETE FROM character_quests WHERE charId=? AND name=? AND var=?");
  917. statement.setInt(1, qs.getPlayer().getObjectId());
  918. statement.setString(2, qs.getQuestName());
  919. statement.setString(3, var);
  920. statement.executeUpdate();
  921. statement.close();
  922. }
  923. catch (Exception e)
  924. {
  925. _log.log(Level.WARNING, "could not delete char quest:", e);
  926. }
  927. finally
  928. {
  929. try
  930. {
  931. con.close();
  932. }
  933. catch (Exception e)
  934. {
  935. }
  936. }
  937. }
  938. /**
  939. * Delete the player's quest from database.
  940. * @param qs : QuestState pointing out the player's quest
  941. */
  942. public static void deleteQuestInDb(QuestState qs)
  943. {
  944. java.sql.Connection con = null;
  945. try
  946. {
  947. con = L2DatabaseFactory.getInstance().getConnection();
  948. PreparedStatement statement;
  949. statement = con.prepareStatement("DELETE FROM character_quests WHERE charId=? AND name=?");
  950. statement.setInt(1, qs.getPlayer().getObjectId());
  951. statement.setString(2, qs.getQuestName());
  952. statement.executeUpdate();
  953. statement.close();
  954. }
  955. catch (Exception e)
  956. {
  957. _log.log(Level.WARNING, "could not delete char quest:", e);
  958. }
  959. finally
  960. {
  961. try
  962. {
  963. con.close();
  964. }
  965. catch (Exception e)
  966. {
  967. }
  968. }
  969. }
  970. /**
  971. * Create a record in database for quest.<BR><BR>
  972. * <U><I>Actions :</I></U><BR>
  973. * Use fucntion createQuestVarInDb() with following parameters :<BR>
  974. * <LI>QuestState : parameter sq that puts in fields of database :
  975. * <UL type="square">
  976. * <LI>charId : ID of the player</LI>
  977. * <LI>name : name of the quest</LI>
  978. * </UL>
  979. * </LI>
  980. * <LI>var : string "&lt;state&gt;" as the name of the variable for the quest</LI>
  981. * <LI>val : string corresponding at the ID of the state (in fact, initial state)</LI>
  982. * @param qs : QuestState
  983. */
  984. public static void createQuestInDb(QuestState qs)
  985. {
  986. createQuestVarInDb(qs, "<state>", State.getStateName(qs.getState()));
  987. }
  988. /**
  989. * Update informations regarding quest in database.<BR>
  990. * <U><I>Actions :</I></U><BR>
  991. * <LI>Get ID state of the quest recorded in object qs</LI>
  992. * <LI>Test if quest is completed. If true, add a star (*) before the ID state</LI>
  993. * <LI>Save in database the ID state (with or without the star) for the variable called "&lt;state&gt;" of the quest</LI>
  994. * @param qs : QuestState
  995. */
  996. public static void updateQuestInDb(QuestState qs)
  997. {
  998. String val = State.getStateName(qs.getState());
  999. updateQuestVarInDb(qs, "<state>", val);
  1000. }
  1001. /**
  1002. * Add this quest to the list of quests that the passed mob will respond to for the specified Event type.<BR><BR>
  1003. * @param npcId : id of the NPC to register
  1004. * @param eventType : type of event being registered
  1005. * @return L2NpcTemplate : Npc Template corresponding to the npcId, or null if the id is invalid
  1006. */
  1007. public L2NpcTemplate addEventId(int npcId, QuestEventType eventType)
  1008. {
  1009. try
  1010. {
  1011. L2NpcTemplate t = NpcTable.getInstance().getTemplate(npcId);
  1012. if (t != null)
  1013. {
  1014. t.addQuestEvent(eventType, this);
  1015. }
  1016. return t;
  1017. }
  1018. catch (Exception e)
  1019. {
  1020. e.printStackTrace();
  1021. return null;
  1022. }
  1023. }
  1024. /**
  1025. * Add the quest to the NPC's startQuest
  1026. * @param npcId
  1027. * @return L2NpcTemplate : Start NPC
  1028. */
  1029. public L2NpcTemplate addStartNpc(int npcId)
  1030. {
  1031. return addEventId(npcId, Quest.QuestEventType.QUEST_START);
  1032. }
  1033. /**
  1034. * Add the quest to the NPC's first-talk (default action dialog)
  1035. * @param npcId
  1036. * @return L2NpcTemplate : Start NPC
  1037. */
  1038. public L2NpcTemplate addFirstTalkId(int npcId)
  1039. {
  1040. return addEventId(npcId, Quest.QuestEventType.ON_FIRST_TALK);
  1041. }
  1042. /**
  1043. * Add this quest to the list of quests that the passed mob will respond to for Attack Events.<BR><BR>
  1044. * @param attackId
  1045. * @return int : attackId
  1046. */
  1047. public L2NpcTemplate addAttackId(int attackId)
  1048. {
  1049. return addEventId(attackId, Quest.QuestEventType.ON_ATTACK);
  1050. }
  1051. /**
  1052. * Add this quest to the list of quests that the passed mob will respond to for Kill Events.<BR><BR>
  1053. * @param killId
  1054. * @return int : killId
  1055. */
  1056. public L2NpcTemplate addKillId(int killId)
  1057. {
  1058. return addEventId(killId, Quest.QuestEventType.ON_KILL);
  1059. }
  1060. /**
  1061. * Add this quest to the list of quests that the passed npc will respond to for Talk Events.<BR><BR>
  1062. * @param talkId : ID of the NPC
  1063. * @return int : ID of the NPC
  1064. */
  1065. public L2NpcTemplate addTalkId(int talkId)
  1066. {
  1067. return addEventId(talkId, Quest.QuestEventType.ON_TALK);
  1068. }
  1069. /**
  1070. * Add this quest to the list of quests that the passed npc will respond to for Spawn Events.<BR><BR>
  1071. * @param talkId : ID of the NPC
  1072. * @return int : ID of the NPC
  1073. */
  1074. public L2NpcTemplate addSpawnId(int npcId)
  1075. {
  1076. return addEventId(npcId, Quest.QuestEventType.ON_SPAWN);
  1077. }
  1078. /**
  1079. * Add this quest to the list of quests that the passed npc will respond to for Skill-See Events.<BR><BR>
  1080. * @param talkId : ID of the NPC
  1081. * @return int : ID of the NPC
  1082. */
  1083. public L2NpcTemplate addSkillSeeId(int npcId)
  1084. {
  1085. return addEventId(npcId, Quest.QuestEventType.ON_SKILL_SEE);
  1086. }
  1087. public L2NpcTemplate addSpellFinishedId(int npcId)
  1088. {
  1089. return addEventId(npcId, Quest.QuestEventType.ON_SPELL_FINISHED);
  1090. }
  1091. /**
  1092. * Add this quest to the list of quests that the passed npc will respond to for Faction Call Events.<BR><BR>
  1093. * @param talkId : ID of the NPC
  1094. * @return int : ID of the NPC
  1095. */
  1096. public L2NpcTemplate addFactionCallId(int npcId)
  1097. {
  1098. return addEventId(npcId, Quest.QuestEventType.ON_FACTION_CALL);
  1099. }
  1100. /**
  1101. * Add this quest to the list of quests that the passed npc will respond to for Character See Events.<BR><BR>
  1102. * @param talkId : ID of the NPC
  1103. * @return int : ID of the NPC
  1104. */
  1105. public L2NpcTemplate addAggroRangeEnterId(int npcId)
  1106. {
  1107. return addEventId(npcId, Quest.QuestEventType.ON_AGGRO_RANGE_ENTER);
  1108. }
  1109. // returns a random party member's L2PcInstance for the passed player's party
  1110. // returns the passed player if he has no party.
  1111. public L2PcInstance getRandomPartyMember(L2PcInstance player)
  1112. {
  1113. // NPE prevention. If the player is null, there is nothing to return
  1114. if (player == null)
  1115. return null;
  1116. if ((player.getParty() == null) || (player.getParty().getPartyMembers().size() == 0))
  1117. return player;
  1118. L2Party party = player.getParty();
  1119. return party.getPartyMembers().get(Rnd.get(party.getPartyMembers().size()));
  1120. }
  1121. /**
  1122. * Auxilary function for party quests.
  1123. * Note: This function is only here because of how commonly it may be used by quest developers.
  1124. * For any variations on this function, the quest script can always handle things on its own
  1125. * @param player: the instance of a player whose party is to be searched
  1126. * @param value: the value of the "cond" variable that must be matched
  1127. * @return L2PcInstance: L2PcInstance for a random party member that matches the specified
  1128. * condition, or null if no match.
  1129. */
  1130. public L2PcInstance getRandomPartyMember(L2PcInstance player, String value)
  1131. {
  1132. return getRandomPartyMember(player, "cond", value);
  1133. }
  1134. /**
  1135. * Auxilary function for party quests.
  1136. * Note: This function is only here because of how commonly it may be used by quest developers.
  1137. * For any variations on this function, the quest script can always handle things on its own
  1138. * @param player: the instance of a player whose party is to be searched
  1139. * @param var/value: a tuple specifying a quest condition that must be satisfied for
  1140. * a party member to be considered.
  1141. * @return L2PcInstance: L2PcInstance for a random party member that matches the specified
  1142. * condition, or null if no match. If the var is null, any random party
  1143. * member is returned (i.e. no condition is applied).
  1144. * The party member must be within 1500 distance from the target of the reference
  1145. * player, or if no target exists, 1500 distance from the player itself.
  1146. */
  1147. public L2PcInstance getRandomPartyMember(L2PcInstance player, String var, String value)
  1148. {
  1149. // if no valid player instance is passed, there is nothing to check...
  1150. if (player == null)
  1151. return null;
  1152. // for null var condition, return any random party member.
  1153. if (var == null)
  1154. return getRandomPartyMember(player);
  1155. // normal cases...if the player is not in a party, check the player's state
  1156. QuestState temp = null;
  1157. L2Party party = player.getParty();
  1158. // if this player is not in a party, just check if this player instance matches the conditions itself
  1159. if ((party == null) || (party.getPartyMembers().size() == 0))
  1160. {
  1161. temp = player.getQuestState(getName());
  1162. if ((temp != null) && (temp.get(var) != null) && ((String) temp.get(var)).equalsIgnoreCase(value))
  1163. return player; // match
  1164. return null; // no match
  1165. }
  1166. // if the player is in a party, gather a list of all matching party members (possibly
  1167. // including this player)
  1168. FastList<L2PcInstance> candidates = new FastList<L2PcInstance>();
  1169. // get the target for enforcing distance limitations.
  1170. L2Object target = player.getTarget();
  1171. if (target == null)
  1172. target = player;
  1173. for (L2PcInstance partyMember : party.getPartyMembers())
  1174. {
  1175. temp = partyMember.getQuestState(getName());
  1176. if ((temp != null) && (temp.get(var) != null) && ((String) temp.get(var)).equalsIgnoreCase(value) && partyMember.isInsideRadius(target, 1500, true, false))
  1177. candidates.add(partyMember);
  1178. }
  1179. // if there was no match, return null...
  1180. if (candidates.size() == 0)
  1181. return null;
  1182. // if a match was found from the party, return one of them at random.
  1183. return candidates.get(Rnd.get(candidates.size()));
  1184. }
  1185. /**
  1186. * Auxilary function for party quests.
  1187. * Note: This function is only here because of how commonly it may be used by quest developers.
  1188. * For any variations on this function, the quest script can always handle things on its own
  1189. * @param player: the instance of a player whose party is to be searched
  1190. * @param state: the state in which the party member's queststate must be in order to be considered.
  1191. * @return L2PcInstance: L2PcInstance for a random party member that matches the specified
  1192. * condition, or null if no match. If the var is null, any random party
  1193. * member is returned (i.e. no condition is applied).
  1194. */
  1195. public L2PcInstance getRandomPartyMemberState(L2PcInstance player, byte state)
  1196. {
  1197. // if no valid player instance is passed, there is nothing to check...
  1198. if (player == null)
  1199. return null;
  1200. // normal cases...if the player is not in a partym check the player's state
  1201. QuestState temp = null;
  1202. L2Party party = player.getParty();
  1203. // if this player is not in a party, just check if this player instance matches the conditions itself
  1204. if ((party == null) || (party.getPartyMembers().size() == 0))
  1205. {
  1206. temp = player.getQuestState(getName());
  1207. if ((temp != null) && (temp.getState() == state))
  1208. return player; // match
  1209. return null; // no match
  1210. }
  1211. // if the player is in a party, gather a list of all matching party members (possibly
  1212. // including this player)
  1213. FastList<L2PcInstance> candidates = new FastList<L2PcInstance>();
  1214. // get the target for enforcing distance limitations.
  1215. L2Object target = player.getTarget();
  1216. if (target == null)
  1217. target = player;
  1218. for (L2PcInstance partyMember : party.getPartyMembers())
  1219. {
  1220. temp = partyMember.getQuestState(getName());
  1221. if ((temp != null) && (temp.getState() == state) && partyMember.isInsideRadius(target, 1500, true, false))
  1222. candidates.add(partyMember);
  1223. }
  1224. // if there was no match, return null...
  1225. if (candidates.size() == 0)
  1226. return null;
  1227. // if a match was found from the party, return one of them at random.
  1228. return candidates.get(Rnd.get(candidates.size()));
  1229. }
  1230. /**
  1231. * Show HTML file to client
  1232. * @param fileName
  1233. * @return String : message sent to client
  1234. */
  1235. public String showHtmlFile(L2PcInstance player, String fileName)
  1236. {
  1237. String questId = getName();
  1238. //Create handler to file linked to the quest
  1239. String directory = getDescr().toLowerCase();
  1240. String content = HtmCache.getInstance().getHtm("data/scripts/" + directory + "/" + questId + "/" + fileName);
  1241. if (content == null)
  1242. content = HtmCache.getInstance().getHtmForce("data/scripts/quests/" + questId + "/" + fileName);
  1243. if (player != null && player.getTarget() != null)
  1244. content = content.replaceAll("%objectId%", String.valueOf(player.getTarget().getObjectId()));
  1245. //Send message to client if message not empty
  1246. if (content != null)
  1247. {
  1248. NpcHtmlMessage npcReply = new NpcHtmlMessage(5);
  1249. npcReply.setHtml(content);
  1250. npcReply.replace("%playername%", player.getName());
  1251. player.sendPacket(npcReply);
  1252. player.sendPacket(ActionFailed.STATIC_PACKET);
  1253. }
  1254. return content;
  1255. }
  1256. // =========================================================
  1257. // QUEST SPAWNS
  1258. // =========================================================
  1259. public class DeSpawnScheduleTimerTask implements Runnable
  1260. {
  1261. L2NpcInstance _npc = null;
  1262. public DeSpawnScheduleTimerTask(L2NpcInstance npc)
  1263. {
  1264. _npc = npc;
  1265. }
  1266. public void run()
  1267. {
  1268. _npc.onDecay();
  1269. }
  1270. }
  1271. // Method - Public
  1272. /**
  1273. * Add a temporary (quest) spawn
  1274. * Return instance of newly spawned npc
  1275. */
  1276. public L2NpcInstance addSpawn(int npcId, L2Character cha)
  1277. {
  1278. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), false, 0, false);
  1279. }
  1280. /**
  1281. * Add a temporary (quest) spawn
  1282. * Return instance of newly spawned npc
  1283. * with summon animation
  1284. */
  1285. public L2NpcInstance addSpawn(int npcId, L2Character cha, boolean isSummonSpawn)
  1286. {
  1287. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), false, 0, isSummonSpawn);
  1288. }
  1289. public L2NpcInstance addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffSet, int despawnDelay)
  1290. {
  1291. return addSpawn(npcId, x, y, z, heading, randomOffSet, despawnDelay, false);
  1292. }
  1293. public L2NpcInstance addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  1294. {
  1295. L2NpcInstance result = null;
  1296. try
  1297. {
  1298. L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId);
  1299. if (template != null)
  1300. {
  1301. // Sometimes, even if the quest script specifies some xyz (for example npc.getX() etc) by the time the code
  1302. // reaches here, xyz have become 0! Also, a questdev might have purposely set xy to 0,0...however,
  1303. // the spawn code is coded such that if x=y=0, it looks into location for the spawn loc! This will NOT work
  1304. // with quest spawns! For both of the above cases, we need a fail-safe spawn. For this, we use the
  1305. // default spawn location, which is at the player's loc.
  1306. if ((x == 0) && (y == 0))
  1307. {
  1308. _log.log(Level.SEVERE, "Failed to adjust bad locks for quest spawn! Spawn aborted!");
  1309. return null;
  1310. }
  1311. if (randomOffset)
  1312. {
  1313. int offset;
  1314. offset = Rnd.get(2); // Get the direction of the offset
  1315. if (offset == 0)
  1316. {
  1317. offset = -1;
  1318. } // make offset negative
  1319. offset *= Rnd.get(50, 100);
  1320. x += offset;
  1321. offset = Rnd.get(2); // Get the direction of the offset
  1322. if (offset == 0)
  1323. {
  1324. offset = -1;
  1325. } // make offset negative
  1326. offset *= Rnd.get(50, 100);
  1327. y += offset;
  1328. }
  1329. L2Spawn spawn = new L2Spawn(template);
  1330. spawn.setHeading(heading);
  1331. spawn.setLocx(x);
  1332. spawn.setLocy(y);
  1333. spawn.setLocz(z + 20);
  1334. spawn.stopRespawn();
  1335. result = spawn.spawnOne(isSummonSpawn);
  1336. if (despawnDelay > 0)
  1337. ThreadPoolManager.getInstance().scheduleGeneral(new DeSpawnScheduleTimerTask(result), despawnDelay);
  1338. return result;
  1339. }
  1340. }
  1341. catch (Exception e1)
  1342. {
  1343. _log.warning("Could not spawn Npc " + npcId);
  1344. }
  1345. return null;
  1346. }
  1347. public int[] getRegisteredItemIds()
  1348. {
  1349. return questItemIds;
  1350. }
  1351. /**
  1352. * @see net.sf.l2j.gameserver.scripting.ManagedScript#getScriptName()
  1353. */
  1354. @Override
  1355. public String getScriptName()
  1356. {
  1357. return this.getName();
  1358. }
  1359. /**
  1360. * @see net.sf.l2j.gameserver.scripting.ManagedScript#setActive(boolean)
  1361. */
  1362. @Override
  1363. public void setActive(boolean status)
  1364. {
  1365. // TODO implement me
  1366. }
  1367. /**
  1368. * @see net.sf.l2j.gameserver.scripting.ManagedScript#reload()
  1369. */
  1370. @Override
  1371. public boolean reload()
  1372. {
  1373. unload();
  1374. return super.reload();
  1375. }
  1376. /**
  1377. * @see net.sf.l2j.gameserver.scripting.ManagedScript#unload()
  1378. */
  1379. @Override
  1380. public boolean unload()
  1381. {
  1382. this.saveGlobalData();
  1383. // cancel all pending timers before reloading.
  1384. // if timers ought to be restarted, the quest can take care of it
  1385. // with its code (example: save global data indicating what timer must
  1386. // be restarted).
  1387. for (FastList<QuestTimer> timers : _allEventTimers.values())
  1388. for (QuestTimer timer : timers)
  1389. timer.cancel();
  1390. _allEventTimers.clear();
  1391. return QuestManager.getInstance().removeQuest(this);
  1392. }
  1393. /**
  1394. * @see net.sf.l2j.gameserver.scripting.ManagedScript#getScriptManager()
  1395. */
  1396. @Override
  1397. public ScriptManager<?> getScriptManager()
  1398. {
  1399. return QuestManager.getInstance();
  1400. }
  1401. }