Quest.java 47 KB

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