Quest.java 50 KB

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