Quest.java 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.quest;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.concurrent.ConcurrentHashMap;
  28. import java.util.concurrent.locks.ReentrantReadWriteLock;
  29. import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
  30. import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
  31. import java.util.logging.Level;
  32. import java.util.logging.Logger;
  33. import com.l2jserver.Config;
  34. import com.l2jserver.L2DatabaseFactory;
  35. import com.l2jserver.gameserver.cache.HtmCache;
  36. import com.l2jserver.gameserver.enums.TrapAction;
  37. import com.l2jserver.gameserver.instancemanager.QuestManager;
  38. import com.l2jserver.gameserver.model.L2Object;
  39. import com.l2jserver.gameserver.model.L2Party;
  40. import com.l2jserver.gameserver.model.actor.L2Attackable;
  41. import com.l2jserver.gameserver.model.actor.L2Character;
  42. import com.l2jserver.gameserver.model.actor.L2Npc;
  43. import com.l2jserver.gameserver.model.actor.L2Summon;
  44. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  45. import com.l2jserver.gameserver.model.actor.instance.L2TrapInstance;
  46. import com.l2jserver.gameserver.model.base.AcquireSkillType;
  47. import com.l2jserver.gameserver.model.events.AbstractScript;
  48. import com.l2jserver.gameserver.model.events.EventType;
  49. import com.l2jserver.gameserver.model.events.listeners.AbstractEventListener;
  50. import com.l2jserver.gameserver.model.events.returns.TerminateReturn;
  51. import com.l2jserver.gameserver.model.interfaces.IIdentifiable;
  52. import com.l2jserver.gameserver.model.items.L2Item;
  53. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  54. import com.l2jserver.gameserver.model.olympiad.CompetitionType;
  55. import com.l2jserver.gameserver.model.olympiad.Participant;
  56. import com.l2jserver.gameserver.model.skills.Skill;
  57. import com.l2jserver.gameserver.model.zone.L2ZoneType;
  58. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  59. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  60. import com.l2jserver.gameserver.network.serverpackets.NpcQuestHtmlMessage;
  61. import com.l2jserver.gameserver.scripting.ScriptManager;
  62. import com.l2jserver.util.Rnd;
  63. import com.l2jserver.util.Util;
  64. /**
  65. * Quest main class.
  66. * @author Luis Arias
  67. */
  68. public class Quest extends AbstractScript implements IIdentifiable
  69. {
  70. public static final Logger _log = Logger.getLogger(Quest.class.getName());
  71. /** Map containing lists of timers from the name of the timer. */
  72. private final Map<String, List<QuestTimer>> _allEventTimers = new ConcurrentHashMap<>();
  73. private final ReentrantReadWriteLock _rwLock = new ReentrantReadWriteLock();
  74. private final WriteLock _writeLock = _rwLock.writeLock();
  75. private final ReadLock _readLock = _rwLock.readLock();
  76. private final int _questId;
  77. private final String _name;
  78. private final String _descr;
  79. private final byte _initialState = State.CREATED;
  80. protected boolean _onEnterWorld = false;
  81. private boolean _isCustom = false;
  82. public int[] questItemIds = null;
  83. private static final String DEFAULT_NO_QUEST_MSG = "<html><body>You are either not on a quest that involves this NPC, or you don't meet this NPC's minimum quest requirements.</body></html>";
  84. private static final String DEFAULT_ALREADY_COMPLETED_MSG = "<html><body>This quest has already been completed.</body></html>";
  85. private static final String QUEST_DELETE_FROM_CHAR_QUERY = "DELETE FROM character_quests WHERE charId=? AND name=?";
  86. private static final String QUEST_DELETE_FROM_CHAR_QUERY_NON_REPEATABLE_QUERY = "DELETE FROM character_quests WHERE charId=? AND name=? AND var!=?";
  87. private static final int RESET_HOUR = 6;
  88. private static final int RESET_MINUTES = 30;
  89. /**
  90. * @return the reset hour for a daily quest, could be overridden on a script.
  91. */
  92. public int getResetHour()
  93. {
  94. return RESET_HOUR;
  95. }
  96. /**
  97. * @return the reset minutes for a daily quest, could be overridden on a script.
  98. */
  99. public int getResetMinutes()
  100. {
  101. return RESET_MINUTES;
  102. }
  103. /**
  104. * The Quest object constructor.<br>
  105. * Constructing a quest also calls the {@code init_LoadGlobalData} convenience method.
  106. * @param questId ID of the quest
  107. * @param name String corresponding to the name of the quest
  108. * @param descr String for the description of the quest
  109. */
  110. public Quest(int questId, String name, String descr)
  111. {
  112. _questId = questId;
  113. _name = name;
  114. _descr = descr;
  115. if (questId > 0)
  116. {
  117. QuestManager.getInstance().addQuest(this);
  118. }
  119. else
  120. {
  121. QuestManager.getInstance().addScript(this);
  122. }
  123. loadGlobalData();
  124. }
  125. /**
  126. * This method is, by default, called by the constructor of all scripts.<br>
  127. * Children of this class can implement this function in order to define what variables to load and what structures to save them in.<br>
  128. * By default, nothing is loaded.
  129. */
  130. protected void loadGlobalData()
  131. {
  132. }
  133. /**
  134. * The function saveGlobalData is, by default, called at shutdown, for all quests, by the QuestManager.<br>
  135. * Children of this class can implement this function in order to convert their structures<br>
  136. * into <var, value> tuples and make calls to save them to the database, if needed.<br>
  137. * By default, nothing is saved.
  138. */
  139. public void saveGlobalData()
  140. {
  141. }
  142. /**
  143. * Gets the quest ID.
  144. * @return the quest ID
  145. */
  146. @Override
  147. public int getId()
  148. {
  149. return _questId;
  150. }
  151. /**
  152. * Add a new quest state of this quest to the database.
  153. * @param player the owner of the newly created quest state
  154. * @return the newly created {@link QuestState} object
  155. */
  156. public QuestState newQuestState(L2PcInstance player)
  157. {
  158. return new QuestState(this, player, _initialState);
  159. }
  160. /**
  161. * Get the specified player's {@link QuestState} object for this quest.<br>
  162. * If the player does not have it and initIfNode is {@code true},<br>
  163. * create a new QuestState object and return it, otherwise return {@code null}.
  164. * @param player the player whose QuestState to get
  165. * @param initIfNone if true and the player does not have a QuestState for this quest,<br>
  166. * create a new QuestState
  167. * @return the QuestState object for this quest or null if it doesn't exist
  168. */
  169. public QuestState getQuestState(L2PcInstance player, boolean initIfNone)
  170. {
  171. final QuestState qs = player.getQuestState(_name);
  172. if ((qs != null) || !initIfNone)
  173. {
  174. return qs;
  175. }
  176. return newQuestState(player);
  177. }
  178. /**
  179. * @return the initial state of the quest
  180. */
  181. public byte getInitialState()
  182. {
  183. return _initialState;
  184. }
  185. /**
  186. * @return the name of the quest
  187. */
  188. public String getName()
  189. {
  190. return _name;
  191. }
  192. /**
  193. * @return the description of the quest
  194. */
  195. public String getDescr()
  196. {
  197. return _descr;
  198. }
  199. /**
  200. * Add a timer to the quest (if it doesn't exist already) and start it.
  201. * @param name the name of the timer (also passed back as "event" in {@link #onAdvEvent(String, L2Npc, L2PcInstance)})
  202. * @param time time in ms for when to fire the timer
  203. * @param npc the npc associated with this timer (can be null)
  204. * @param player the player associated with this timer (can be null)
  205. * @see #startQuestTimer(String, long, L2Npc, L2PcInstance, boolean)
  206. */
  207. public void startQuestTimer(String name, long time, L2Npc npc, L2PcInstance player)
  208. {
  209. startQuestTimer(name, time, npc, player, false);
  210. }
  211. /**
  212. * Add a timer to the quest (if it doesn't exist already) and start it.
  213. * @param name the name of the timer (also passed back as "event" in {@link #onAdvEvent(String, L2Npc, L2PcInstance)})
  214. * @param time time in ms for when to fire the timer
  215. * @param npc the npc associated with this timer (can be null)
  216. * @param player the player associated with this timer (can be null)
  217. * @param repeating indicates whether the timer is repeatable or one-time.<br>
  218. * If {@code true}, the task is repeated every {@code time} milliseconds until explicitly stopped.
  219. */
  220. public void startQuestTimer(String name, long time, L2Npc npc, L2PcInstance player, boolean repeating)
  221. {
  222. final List<QuestTimer> timers = _allEventTimers.computeIfAbsent(name, k -> new ArrayList<>());
  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. _writeLock.lock();
  228. try
  229. {
  230. timers.add(new QuestTimer(this, name, time, npc, player, repeating));
  231. }
  232. finally
  233. {
  234. _writeLock.unlock();
  235. }
  236. }
  237. }
  238. /**
  239. * Get a quest timer that matches the provided name and parameters.
  240. * @param name the name of the quest timer to get
  241. * @param npc the NPC associated with the quest timer to get
  242. * @param player the player associated with the quest timer to get
  243. * @return the quest timer that matches the specified parameters or {@code null} if nothing was found
  244. */
  245. public QuestTimer getQuestTimer(String name, L2Npc npc, L2PcInstance player)
  246. {
  247. final List<QuestTimer> timers = _allEventTimers.get(name);
  248. if (timers != null)
  249. {
  250. _readLock.lock();
  251. try
  252. {
  253. for (QuestTimer timer : timers)
  254. {
  255. if (timer != null)
  256. {
  257. if (timer.isMatch(this, name, npc, player))
  258. {
  259. return timer;
  260. }
  261. }
  262. }
  263. }
  264. finally
  265. {
  266. _readLock.unlock();
  267. }
  268. }
  269. return null;
  270. }
  271. /**
  272. * Cancel all quest timers with the specified name.
  273. * @param name the name of the quest timers to cancel
  274. */
  275. public void cancelQuestTimers(String name)
  276. {
  277. final List<QuestTimer> timers = _allEventTimers.get(name);
  278. if (timers != null)
  279. {
  280. _writeLock.lock();
  281. try
  282. {
  283. for (QuestTimer timer : timers)
  284. {
  285. if (timer != null)
  286. {
  287. timer.cancel();
  288. }
  289. }
  290. timers.clear();
  291. }
  292. finally
  293. {
  294. _writeLock.unlock();
  295. }
  296. }
  297. }
  298. /**
  299. * Cancel the quest timer that matches the specified name and parameters.
  300. * @param name the name of the quest timer to cancel
  301. * @param npc the NPC associated with the quest timer to cancel
  302. * @param player the player associated with the quest timer to cancel
  303. */
  304. public void cancelQuestTimer(String name, L2Npc npc, L2PcInstance player)
  305. {
  306. final QuestTimer timer = getQuestTimer(name, npc, player);
  307. if (timer != null)
  308. {
  309. timer.cancelAndRemove();
  310. }
  311. }
  312. /**
  313. * Remove a quest timer from the list of all timers.<br>
  314. * Note: does not stop the timer itself!
  315. * @param timer the {@link QuestState} object to remove
  316. */
  317. public void removeQuestTimer(QuestTimer timer)
  318. {
  319. if (timer != null)
  320. {
  321. final List<QuestTimer> timers = _allEventTimers.get(timer.getName());
  322. if (timers != null)
  323. {
  324. _writeLock.lock();
  325. try
  326. {
  327. timers.remove(timer);
  328. }
  329. finally
  330. {
  331. _writeLock.unlock();
  332. }
  333. }
  334. }
  335. }
  336. public Map<String, List<QuestTimer>> getQuestTimers()
  337. {
  338. return _allEventTimers;
  339. }
  340. // These are methods to call within the core to call the quest events.
  341. /**
  342. * @param npc the NPC that was attacked
  343. * @param attacker the attacking player
  344. * @param damage the damage dealt to the NPC by the player
  345. * @param isSummon if {@code true}, the attack was actually made by the player's summon
  346. * @param skill the skill used to attack the NPC (can be null)
  347. */
  348. public final void notifyAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isSummon, Skill skill)
  349. {
  350. String res = null;
  351. try
  352. {
  353. res = onAttack(npc, attacker, damage, isSummon, skill);
  354. }
  355. catch (Exception e)
  356. {
  357. showError(attacker, e);
  358. return;
  359. }
  360. showResult(attacker, res);
  361. }
  362. /**
  363. * @param killer the character that killed the {@code victim}
  364. * @param victim the character that was killed by the {@code killer}
  365. * @param qs the quest state object of the player to be notified of this event
  366. */
  367. public final void notifyDeath(L2Character killer, L2Character victim, QuestState qs)
  368. {
  369. String res = null;
  370. try
  371. {
  372. res = onDeath(killer, victim, qs);
  373. }
  374. catch (Exception e)
  375. {
  376. showError(qs.getPlayer(), e);
  377. }
  378. showResult(qs.getPlayer(), res);
  379. }
  380. /**
  381. * @param item
  382. * @param player
  383. */
  384. public final void notifyItemUse(L2Item item, L2PcInstance player)
  385. {
  386. String res = null;
  387. try
  388. {
  389. res = onItemUse(item, player);
  390. }
  391. catch (Exception e)
  392. {
  393. showError(player, e);
  394. }
  395. showResult(player, res);
  396. }
  397. /**
  398. * @param instance
  399. * @param player
  400. * @param skill
  401. */
  402. public final void notifySpellFinished(L2Npc instance, L2PcInstance player, Skill skill)
  403. {
  404. String res = null;
  405. try
  406. {
  407. res = onSpellFinished(instance, player, skill);
  408. }
  409. catch (Exception e)
  410. {
  411. showError(player, e);
  412. }
  413. showResult(player, res);
  414. }
  415. /**
  416. * Notify quest script when something happens with a trap.
  417. * @param trap the trap instance which triggers the notification
  418. * @param trigger the character which makes effect on the trap
  419. * @param action 0: trap casting its skill. 1: trigger detects the trap. 2: trigger removes the trap
  420. */
  421. public final void notifyTrapAction(L2TrapInstance trap, L2Character trigger, TrapAction action)
  422. {
  423. String res = null;
  424. try
  425. {
  426. res = onTrapAction(trap, trigger, action);
  427. }
  428. catch (Exception e)
  429. {
  430. if (trigger.getActingPlayer() != null)
  431. {
  432. showError(trigger.getActingPlayer(), e);
  433. }
  434. _log.log(Level.WARNING, "Exception on onTrapAction() in notifyTrapAction(): " + e.getMessage(), e);
  435. return;
  436. }
  437. if (trigger.getActingPlayer() != null)
  438. {
  439. showResult(trigger.getActingPlayer(), res);
  440. }
  441. }
  442. /**
  443. * @param npc the spawned NPC
  444. */
  445. public final void notifySpawn(L2Npc npc)
  446. {
  447. try
  448. {
  449. onSpawn(npc);
  450. }
  451. catch (Exception e)
  452. {
  453. _log.log(Level.WARNING, "Exception on onSpawn() in notifySpawn(): " + e.getMessage(), e);
  454. }
  455. }
  456. /**
  457. * @param event
  458. * @param npc
  459. * @param player
  460. * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
  461. */
  462. public final boolean notifyEvent(String event, L2Npc npc, L2PcInstance player)
  463. {
  464. String res = null;
  465. try
  466. {
  467. res = onAdvEvent(event, npc, player);
  468. }
  469. catch (Exception e)
  470. {
  471. return showError(player, e);
  472. }
  473. return showResult(player, res, npc);
  474. }
  475. /**
  476. * @param player the player entering the world
  477. */
  478. public final void notifyEnterWorld(L2PcInstance player)
  479. {
  480. String res = null;
  481. try
  482. {
  483. res = onEnterWorld(player);
  484. }
  485. catch (Exception e)
  486. {
  487. showError(player, e);
  488. }
  489. showResult(player, res);
  490. }
  491. /**
  492. * @param npc
  493. * @param killer
  494. * @param isSummon
  495. */
  496. public final void notifyKill(L2Npc npc, L2PcInstance killer, boolean isSummon)
  497. {
  498. String res = null;
  499. try
  500. {
  501. res = onKill(npc, killer, isSummon);
  502. }
  503. catch (Exception e)
  504. {
  505. showError(killer, e);
  506. }
  507. showResult(killer, res);
  508. }
  509. /**
  510. * @param npc
  511. * @param activeChar
  512. * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
  513. */
  514. public final boolean notifyTalk(L2Npc npc, L2PcInstance activeChar)
  515. {
  516. String res = null;
  517. try
  518. {
  519. res = onTalk(npc, activeChar);
  520. }
  521. catch (Exception e)
  522. {
  523. return showError(activeChar, e);
  524. }
  525. activeChar.setLastQuestNpcObject(npc.getObjectId());
  526. return showResult(activeChar, res, npc);
  527. }
  528. /**
  529. * Override the default NPC dialogs when a quest defines this for the given NPC.<br>
  530. * Note: If the default html for this npc needs to be shown, onFirstTalk should call npc.showChatWindow(player) and then return null.
  531. * @param npc the NPC whose dialogs to override
  532. * @param player the player talking to the NPC
  533. */
  534. public final void notifyFirstTalk(L2Npc npc, L2PcInstance player)
  535. {
  536. String res = null;
  537. try
  538. {
  539. res = onFirstTalk(npc, player);
  540. }
  541. catch (Exception e)
  542. {
  543. showError(player, e);
  544. }
  545. showResult(player, res, npc);
  546. }
  547. /**
  548. * Notify the quest engine that an skill has been acquired.
  549. * @param npc the NPC
  550. * @param player the player
  551. * @param skill the skill
  552. * @param type the skill learn type
  553. */
  554. public final void notifyAcquireSkill(L2Npc npc, L2PcInstance player, Skill skill, AcquireSkillType type)
  555. {
  556. String res = null;
  557. try
  558. {
  559. res = onAcquireSkill(npc, player, skill, type);
  560. }
  561. catch (Exception e)
  562. {
  563. showError(player, e);
  564. }
  565. showResult(player, res);
  566. }
  567. /**
  568. * @param item
  569. * @param player
  570. * @return
  571. */
  572. public final boolean notifyItemTalk(L2ItemInstance item, L2PcInstance player)
  573. {
  574. String res = null;
  575. try
  576. {
  577. res = onItemTalk(item, player);
  578. if (res != null)
  579. {
  580. if (res.equalsIgnoreCase("true"))
  581. {
  582. return true;
  583. }
  584. else if (res.equalsIgnoreCase("false"))
  585. {
  586. return false;
  587. }
  588. }
  589. }
  590. catch (Exception e)
  591. {
  592. return showError(player, e);
  593. }
  594. return showResult(player, res);
  595. }
  596. /**
  597. * @param item
  598. * @param player
  599. * @return
  600. */
  601. public String onItemTalk(L2ItemInstance item, L2PcInstance player)
  602. {
  603. return null;
  604. }
  605. /**
  606. * @param item
  607. * @param player
  608. * @param event
  609. * @return
  610. */
  611. public final boolean notifyItemEvent(L2ItemInstance item, L2PcInstance player, String event)
  612. {
  613. String res = null;
  614. try
  615. {
  616. res = onItemEvent(item, player, event);
  617. if (res != null)
  618. {
  619. if (res.equalsIgnoreCase("true"))
  620. {
  621. return true;
  622. }
  623. else if (res.equalsIgnoreCase("false"))
  624. {
  625. return false;
  626. }
  627. }
  628. }
  629. catch (Exception e)
  630. {
  631. return showError(player, e);
  632. }
  633. return showResult(player, res);
  634. }
  635. /**
  636. * @param npc
  637. * @param caster
  638. * @param skill
  639. * @param targets
  640. * @param isSummon
  641. */
  642. public final void notifySkillSee(L2Npc npc, L2PcInstance caster, Skill skill, L2Object[] targets, boolean isSummon)
  643. {
  644. String res = null;
  645. try
  646. {
  647. res = onSkillSee(npc, caster, skill, targets, isSummon);
  648. }
  649. catch (Exception e)
  650. {
  651. showError(caster, e);
  652. }
  653. showResult(caster, res);
  654. }
  655. /**
  656. * @param npc
  657. * @param caller
  658. * @param attacker
  659. * @param isSummon
  660. */
  661. public final void notifyFactionCall(L2Npc npc, L2Npc caller, L2PcInstance attacker, boolean isSummon)
  662. {
  663. String res = null;
  664. try
  665. {
  666. res = onFactionCall(npc, caller, attacker, isSummon);
  667. }
  668. catch (Exception e)
  669. {
  670. showError(attacker, e);
  671. }
  672. showResult(attacker, res);
  673. }
  674. /**
  675. * @param npc
  676. * @param player
  677. * @param isSummon
  678. */
  679. public final void notifyAggroRangeEnter(L2Npc npc, L2PcInstance player, boolean isSummon)
  680. {
  681. String res = null;
  682. try
  683. {
  684. res = onAggroRangeEnter(npc, player, isSummon);
  685. }
  686. catch (Exception e)
  687. {
  688. showError(player, e);
  689. }
  690. showResult(player, res);
  691. }
  692. /**
  693. * @param npc the NPC that sees the creature
  694. * @param creature the creature seen by the NPC
  695. * @param isSummon
  696. */
  697. public final void notifySeeCreature(L2Npc npc, L2Character creature, boolean isSummon)
  698. {
  699. L2PcInstance player = null;
  700. if (isSummon || creature.isPlayer())
  701. {
  702. player = creature.getActingPlayer();
  703. }
  704. String res = null;
  705. try
  706. {
  707. res = onSeeCreature(npc, creature, isSummon);
  708. }
  709. catch (Exception e)
  710. {
  711. if (player != null)
  712. {
  713. showError(player, e);
  714. }
  715. }
  716. if (player != null)
  717. {
  718. showResult(player, res);
  719. }
  720. }
  721. /**
  722. * @param eventName - name of event
  723. * @param sender - NPC, who sent event
  724. * @param receiver - NPC, who received event
  725. * @param reference - L2Object to pass, if needed
  726. */
  727. public final void notifyEventReceived(String eventName, L2Npc sender, L2Npc receiver, L2Object reference)
  728. {
  729. try
  730. {
  731. onEventReceived(eventName, sender, receiver, reference);
  732. }
  733. catch (Exception e)
  734. {
  735. _log.log(Level.WARNING, "Exception on onEventReceived() in notifyEventReceived(): " + e.getMessage(), e);
  736. }
  737. }
  738. /**
  739. * @param character
  740. * @param zone
  741. */
  742. public final void notifyEnterZone(L2Character character, L2ZoneType zone)
  743. {
  744. L2PcInstance player = character.getActingPlayer();
  745. String res = null;
  746. try
  747. {
  748. res = onEnterZone(character, zone);
  749. }
  750. catch (Exception e)
  751. {
  752. if (player != null)
  753. {
  754. showError(player, e);
  755. }
  756. }
  757. if (player != null)
  758. {
  759. showResult(player, res);
  760. }
  761. }
  762. /**
  763. * @param character
  764. * @param zone
  765. */
  766. public final void notifyExitZone(L2Character character, L2ZoneType zone)
  767. {
  768. L2PcInstance player = character.getActingPlayer();
  769. String res = null;
  770. try
  771. {
  772. res = onExitZone(character, zone);
  773. }
  774. catch (Exception e)
  775. {
  776. if (player != null)
  777. {
  778. showError(player, e);
  779. }
  780. }
  781. if (player != null)
  782. {
  783. showResult(player, res);
  784. }
  785. }
  786. /**
  787. * @param winner
  788. * @param looser
  789. * @param type
  790. */
  791. public final void notifyOlympiadMatch(Participant winner, Participant looser, CompetitionType type)
  792. {
  793. try
  794. {
  795. onOlympiadMatchFinish(winner, looser, type);
  796. }
  797. catch (Exception e)
  798. {
  799. _log.log(Level.WARNING, "Execution on onOlympiadMatchFinish() in notifyOlympiadMatch(): " + e.getMessage(), e);
  800. }
  801. }
  802. /**
  803. * @param npc
  804. */
  805. public final void notifyMoveFinished(L2Npc npc)
  806. {
  807. try
  808. {
  809. onMoveFinished(npc);
  810. }
  811. catch (Exception e)
  812. {
  813. _log.log(Level.WARNING, "Exception on onMoveFinished() in notifyMoveFinished(): " + e.getMessage(), e);
  814. }
  815. }
  816. /**
  817. * @param npc
  818. */
  819. public final void notifyNodeArrived(L2Npc npc)
  820. {
  821. try
  822. {
  823. onNodeArrived(npc);
  824. }
  825. catch (Exception e)
  826. {
  827. _log.log(Level.WARNING, "Exception on onNodeArrived() in notifyNodeArrived(): " + e.getMessage(), e);
  828. }
  829. }
  830. /**
  831. * @param npc
  832. */
  833. public final void notifyRouteFinished(L2Npc npc)
  834. {
  835. try
  836. {
  837. onRouteFinished(npc);
  838. }
  839. catch (Exception e)
  840. {
  841. _log.log(Level.WARNING, "Exception on onRouteFinished() in notifyRouteFinished(): " + e.getMessage(), e);
  842. }
  843. }
  844. /**
  845. * @param npc
  846. * @param player
  847. * @return {@code true} if player can see this npc, {@code false} otherwise.
  848. */
  849. public final boolean notifyOnCanSeeMe(L2Npc npc, L2PcInstance player)
  850. {
  851. try
  852. {
  853. return onCanSeeMe(npc, player);
  854. }
  855. catch (Exception e)
  856. {
  857. _log.log(Level.WARNING, "Exception on onCanSeeMe() in notifyOnCanSeeMe(): " + e.getMessage(), e);
  858. }
  859. return false;
  860. }
  861. // These are methods that java calls to invoke scripts.
  862. /**
  863. * This function is called in place of {@link #onAttack(L2Npc, L2PcInstance, int, boolean, Skill)} if the former is not implemented.<br>
  864. * If a script contains both onAttack(..) implementations, then this method will never be called unless the script's {@link #onAttack(L2Npc, L2PcInstance, int, boolean, Skill)} explicitly calls this method.
  865. * @param npc this parameter contains a reference to the exact instance of the NPC that got attacked the NPC.
  866. * @param attacker this parameter contains a reference to the exact instance of the player who attacked.
  867. * @param damage this parameter represents the total damage that this attack has inflicted to the NPC.
  868. * @param isSummon this parameter if it's {@code false} it denotes that the attacker was indeed the player, else it specifies that the damage was actually dealt by the player's pet.
  869. * @return
  870. */
  871. public String onAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isSummon)
  872. {
  873. return null;
  874. }
  875. /**
  876. * This function is called whenever a player attacks an NPC that is registered for the quest.<br>
  877. * If is not overridden by a subclass, then default to the returned value of the simpler (and older) {@link #onAttack(L2Npc, L2PcInstance, int, boolean)} override.<br>
  878. * @param npc this parameter contains a reference to the exact instance of the NPC that got attacked.
  879. * @param attacker this parameter contains a reference to the exact instance of the player who attacked the NPC.
  880. * @param damage this parameter represents the total damage that this attack has inflicted to the NPC.
  881. * @param isSummon this parameter if it's {@code false} it denotes that the attacker was indeed the player, else it specifies that the damage was actually dealt by the player's summon
  882. * @param skill parameter is the skill that player used to attack NPC.
  883. * @return
  884. */
  885. public String onAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isSummon, Skill skill)
  886. {
  887. return onAttack(npc, attacker, damage, isSummon);
  888. }
  889. /**
  890. * This function is called whenever an <b>exact instance</b> of a character who was previously registered for this event dies.<br>
  891. * The registration for {@link #onDeath(L2Character, L2Character, QuestState)} events <b>is not</b> done via the quest itself, but it is instead handled by the QuestState of a particular player.
  892. * @param killer this parameter contains a reference to the exact instance of the NPC that <b>killed</b> the character.
  893. * @param victim this parameter contains a reference to the exact instance of the character that got killed.
  894. * @param qs this parameter contains a reference to the QuestState of whomever was interested (waiting) for this kill.
  895. * @return
  896. */
  897. public String onDeath(L2Character killer, L2Character victim, QuestState qs)
  898. {
  899. return onAdvEvent("", ((killer instanceof L2Npc) ? ((L2Npc) killer) : null), qs.getPlayer());
  900. }
  901. /**
  902. * This function is called whenever a player clicks on a link in a quest dialog and whenever a timer fires.<br>
  903. * If is not overridden by a subclass, then default to the returned value of the simpler (and older) {@link #onEvent(String, QuestState)} override.<br>
  904. * If the player has a quest state, use it as parameter in the next call, otherwise return null.
  905. * @param event this parameter contains a string identifier for the event.<br>
  906. * Generally, this string is passed directly via the link.<br>
  907. * For example:<br>
  908. * <code>
  909. * &lt;a action="bypass -h Quest 626_ADarkTwilight 31517-01.htm"&gt;hello&lt;/a&gt;
  910. * </code><br>
  911. * The above link sets the event variable to "31517-01.htm" for the quest 626_ADarkTwilight.<br>
  912. * In the case of timers, this will be the name of the timer.<br>
  913. * This parameter serves as a sort of identifier.
  914. * @param npc this parameter contains a reference to the instance of NPC associated with this event.<br>
  915. * This may be the NPC registered in a timer, or the NPC with whom a player is speaking, etc.<br>
  916. * This parameter may be {@code null} in certain circumstances.
  917. * @param player this parameter contains a reference to the player participating in this function.<br>
  918. * It may be the player speaking to the NPC, or the player who caused a timer to start (and owns that timer).<br>
  919. * This parameter may be {@code null} in certain circumstances.
  920. * @return the text returned by the event (may be {@code null}, a filename or just text)
  921. */
  922. public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  923. {
  924. if (player != null)
  925. {
  926. final QuestState qs = player.getQuestState(getName());
  927. if (qs != null)
  928. {
  929. return onEvent(event, qs);
  930. }
  931. }
  932. return null;
  933. }
  934. /**
  935. * This function is called in place of {@link #onAdvEvent(String, L2Npc, L2PcInstance)} if the former is not implemented.<br>
  936. * If a script contains both {@link #onAdvEvent(String, L2Npc, L2PcInstance)} and this implementation, then this method will never be called unless the script's {@link #onAdvEvent(String, L2Npc, L2PcInstance)} explicitly calls this method.
  937. * @param event this parameter contains a string identifier for the event.<br>
  938. * Generally, this string is passed directly via the link.<br>
  939. * For example:<br>
  940. * <code>
  941. * &lt;a action="bypass -h Quest 626_ADarkTwilight 31517-01.htm"&gt;hello&lt;/a&gt;
  942. * </code><br>
  943. * The above link sets the event variable to "31517-01.htm" for the quest 626_ADarkTwilight.<br>
  944. * In the case of timers, this will be the name of the timer.<br>
  945. * This parameter serves as a sort of identifier.
  946. * @param qs this parameter contains a reference to the quest state of the player who used the link or started the timer.
  947. * @return the text returned by the event (may be {@code null}, a filename or just text)
  948. */
  949. public String onEvent(String event, QuestState qs)
  950. {
  951. return null;
  952. }
  953. /**
  954. * This function is called whenever a player kills a NPC that is registered for the quest.
  955. * @param npc this parameter contains a reference to the exact instance of the NPC that got killed.
  956. * @param killer this parameter contains a reference to the exact instance of the player who killed the NPC.
  957. * @param isSummon this parameter if it's {@code false} it denotes that the attacker was indeed the player, else it specifies that the killer was the player's pet.
  958. * @return the text returned by the event (may be {@code null}, a filename or just text)
  959. */
  960. public String onKill(L2Npc npc, L2PcInstance killer, boolean isSummon)
  961. {
  962. return null;
  963. }
  964. /**
  965. * This function is called whenever a player clicks to the "Quest" link of an NPC that is registered for the quest.
  966. * @param npc this parameter contains a reference to the exact instance of the NPC that the player is talking with.
  967. * @param talker this parameter contains a reference to the exact instance of the player who is talking to the NPC.
  968. * @return the text returned by the event (may be {@code null}, a filename or just text)
  969. */
  970. public String onTalk(L2Npc npc, L2PcInstance talker)
  971. {
  972. return null;
  973. }
  974. /**
  975. * This function is called whenever a player talks to an NPC that is registered for the quest.<br>
  976. * That is, it is triggered from the very first click on the NPC, not via another dialog.<br>
  977. * <b>Note 1:</b><br>
  978. * Each NPC can be registered to at most one quest for triggering this function.<br>
  979. * In other words, the same one NPC cannot respond to an "onFirstTalk" request from two different quests.<br>
  980. * Attempting to register an NPC in two different quests for this function will result in one of the two registration being ignored.<br>
  981. * <b>Note 2:</b><br>
  982. * Since a Quest link isn't clicked in order to reach this, a quest state can be invalid within this function.<br>
  983. * The coder of the script may need to create a new quest state (if necessary).<br>
  984. * <b>Note 3:</b><br>
  985. * The returned value of onFirstTalk replaces the default HTML that would have otherwise been loaded from a sub-folder of DatapackRoot/game/data/html/.<br>
  986. * If you wish to show the default HTML, within onFirstTalk do npc.showChatWindow(player) and then return ""<br>
  987. * @param npc this parameter contains a reference to the exact instance of the NPC that the player is talking with.
  988. * @param player this parameter contains a reference to the exact instance of the player who is talking to the NPC.
  989. * @return the text returned by the event (may be {@code null}, a filename or just text)
  990. * @since <a href="http://trac.l2jserver.com/changeset/771">Jython AI support for "onFirstTalk"</a>
  991. */
  992. public String onFirstTalk(L2Npc npc, L2PcInstance player)
  993. {
  994. return null;
  995. }
  996. /**
  997. * @param item
  998. * @param player
  999. * @param event
  1000. * @return
  1001. */
  1002. public String onItemEvent(L2ItemInstance item, L2PcInstance player, String event)
  1003. {
  1004. return null;
  1005. }
  1006. /**
  1007. * This function is called whenever a player request a skill list.<br>
  1008. * TODO: Re-implement, since Skill Trees rework it's support was removed.
  1009. * @param npc this parameter contains a reference to the exact instance of the NPC that the player requested the skill list.
  1010. * @param player this parameter contains a reference to the exact instance of the player who requested the skill list.
  1011. * @return
  1012. */
  1013. public String onAcquireSkillList(L2Npc npc, L2PcInstance player)
  1014. {
  1015. return null;
  1016. }
  1017. /**
  1018. * This function is called whenever a player request a skill info.
  1019. * @param npc this parameter contains a reference to the exact instance of the NPC that the player requested the skill info.
  1020. * @param player this parameter contains a reference to the exact instance of the player who requested the skill info.
  1021. * @param skill this parameter contains a reference to the skill that the player requested its info.
  1022. * @return
  1023. */
  1024. public String onAcquireSkillInfo(L2Npc npc, L2PcInstance player, Skill skill)
  1025. {
  1026. return null;
  1027. }
  1028. /**
  1029. * This function is called whenever a player acquire a skill.<br>
  1030. * TODO: Re-implement, since Skill Trees rework it's support was removed.
  1031. * @param npc this parameter contains a reference to the exact instance of the NPC that the player requested the skill.
  1032. * @param player this parameter contains a reference to the exact instance of the player who requested the skill.
  1033. * @param skill this parameter contains a reference to the skill that the player requested.
  1034. * @param type the skill learn type
  1035. * @return
  1036. */
  1037. public String onAcquireSkill(L2Npc npc, L2PcInstance player, Skill skill, AcquireSkillType type)
  1038. {
  1039. return null;
  1040. }
  1041. /**
  1042. * This function is called whenever a player uses a quest item that has a quest events list.<br>
  1043. * TODO: complete this documentation and unhardcode it to work with all item uses not with those listed.
  1044. * @param item the quest item that the player used
  1045. * @param player the player who used the item
  1046. * @return
  1047. */
  1048. public String onItemUse(L2Item item, L2PcInstance player)
  1049. {
  1050. return null;
  1051. }
  1052. /**
  1053. * This function is called whenever a player casts a skill near a registered NPC (1000 distance).<br>
  1054. * <b>Note:</b><br>
  1055. * If a skill does damage, both onSkillSee(..) and onAttack(..) will be triggered for the damaged NPC!<br>
  1056. * However, only onSkillSee(..) will be triggered if the skill does no damage,<br>
  1057. * or if it damages an NPC who has no onAttack(..) registration while near another NPC who has an onSkillSee registration.<br>
  1058. * TODO: confirm if the distance is 1000 and unhardcode.
  1059. * @param npc the NPC that saw the skill
  1060. * @param caster the player who cast the skill
  1061. * @param skill the actual skill that was used
  1062. * @param targets an array of all objects (can be any type of object, including mobs and players) that were affected by the skill
  1063. * @param isSummon if {@code true}, the skill was actually cast by the player's summon, not the player himself
  1064. * @return
  1065. */
  1066. public String onSkillSee(L2Npc npc, L2PcInstance caster, Skill skill, L2Object[] targets, boolean isSummon)
  1067. {
  1068. return null;
  1069. }
  1070. /**
  1071. * This function is called whenever an NPC finishes casting a skill.
  1072. * @param npc the NPC that casted the skill.
  1073. * @param player the player who is the target of the skill. Can be {@code null}.
  1074. * @param skill the actual skill that was used by the NPC.
  1075. * @return
  1076. */
  1077. public String onSpellFinished(L2Npc npc, L2PcInstance player, Skill skill)
  1078. {
  1079. return null;
  1080. }
  1081. /**
  1082. * This function is called whenever a trap action is performed.
  1083. * @param trap this parameter contains a reference to the exact instance of the trap that was activated.
  1084. * @param trigger this parameter contains a reference to the exact instance of the character that triggered the action.
  1085. * @param action this parameter contains a reference to the action that was triggered.
  1086. * @return
  1087. */
  1088. public String onTrapAction(L2TrapInstance trap, L2Character trigger, TrapAction action)
  1089. {
  1090. return null;
  1091. }
  1092. /**
  1093. * This function is called whenever an NPC spawns or re-spawns and passes a reference to the newly (re)spawned NPC.<br>
  1094. * Currently the only function that has no reference to a player.<br>
  1095. * It is useful for initializations, starting quest timers, displaying chat (NpcSay), and more.
  1096. * @param npc this parameter contains a reference to the exact instance of the NPC who just (re)spawned.
  1097. * @return
  1098. */
  1099. public String onSpawn(L2Npc npc)
  1100. {
  1101. return null;
  1102. }
  1103. /**
  1104. * This function is called whenever an NPC is called by another NPC in the same faction.
  1105. * @param npc this parameter contains a reference to the exact instance of the NPC who is being asked for help.
  1106. * @param caller this parameter contains a reference to the exact instance of the NPC who is asking for help.<br>
  1107. * @param attacker this parameter contains a reference to the exact instance of the player who attacked.
  1108. * @param isSummon this parameter if it's {@code false} it denotes that the attacker was indeed the player, else it specifies that the attacker was the player's summon.
  1109. * @return
  1110. */
  1111. public String onFactionCall(L2Npc npc, L2Npc caller, L2PcInstance attacker, boolean isSummon)
  1112. {
  1113. return null;
  1114. }
  1115. /**
  1116. * This function is called whenever a player enters an NPC aggression range.
  1117. * @param npc this parameter contains a reference to the exact instance of the NPC whose aggression range is being transgressed.
  1118. * @param player this parameter contains a reference to the exact instance of the player who is entering the NPC's aggression range.
  1119. * @param isSummon this parameter if it's {@code false} it denotes that the character that entered the aggression range was indeed the player, else it specifies that the character was the player's summon.
  1120. * @return
  1121. */
  1122. public String onAggroRangeEnter(L2Npc npc, L2PcInstance player, boolean isSummon)
  1123. {
  1124. return null;
  1125. }
  1126. /**
  1127. * This function is called whenever a NPC "sees" a creature.
  1128. * @param npc the NPC who sees the creature
  1129. * @param creature the creature seen by the NPC
  1130. * @param isSummon this parameter if it's {@code false} it denotes that the character seen by the NPC was indeed the player, else it specifies that the character was the player's summon
  1131. * @return
  1132. */
  1133. public String onSeeCreature(L2Npc npc, L2Character creature, boolean isSummon)
  1134. {
  1135. return null;
  1136. }
  1137. /**
  1138. * This function is called whenever a player enters the game.
  1139. * @param player this parameter contains a reference to the exact instance of the player who is entering to the world.
  1140. * @return
  1141. */
  1142. public String onEnterWorld(L2PcInstance player)
  1143. {
  1144. return null;
  1145. }
  1146. /**
  1147. * This function is called whenever a character enters a registered zone.
  1148. * @param character this parameter contains a reference to the exact instance of the character who is entering the zone.
  1149. * @param zone this parameter contains a reference to the zone.
  1150. * @return
  1151. */
  1152. public String onEnterZone(L2Character character, L2ZoneType zone)
  1153. {
  1154. return null;
  1155. }
  1156. /**
  1157. * This function is called whenever a character exits a registered zone.
  1158. * @param character this parameter contains a reference to the exact instance of the character who is exiting the zone.
  1159. * @param zone this parameter contains a reference to the zone.
  1160. * @return
  1161. */
  1162. public String onExitZone(L2Character character, L2ZoneType zone)
  1163. {
  1164. return null;
  1165. }
  1166. /**
  1167. * @param eventName - name of event
  1168. * @param sender - NPC, who sent event
  1169. * @param receiver - NPC, who received event
  1170. * @param reference - L2Object to pass, if needed
  1171. * @return
  1172. */
  1173. public String onEventReceived(String eventName, L2Npc sender, L2Npc receiver, L2Object reference)
  1174. {
  1175. return null;
  1176. }
  1177. /**
  1178. * This function is called whenever a player wins an Olympiad Game.
  1179. * @param winner in this match.
  1180. * @param looser in this match.
  1181. * @param type the competition type.
  1182. */
  1183. public void onOlympiadMatchFinish(Participant winner, Participant looser, CompetitionType type)
  1184. {
  1185. }
  1186. /**
  1187. * This function is called whenever a player looses an Olympiad Game.
  1188. * @param loser this parameter contains a reference to the exact instance of the player who lose the competition.
  1189. * @param type this parameter contains a reference to the competition type.
  1190. */
  1191. public void onOlympiadLose(L2PcInstance loser, CompetitionType type)
  1192. {
  1193. }
  1194. /**
  1195. * This function is called whenever a NPC finishes moving
  1196. * @param npc registered NPC
  1197. */
  1198. public void onMoveFinished(L2Npc npc)
  1199. {
  1200. }
  1201. /**
  1202. * This function is called whenever a walker NPC (controlled by WalkingManager) arrive a walking node
  1203. * @param npc registered NPC
  1204. */
  1205. public void onNodeArrived(L2Npc npc)
  1206. {
  1207. }
  1208. /**
  1209. * This function is called whenever a walker NPC (controlled by WalkingManager) arrive to last node
  1210. * @param npc registered NPC
  1211. */
  1212. public void onRouteFinished(L2Npc npc)
  1213. {
  1214. }
  1215. /**
  1216. * @param mob
  1217. * @param player
  1218. * @param isSummon
  1219. * @return {@code true} if npc can hate the playable, {@code false} otherwise.
  1220. */
  1221. public boolean onNpcHate(L2Attackable mob, L2PcInstance player, boolean isSummon)
  1222. {
  1223. return true;
  1224. }
  1225. /**
  1226. * @param summon
  1227. */
  1228. public void onSummonSpawn(L2Summon summon)
  1229. {
  1230. }
  1231. /**
  1232. * @param summon
  1233. */
  1234. public void onSummonTalk(L2Summon summon)
  1235. {
  1236. }
  1237. /**
  1238. * @param npc
  1239. * @param player
  1240. * @return {@code true} if player can see this npc, {@code false} otherwise.
  1241. */
  1242. public boolean onCanSeeMe(L2Npc npc, L2PcInstance player)
  1243. {
  1244. return false;
  1245. }
  1246. /**
  1247. * Show an error message to the specified player.
  1248. * @param player the player to whom to send the error (must be a GM)
  1249. * @param t the {@link Throwable} to get the message/stacktrace from
  1250. * @return {@code false}
  1251. */
  1252. public boolean showError(L2PcInstance player, Throwable t)
  1253. {
  1254. _log.log(Level.WARNING, getScriptFile().getAbsolutePath(), t);
  1255. if (t.getMessage() == null)
  1256. {
  1257. _log.warning(getClass().getSimpleName() + ": " + t.getMessage());
  1258. }
  1259. if ((player != null) && player.getAccessLevel().isGm())
  1260. {
  1261. String res = "<html><body><title>Script error</title>" + Util.getStackTrace(t) + "</body></html>";
  1262. return showResult(player, res);
  1263. }
  1264. return false;
  1265. }
  1266. /**
  1267. * @param player the player to whom to show the result
  1268. * @param res the message to show to the player
  1269. * @return {@code false} if the message was sent, {@code true} otherwise
  1270. * @see #showResult(L2PcInstance, String, L2Npc)
  1271. */
  1272. public boolean showResult(L2PcInstance player, String res)
  1273. {
  1274. return showResult(player, res, null);
  1275. }
  1276. /**
  1277. * Show a message to the specified player.<br>
  1278. * <u><i>Concept:</i></u><br>
  1279. * Three cases are managed according to the value of the {@code res} parameter:<br>
  1280. * <ul>
  1281. * <li><u>{@code res} ends with ".htm" or ".html":</u> the contents of the specified HTML file are shown in a dialog window</li>
  1282. * <li><u>{@code res} starts with "&lt;html&gt;":</u> the contents of the parameter are shown in a dialog window</li>
  1283. * <li><u>all other cases :</u> the text contained in the parameter is shown in chat</li>
  1284. * </ul>
  1285. * @param player the player to whom to show the result
  1286. * @param npc npc to show the result for
  1287. * @param res the message to show to the player
  1288. * @return {@code false} if the message was sent, {@code true} otherwise
  1289. */
  1290. public boolean showResult(L2PcInstance player, String res, L2Npc npc)
  1291. {
  1292. if ((res == null) || res.isEmpty() || (player == null))
  1293. {
  1294. return true;
  1295. }
  1296. if (res.endsWith(".htm") || res.endsWith(".html"))
  1297. {
  1298. showHtmlFile(player, res, npc);
  1299. }
  1300. else if (res.startsWith("<html>"))
  1301. {
  1302. final NpcHtmlMessage npcReply = new NpcHtmlMessage(npc != null ? npc.getObjectId() : 0, res);
  1303. npcReply.replace("%playername%", player.getName());
  1304. player.sendPacket(npcReply);
  1305. player.sendPacket(ActionFailed.STATIC_PACKET);
  1306. }
  1307. else
  1308. {
  1309. player.sendMessage(res);
  1310. }
  1311. return false;
  1312. }
  1313. /**
  1314. * Loads all quest states and variables for the specified player.
  1315. * @param player the player who is entering the world
  1316. */
  1317. public static final void playerEnter(L2PcInstance player)
  1318. {
  1319. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1320. PreparedStatement invalidQuestData = con.prepareStatement("DELETE FROM character_quests WHERE charId = ? AND name = ?");
  1321. PreparedStatement invalidQuestDataVar = con.prepareStatement("DELETE FROM character_quests WHERE charId = ? AND name = ? AND var = ?");
  1322. PreparedStatement ps1 = con.prepareStatement("SELECT name, value FROM character_quests WHERE charId = ? AND var = ?"))
  1323. {
  1324. // Get list of quests owned by the player from database
  1325. ps1.setInt(1, player.getObjectId());
  1326. ps1.setString(2, "<state>");
  1327. try (ResultSet rs = ps1.executeQuery())
  1328. {
  1329. while (rs.next())
  1330. {
  1331. // Get the ID of the quest and its state
  1332. String questId = rs.getString("name");
  1333. String statename = rs.getString("value");
  1334. // Search quest associated with the ID
  1335. Quest q = QuestManager.getInstance().getQuest(questId);
  1336. if (q == null)
  1337. {
  1338. _log.finer("Unknown quest " + questId + " for player " + player.getName());
  1339. if (Config.AUTODELETE_INVALID_QUEST_DATA)
  1340. {
  1341. invalidQuestData.setInt(1, player.getObjectId());
  1342. invalidQuestData.setString(2, questId);
  1343. invalidQuestData.executeUpdate();
  1344. }
  1345. continue;
  1346. }
  1347. // Create a new QuestState for the player that will be added to the player's list of quests
  1348. new QuestState(q, player, State.getStateId(statename));
  1349. }
  1350. }
  1351. // Get list of quests owned by the player from the DB in order to add variables used in the quest.
  1352. try (PreparedStatement ps2 = con.prepareStatement("SELECT name, var, value FROM character_quests WHERE charId = ? AND var <> ?"))
  1353. {
  1354. ps2.setInt(1, player.getObjectId());
  1355. ps2.setString(2, "<state>");
  1356. try (ResultSet rs = ps2.executeQuery())
  1357. {
  1358. while (rs.next())
  1359. {
  1360. String questId = rs.getString("name");
  1361. String var = rs.getString("var");
  1362. String value = rs.getString("value");
  1363. // Get the QuestState saved in the loop before
  1364. QuestState qs = player.getQuestState(questId);
  1365. if (qs == null)
  1366. {
  1367. _log.finer("Lost variable " + var + " in quest " + questId + " for player " + player.getName());
  1368. if (Config.AUTODELETE_INVALID_QUEST_DATA)
  1369. {
  1370. invalidQuestDataVar.setInt(1, player.getObjectId());
  1371. invalidQuestDataVar.setString(2, questId);
  1372. invalidQuestDataVar.setString(3, var);
  1373. invalidQuestDataVar.executeUpdate();
  1374. }
  1375. continue;
  1376. }
  1377. // Add parameter to the quest
  1378. qs.setInternal(var, value);
  1379. }
  1380. }
  1381. }
  1382. }
  1383. catch (Exception e)
  1384. {
  1385. _log.log(Level.WARNING, "could not insert char quest:", e);
  1386. }
  1387. // events
  1388. for (String name : QuestManager.getInstance().getScripts().keySet())
  1389. {
  1390. player.processQuestEvent(name, "enter");
  1391. }
  1392. }
  1393. /**
  1394. * Insert (or update) in the database variables that need to stay persistent for this quest after a reboot.<br>
  1395. * This function is for storage of values that do not related to a specific player but are global for all characters.<br>
  1396. * For example, if we need to disable a quest-gatekeeper until a certain time (as is done with some grand-boss gatekeepers), we can save that time in the DB.
  1397. * @param var the name of the variable to save
  1398. * @param value the value of the variable
  1399. */
  1400. public final void saveGlobalQuestVar(String var, String value)
  1401. {
  1402. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1403. PreparedStatement statement = con.prepareStatement("REPLACE INTO quest_global_data (quest_name,var,value) VALUES (?,?,?)"))
  1404. {
  1405. statement.setString(1, getName());
  1406. statement.setString(2, var);
  1407. statement.setString(3, value);
  1408. statement.executeUpdate();
  1409. }
  1410. catch (Exception e)
  1411. {
  1412. _log.log(Level.WARNING, "could not insert global quest variable:", e);
  1413. }
  1414. }
  1415. /**
  1416. * Read from the database a previously saved variable for this quest.<br>
  1417. * Due to performance considerations, this function should best be used only when the quest is first loaded.<br>
  1418. * Subclasses of this class can define structures into which these loaded values can be saved.<br>
  1419. * However, on-demand usage of this function throughout the script is not prohibited, only not recommended.<br>
  1420. * Values read from this function were entered by calls to "saveGlobalQuestVar".
  1421. * @param var the name of the variable to load
  1422. * @return the current value of the specified variable, or an empty string if the variable does not exist
  1423. */
  1424. public final String loadGlobalQuestVar(String var)
  1425. {
  1426. String result = "";
  1427. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1428. PreparedStatement statement = con.prepareStatement("SELECT value FROM quest_global_data WHERE quest_name = ? AND var = ?"))
  1429. {
  1430. statement.setString(1, getName());
  1431. statement.setString(2, var);
  1432. try (ResultSet rs = statement.executeQuery())
  1433. {
  1434. if (rs.first())
  1435. {
  1436. result = rs.getString(1);
  1437. }
  1438. }
  1439. }
  1440. catch (Exception e)
  1441. {
  1442. _log.log(Level.WARNING, "could not load global quest variable:", e);
  1443. }
  1444. return result;
  1445. }
  1446. /**
  1447. * Permanently delete from the database a global quest variable that was previously saved for this quest.
  1448. * @param var the name of the variable to delete
  1449. */
  1450. public final void deleteGlobalQuestVar(String var)
  1451. {
  1452. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1453. PreparedStatement statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ? AND var = ?"))
  1454. {
  1455. statement.setString(1, getName());
  1456. statement.setString(2, var);
  1457. statement.executeUpdate();
  1458. }
  1459. catch (Exception e)
  1460. {
  1461. _log.log(Level.WARNING, "could not delete global quest variable:", e);
  1462. }
  1463. }
  1464. /**
  1465. * Permanently delete from the database all global quest variables that were previously saved for this quest.
  1466. */
  1467. public final void deleteAllGlobalQuestVars()
  1468. {
  1469. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1470. PreparedStatement statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ?"))
  1471. {
  1472. statement.setString(1, getName());
  1473. statement.executeUpdate();
  1474. }
  1475. catch (Exception e)
  1476. {
  1477. _log.log(Level.WARNING, "could not delete global quest variables:", e);
  1478. }
  1479. }
  1480. /**
  1481. * Insert in the database the quest for the player.
  1482. * @param qs the {@link QuestState} object whose variable to insert
  1483. * @param var the name of the variable
  1484. * @param value the value of the variable
  1485. */
  1486. public static void createQuestVarInDb(QuestState qs, String var, String value)
  1487. {
  1488. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1489. PreparedStatement statement = con.prepareStatement("INSERT INTO character_quests (charId,name,var,value) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE value=?"))
  1490. {
  1491. statement.setInt(1, qs.getPlayer().getObjectId());
  1492. statement.setString(2, qs.getQuestName());
  1493. statement.setString(3, var);
  1494. statement.setString(4, value);
  1495. statement.setString(5, value);
  1496. statement.executeUpdate();
  1497. }
  1498. catch (Exception e)
  1499. {
  1500. _log.log(Level.WARNING, "could not insert char quest:", e);
  1501. }
  1502. }
  1503. /**
  1504. * Update the value of the variable "var" for the specified quest in database
  1505. * @param qs the {@link QuestState} object whose variable to update
  1506. * @param var the name of the variable
  1507. * @param value the value of the variable
  1508. */
  1509. public static void updateQuestVarInDb(QuestState qs, String var, String value)
  1510. {
  1511. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1512. PreparedStatement statement = con.prepareStatement("UPDATE character_quests SET value=? WHERE charId=? AND name=? AND var = ?"))
  1513. {
  1514. statement.setString(1, value);
  1515. statement.setInt(2, qs.getPlayer().getObjectId());
  1516. statement.setString(3, qs.getQuestName());
  1517. statement.setString(4, var);
  1518. statement.executeUpdate();
  1519. }
  1520. catch (Exception e)
  1521. {
  1522. _log.log(Level.WARNING, "could not update char quest:", e);
  1523. }
  1524. }
  1525. /**
  1526. * Delete a variable of player's quest from the database.
  1527. * @param qs the {@link QuestState} object whose variable to delete
  1528. * @param var the name of the variable to delete
  1529. */
  1530. public static void deleteQuestVarInDb(QuestState qs, String var)
  1531. {
  1532. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1533. PreparedStatement statement = con.prepareStatement("DELETE FROM character_quests WHERE charId=? AND name=? AND var=?"))
  1534. {
  1535. statement.setInt(1, qs.getPlayer().getObjectId());
  1536. statement.setString(2, qs.getQuestName());
  1537. statement.setString(3, var);
  1538. statement.executeUpdate();
  1539. }
  1540. catch (Exception e)
  1541. {
  1542. _log.log(Level.WARNING, "could not delete char quest:", e);
  1543. }
  1544. }
  1545. /**
  1546. * Delete from the database all variables and states of the specified quest state.
  1547. * @param qs the {@link QuestState} object whose variables to delete
  1548. * @param repeatable if {@code false}, the state variable will be preserved, otherwise it will be deleted as well
  1549. */
  1550. public static void deleteQuestInDb(QuestState qs, boolean repeatable)
  1551. {
  1552. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1553. PreparedStatement ps = con.prepareStatement(repeatable ? QUEST_DELETE_FROM_CHAR_QUERY : QUEST_DELETE_FROM_CHAR_QUERY_NON_REPEATABLE_QUERY))
  1554. {
  1555. ps.setInt(1, qs.getPlayer().getObjectId());
  1556. ps.setString(2, qs.getQuestName());
  1557. if (!repeatable)
  1558. {
  1559. ps.setString(3, "<state>");
  1560. }
  1561. ps.executeUpdate();
  1562. }
  1563. catch (Exception e)
  1564. {
  1565. _log.log(Level.WARNING, "could not delete char quest:", e);
  1566. }
  1567. }
  1568. /**
  1569. * Create a database record for the specified quest state.
  1570. * @param qs the {@link QuestState} object whose data to write in the database
  1571. */
  1572. public static void createQuestInDb(QuestState qs)
  1573. {
  1574. createQuestVarInDb(qs, "<state>", State.getStateName(qs.getState()));
  1575. }
  1576. /**
  1577. * Update a quest state record of the specified quest state in database.
  1578. * @param qs the {@link QuestState} object whose data to update in the database
  1579. */
  1580. public static void updateQuestInDb(QuestState qs)
  1581. {
  1582. updateQuestVarInDb(qs, "<state>", State.getStateName(qs.getState()));
  1583. }
  1584. /**
  1585. * @param player the player whose language settings to use in finding the html of the right language
  1586. * @return the default html for when no quest is available: "You are either not on a quest that involves this NPC.."
  1587. */
  1588. public static String getNoQuestMsg(L2PcInstance player)
  1589. {
  1590. final String result = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/noquest.htm");
  1591. if ((result != null) && (result.length() > 0))
  1592. {
  1593. return result;
  1594. }
  1595. return DEFAULT_NO_QUEST_MSG;
  1596. }
  1597. /**
  1598. * @param player the player whose language settings to use in finding the html of the right language
  1599. * @return the default html for when no quest is already completed: "This quest has already been completed."
  1600. */
  1601. public static String getAlreadyCompletedMsg(L2PcInstance player)
  1602. {
  1603. final String result = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/alreadycompleted.htm");
  1604. if ((result != null) && (result.length() > 0))
  1605. {
  1606. return result;
  1607. }
  1608. return DEFAULT_ALREADY_COMPLETED_MSG;
  1609. }
  1610. // TODO: Remove after all Jython scripts are replaced with Java versions.
  1611. public void addStartNpc(int npcId)
  1612. {
  1613. setNpcQuestStartId(npcId);
  1614. }
  1615. public void addFirstTalkId(int npcId)
  1616. {
  1617. setNpcFirstTalkId(event -> notifyFirstTalk(event.getNpc(), event.getActiveChar()), npcId);
  1618. }
  1619. public void addTalkId(int npcId)
  1620. {
  1621. setNpcTalkId(npcId);
  1622. }
  1623. public void addKillId(int npcId)
  1624. {
  1625. setAttackableKillId(kill -> notifyKill(kill.getTarget(), kill.getAttacker(), kill.isSummon()), npcId);
  1626. }
  1627. public void addAttackId(int npcId)
  1628. {
  1629. setAttackableAttackId(attack -> notifyAttack(attack.getTarget(), attack.getAttacker(), attack.getDamage(), attack.isSummon(), attack.getSkill()), npcId);
  1630. }
  1631. /**
  1632. * Add the quest to the NPC's startQuest
  1633. * @param npcIds the IDs of the NPCs to register
  1634. */
  1635. public void addStartNpc(int... npcIds)
  1636. {
  1637. setNpcQuestStartId(npcIds);
  1638. }
  1639. /**
  1640. * Add the quest to the NPC's startQuest
  1641. * @param npcIds the IDs of the NPCs to register
  1642. */
  1643. public void addStartNpc(Collection<Integer> npcIds)
  1644. {
  1645. setNpcQuestStartId(npcIds);
  1646. }
  1647. /**
  1648. * Add the quest to the NPC's first-talk (default action dialog).
  1649. * @param npcIds the IDs of the NPCs to register
  1650. */
  1651. public void addFirstTalkId(int... npcIds)
  1652. {
  1653. setNpcFirstTalkId(event -> notifyFirstTalk(event.getNpc(), event.getActiveChar()), npcIds);
  1654. }
  1655. /**
  1656. * Add the quest to the NPC's first-talk (default action dialog).
  1657. * @param npcIds the IDs of the NPCs to register
  1658. */
  1659. public void addFirstTalkId(Collection<Integer> npcIds)
  1660. {
  1661. setNpcFirstTalkId(event -> notifyFirstTalk(event.getNpc(), event.getActiveChar()), npcIds);
  1662. }
  1663. /**
  1664. * Add the NPC to the AcquireSkill dialog.
  1665. * @param npcIds the IDs of the NPCs to register
  1666. */
  1667. public void addAcquireSkillId(int... npcIds)
  1668. {
  1669. setPlayerSkillLearnId(event -> notifyAcquireSkill(event.getTrainer(), event.getActiveChar(), event.getSkill(), event.getAcquireType()), npcIds);
  1670. }
  1671. /**
  1672. * Add the NPC to the AcquireSkill dialog.
  1673. * @param npcIds the IDs of the NPCs to register
  1674. */
  1675. public void addAcquireSkillId(Collection<Integer> npcIds)
  1676. {
  1677. setPlayerSkillLearnId(event -> notifyAcquireSkill(event.getTrainer(), event.getActiveChar(), event.getSkill(), event.getAcquireType()), npcIds);
  1678. }
  1679. /**
  1680. * Add the Item to the notify when player speaks with it.
  1681. * @param itemIds the IDs of the Item to register
  1682. */
  1683. public void addItemBypassEventId(int... itemIds)
  1684. {
  1685. setItemBypassEvenId(event -> notifyItemEvent(event.getItem(), event.getActiveChar(), event.getEvent()), itemIds);
  1686. }
  1687. /**
  1688. * Add the Item to the notify when player speaks with it.
  1689. * @param itemIds the IDs of the Item to register
  1690. */
  1691. public void addItemBypassEventId(Collection<Integer> itemIds)
  1692. {
  1693. setItemBypassEvenId(event -> notifyItemEvent(event.getItem(), event.getActiveChar(), event.getEvent()), itemIds);
  1694. }
  1695. /**
  1696. * Add the Item to the notify when player speaks with it.
  1697. * @param itemIds the IDs of the Item to register
  1698. */
  1699. public void addItemTalkId(int... itemIds)
  1700. {
  1701. setItemTalkId(event -> notifyItemTalk(event.getItem(), event.getActiveChar()), itemIds);
  1702. }
  1703. /**
  1704. * Add the Item to the notify when player speaks with it.
  1705. * @param itemIds the IDs of the Item to register
  1706. */
  1707. public void addItemTalkId(Collection<Integer> itemIds)
  1708. {
  1709. setItemTalkId(event -> notifyItemTalk(event.getItem(), event.getActiveChar()), itemIds);
  1710. }
  1711. /**
  1712. * Add this quest to the list of quests that the passed mob will respond to for attack events.
  1713. * @param npcIds the IDs of the NPCs to register
  1714. */
  1715. public void addAttackId(int... npcIds)
  1716. {
  1717. setAttackableAttackId(attack -> notifyAttack(attack.getTarget(), attack.getAttacker(), attack.getDamage(), attack.isSummon(), attack.getSkill()), npcIds);
  1718. }
  1719. /**
  1720. * Add this quest to the list of quests that the passed mob will respond to for attack events.
  1721. * @param npcIds the IDs of the NPCs to register
  1722. */
  1723. public void addAttackId(Collection<Integer> npcIds)
  1724. {
  1725. setAttackableAttackId(attack -> notifyAttack(attack.getTarget(), attack.getAttacker(), attack.getDamage(), attack.isSummon(), attack.getSkill()), npcIds);
  1726. }
  1727. /**
  1728. * Add this quest to the list of quests that the passed mob will respond to for kill events.
  1729. * @param npcIds
  1730. */
  1731. public void addKillId(int... npcIds)
  1732. {
  1733. setAttackableKillId(kill -> notifyKill(kill.getTarget(), kill.getAttacker(), kill.isSummon()), npcIds);
  1734. }
  1735. /**
  1736. * Add this quest event to the collection of NPC IDs that will respond to for on kill events.
  1737. * @param npcIds the collection of NPC IDs
  1738. */
  1739. public void addKillId(Collection<Integer> npcIds)
  1740. {
  1741. setAttackableKillId(kill -> notifyKill(kill.getTarget(), kill.getAttacker(), kill.isSummon()), npcIds);
  1742. }
  1743. /**
  1744. * Add this quest to the list of quests that the passed npc will respond to for Talk Events.
  1745. * @param npcIds the IDs of the NPCs to register
  1746. */
  1747. public void addTalkId(int... npcIds)
  1748. {
  1749. setNpcTalkId(npcIds);
  1750. }
  1751. public void addTalkId(Collection<Integer> npcIds)
  1752. {
  1753. setNpcTalkId(npcIds);
  1754. }
  1755. /**
  1756. * Add this quest to the list of quests that the passed npc will respond to for spawn events.
  1757. * @param npcIds the IDs of the NPCs to register
  1758. */
  1759. public void addSpawnId(int... npcIds)
  1760. {
  1761. setNpcSpawnId(event -> notifySpawn(event.getNpc()), npcIds);
  1762. }
  1763. /**
  1764. * Add this quest to the list of quests that the passed npc will respond to for spawn events.
  1765. * @param npcIds the IDs of the NPCs to register
  1766. */
  1767. public void addSpawnId(Collection<Integer> npcIds)
  1768. {
  1769. setNpcSpawnId(event -> notifySpawn(event.getNpc()), npcIds);
  1770. }
  1771. /**
  1772. * Add this quest to the list of quests that the passed npc will respond to for skill see events.
  1773. * @param npcIds the IDs of the NPCs to register
  1774. */
  1775. public void addSkillSeeId(int... npcIds)
  1776. {
  1777. setNpcSkillSeeId(event -> notifySkillSee(event.getTarget(), event.getCaster(), event.getSkill(), event.getTargets(), event.isSummon()), npcIds);
  1778. }
  1779. /**
  1780. * Add this quest to the list of quests that the passed npc will respond to for skill see events.
  1781. * @param npcIds the IDs of the NPCs to register
  1782. */
  1783. public void addSkillSeeId(Collection<Integer> npcIds)
  1784. {
  1785. setNpcSkillSeeId(event -> notifySkillSee(event.getTarget(), event.getCaster(), event.getSkill(), event.getTargets(), event.isSummon()), npcIds);
  1786. }
  1787. /**
  1788. * @param npcIds the IDs of the NPCs to register
  1789. */
  1790. public void addSpellFinishedId(int... npcIds)
  1791. {
  1792. setNpcSkillFinishedId(event -> notifySpellFinished(event.getCaster(), event.getTarget(), event.getSkill()), npcIds);
  1793. }
  1794. /**
  1795. * @param npcIds the IDs of the NPCs to register
  1796. */
  1797. public void addSpellFinishedId(Collection<Integer> npcIds)
  1798. {
  1799. setNpcSkillFinishedId(event -> notifySpellFinished(event.getCaster(), event.getTarget(), event.getSkill()), npcIds);
  1800. }
  1801. /**
  1802. * @param npcIds the IDs of the NPCs to register
  1803. */
  1804. public void addTrapActionId(int... npcIds)
  1805. {
  1806. setTrapActionId(event -> notifyTrapAction(event.getTrap(), event.getTrigger(), event.getAction()), npcIds);
  1807. }
  1808. /**
  1809. * @param npcIds the IDs of the NPCs to register
  1810. */
  1811. public void addTrapActionId(Collection<Integer> npcIds)
  1812. {
  1813. setTrapActionId(event -> notifyTrapAction(event.getTrap(), event.getTrigger(), event.getAction()), npcIds);
  1814. }
  1815. /**
  1816. * Add this quest to the list of quests that the passed npc will respond to for faction call events.
  1817. * @param npcIds the IDs of the NPCs to register
  1818. */
  1819. public void addFactionCallId(int... npcIds)
  1820. {
  1821. setAttackableFactionIdId(event -> notifyFactionCall(event.getNpc(), event.getCaller(), event.getAttacker(), event.isSummon()), npcIds);
  1822. }
  1823. /**
  1824. * Add this quest to the list of quests that the passed npc will respond to for faction call events.
  1825. * @param npcIds the IDs of the NPCs to register
  1826. */
  1827. public void addFactionCallId(Collection<Integer> npcIds)
  1828. {
  1829. setAttackableFactionIdId(event -> notifyFactionCall(event.getNpc(), event.getCaller(), event.getAttacker(), event.isSummon()), npcIds);
  1830. }
  1831. /**
  1832. * Add this quest to the list of quests that the passed npc will respond to for character see events.
  1833. * @param npcIds the IDs of the NPCs to register
  1834. */
  1835. public void addAggroRangeEnterId(int... npcIds)
  1836. {
  1837. setAttackableAggroRangeEnterId(event -> notifyAggroRangeEnter(event.getNpc(), event.getActiveChar(), event.isSummon()), npcIds);
  1838. }
  1839. /**
  1840. * Add this quest to the list of quests that the passed npc will respond to for character see events.
  1841. * @param npcIds the IDs of the NPCs to register
  1842. */
  1843. public void addAggroRangeEnterId(Collection<Integer> npcIds)
  1844. {
  1845. setAttackableAggroRangeEnterId(event -> notifyAggroRangeEnter(event.getNpc(), event.getActiveChar(), event.isSummon()), npcIds);
  1846. }
  1847. /**
  1848. * @param npcIds the IDs of the NPCs to register
  1849. */
  1850. public void addSeeCreatureId(int... npcIds)
  1851. {
  1852. setNpcCreatureSeeId(event -> notifySeeCreature(event.getNpc(), event.getCreature(), event.isSummon()), npcIds);
  1853. }
  1854. /**
  1855. * @param npcIds the IDs of the NPCs to register
  1856. */
  1857. public void addSeeCreatureId(Collection<Integer> npcIds)
  1858. {
  1859. setNpcCreatureSeeId(event -> notifySeeCreature(event.getNpc(), event.getCreature(), event.isSummon()), npcIds);
  1860. }
  1861. /**
  1862. * Register onEnterZone trigger for zone
  1863. * @param zoneId the ID of the zone to register
  1864. */
  1865. public void addEnterZoneId(int zoneId)
  1866. {
  1867. setCreatureZoneEnterId(event -> notifyEnterZone(event.getCreature(), event.getZone()), zoneId);
  1868. }
  1869. /**
  1870. * Register onEnterZone trigger for zones
  1871. * @param zoneIds the IDs of the zones to register
  1872. */
  1873. public void addEnterZoneId(int... zoneIds)
  1874. {
  1875. setCreatureZoneEnterId(event -> notifyEnterZone(event.getCreature(), event.getZone()), zoneIds);
  1876. }
  1877. /**
  1878. * Register onEnterZone trigger for zones
  1879. * @param zoneIds the IDs of the zones to register
  1880. */
  1881. public void addEnterZoneId(Collection<Integer> zoneIds)
  1882. {
  1883. setCreatureZoneEnterId(event -> notifyEnterZone(event.getCreature(), event.getZone()), zoneIds);
  1884. }
  1885. /**
  1886. * Register onExitZone trigger for zone
  1887. * @param zoneId the ID of the zone to register
  1888. */
  1889. public void addExitZoneId(int zoneId)
  1890. {
  1891. setCreatureZoneExitId(event -> notifyExitZone(event.getCreature(), event.getZone()), zoneId);
  1892. }
  1893. /**
  1894. * Register onExitZone trigger for zones
  1895. * @param zoneIds the IDs of the zones to register
  1896. */
  1897. public void addExitZoneId(int... zoneIds)
  1898. {
  1899. setCreatureZoneExitId(event -> notifyExitZone(event.getCreature(), event.getZone()), zoneIds);
  1900. }
  1901. /**
  1902. * Register onExitZone trigger for zones
  1903. * @param zoneIds the IDs of the zones to register
  1904. */
  1905. public void addExitZoneId(Collection<Integer> zoneIds)
  1906. {
  1907. setCreatureZoneExitId(event -> notifyExitZone(event.getCreature(), event.getZone()), zoneIds);
  1908. }
  1909. /**
  1910. * Register onEventReceived trigger for NPC
  1911. * @param npcIds the IDs of the NPCs to register
  1912. */
  1913. public void addEventReceivedId(int... npcIds)
  1914. {
  1915. setNpcEventReceivedId(event -> notifyEventReceived(event.getEventName(), event.getSender(), event.getReceiver(), event.getReference()), npcIds);
  1916. }
  1917. /**
  1918. * Register onEventReceived trigger for NPC
  1919. * @param npcIds the IDs of the NPCs to register
  1920. */
  1921. public void addEventReceivedId(Collection<Integer> npcIds)
  1922. {
  1923. setNpcEventReceivedId(event -> notifyEventReceived(event.getEventName(), event.getSender(), event.getReceiver(), event.getReference()), npcIds);
  1924. }
  1925. /**
  1926. * Register onMoveFinished trigger for NPC
  1927. * @param npcIds the IDs of the NPCs to register
  1928. */
  1929. public void addMoveFinishedId(int... npcIds)
  1930. {
  1931. setNpcMoveFinishedId(event -> notifyMoveFinished(event.getNpc()), npcIds);
  1932. }
  1933. /**
  1934. * Register onMoveFinished trigger for NPC
  1935. * @param npcIds the IDs of the NPCs to register
  1936. */
  1937. public void addMoveFinishedId(Collection<Integer> npcIds)
  1938. {
  1939. setNpcMoveFinishedId(event -> notifyMoveFinished(event.getNpc()), npcIds);
  1940. }
  1941. /**
  1942. * Register onNodeArrived trigger for NPC
  1943. * @param npcIds the IDs of the NPCs to register
  1944. */
  1945. public void addNodeArrivedId(int... npcIds)
  1946. {
  1947. setNpcMoveNodeArrivedId(event -> notifyNodeArrived(event.getNpc()), npcIds);
  1948. }
  1949. /**
  1950. * Register onNodeArrived trigger for NPC
  1951. * @param npcIds the IDs of the NPCs to register
  1952. */
  1953. public void addNodeArrivedId(Collection<Integer> npcIds)
  1954. {
  1955. setNpcMoveNodeArrivedId(event -> notifyNodeArrived(event.getNpc()), npcIds);
  1956. }
  1957. /**
  1958. * Register onRouteFinished trigger for NPC
  1959. * @param npcIds the IDs of the NPCs to register
  1960. */
  1961. public void addRouteFinishedId(int... npcIds)
  1962. {
  1963. setNpcMoveRouteFinishedId(event -> notifyRouteFinished(event.getNpc()), npcIds);
  1964. }
  1965. /**
  1966. * Register onRouteFinished trigger for NPC
  1967. * @param npcIds the IDs of the NPCs to register
  1968. */
  1969. public void addRouteFinishedId(Collection<Integer> npcIds)
  1970. {
  1971. setNpcMoveRouteFinishedId(event -> notifyRouteFinished(event.getNpc()), npcIds);
  1972. }
  1973. /**
  1974. * Register onNpcHate trigger for NPC
  1975. * @param npcIds
  1976. */
  1977. public void addNpcHateId(int... npcIds)
  1978. {
  1979. addNpcHateId(event -> new TerminateReturn(!onNpcHate(event.getNpc(), event.getActiveChar(), event.isSummon()), false, false), npcIds);
  1980. }
  1981. /**
  1982. * Register onNpcHate trigger for NPC
  1983. * @param npcIds
  1984. */
  1985. public void addNpcHateId(Collection<Integer> npcIds)
  1986. {
  1987. addNpcHateId(event -> new TerminateReturn(!onNpcHate(event.getNpc(), event.getActiveChar(), event.isSummon()), false, false), npcIds);
  1988. }
  1989. /**
  1990. * Register onSummonSpawn trigger when summon is spawned.
  1991. * @param npcIds
  1992. */
  1993. public void addSummonSpawnId(int... npcIds)
  1994. {
  1995. setPlayerSummonSpawnId(event -> onSummonSpawn(event.getSummon()), npcIds);
  1996. }
  1997. /**
  1998. * Register onSummonSpawn trigger when summon is spawned.
  1999. * @param npcIds
  2000. */
  2001. public void addSummonSpawnId(Collection<Integer> npcIds)
  2002. {
  2003. setPlayerSummonSpawnId(event -> onSummonSpawn(event.getSummon()), npcIds);
  2004. }
  2005. /**
  2006. * Register onSummonTalk trigger when master talked to summon.
  2007. * @param npcIds
  2008. */
  2009. public void addSummonTalkId(int... npcIds)
  2010. {
  2011. setPlayerSummonTalkId(event -> onSummonTalk(event.getSummon()), npcIds);
  2012. }
  2013. /**
  2014. * Register onSummonTalk trigger when summon is spawned.
  2015. * @param npcIds
  2016. */
  2017. public void addSummonTalkId(Collection<Integer> npcIds)
  2018. {
  2019. setPlayerSummonTalkId(event -> onSummonTalk(event.getSummon()), npcIds);
  2020. }
  2021. /**
  2022. * Registers onCanSeeMe trigger whenever an npc info must be sent to player.
  2023. * @param npcIds
  2024. */
  2025. public void addCanSeeMeId(int... npcIds)
  2026. {
  2027. addNpcHateId(event -> new TerminateReturn(!notifyOnCanSeeMe(event.getNpc(), event.getActiveChar()), false, false), npcIds);
  2028. }
  2029. /**
  2030. * Registers onCanSeeMe trigger whenever an npc info must be sent to player.
  2031. * @param npcIds
  2032. */
  2033. public void addCanSeeMeId(Collection<Integer> npcIds)
  2034. {
  2035. addNpcHateId(event -> new TerminateReturn(!notifyOnCanSeeMe(event.getNpc(), event.getActiveChar()), false, false), npcIds);
  2036. }
  2037. public void addOlympiadMatchFinishId()
  2038. {
  2039. setOlympiadMatchResult(event -> notifyOlympiadMatch(event.getWinner(), event.getLoser(), event.getCompetitionType()));
  2040. }
  2041. /**
  2042. * Use this method to get a random party member from a player's party.<br>
  2043. * Useful when distributing rewards after killing an NPC.
  2044. * @param player this parameter represents the player whom the party will taken.
  2045. * @return {@code null} if {@code player} is {@code null}, {@code player} itself if the player does not have a party, and a random party member in all other cases
  2046. */
  2047. public L2PcInstance getRandomPartyMember(L2PcInstance player)
  2048. {
  2049. if (player == null)
  2050. {
  2051. return null;
  2052. }
  2053. final L2Party party = player.getParty();
  2054. if ((party == null) || (party.getMembers().isEmpty()))
  2055. {
  2056. return player;
  2057. }
  2058. return party.getMembers().get(Rnd.get(party.getMembers().size()));
  2059. }
  2060. /**
  2061. * Get a random party member with required cond value.
  2062. * @param player the instance of a player whose party is to be searched
  2063. * @param cond the value of the "cond" variable that must be matched
  2064. * @return a random party member that matches the specified condition, or {@code null} if no match was found
  2065. */
  2066. public L2PcInstance getRandomPartyMember(L2PcInstance player, int cond)
  2067. {
  2068. return getRandomPartyMember(player, "cond", String.valueOf(cond));
  2069. }
  2070. /**
  2071. * Auxiliary function for party quests.<br>
  2072. * Note: This function is only here because of how commonly it may be used by quest developers.<br>
  2073. * For any variations on this function, the quest script can always handle things on its own.
  2074. * @param player the instance of a player whose party is to be searched
  2075. * @param var the quest variable to look for in party members. If {@code null}, it simply unconditionally returns a random party member
  2076. * @param value the value of the specified quest variable the random party member must have
  2077. * @return a random party member that matches the specified conditions or {@code null} if no match was found.<br>
  2078. * If the {@code var} parameter is {@code null}, a random party member is selected without any conditions.<br>
  2079. * The party member must be within a range of 1500 ingame units of the target of the reference player, or, if no target exists, within the same range of the player itself
  2080. */
  2081. public L2PcInstance getRandomPartyMember(L2PcInstance player, String var, String value)
  2082. {
  2083. // if no valid player instance is passed, there is nothing to check...
  2084. if (player == null)
  2085. {
  2086. return null;
  2087. }
  2088. // for null var condition, return any random party member.
  2089. if (var == null)
  2090. {
  2091. return getRandomPartyMember(player);
  2092. }
  2093. // normal cases...if the player is not in a party, check the player's state
  2094. QuestState temp = null;
  2095. L2Party party = player.getParty();
  2096. // if this player is not in a party, just check if this player instance matches the conditions itself
  2097. if ((party == null) || (party.getMembers().isEmpty()))
  2098. {
  2099. temp = player.getQuestState(getName());
  2100. if ((temp != null) && temp.isSet(var) && temp.get(var).equalsIgnoreCase(value))
  2101. {
  2102. return player; // match
  2103. }
  2104. return null; // no match
  2105. }
  2106. // if the player is in a party, gather a list of all matching party members (possibly including this player)
  2107. List<L2PcInstance> candidates = new ArrayList<>();
  2108. // get the target for enforcing distance limitations.
  2109. L2Object target = player.getTarget();
  2110. if (target == null)
  2111. {
  2112. target = player;
  2113. }
  2114. for (L2PcInstance partyMember : party.getMembers())
  2115. {
  2116. if (partyMember == null)
  2117. {
  2118. continue;
  2119. }
  2120. temp = partyMember.getQuestState(getName());
  2121. if ((temp != null) && (temp.get(var) != null) && (temp.get(var)).equalsIgnoreCase(value) && partyMember.isInsideRadius(target, 1500, true, false))
  2122. {
  2123. candidates.add(partyMember);
  2124. }
  2125. }
  2126. // if there was no match, return null...
  2127. if (candidates.isEmpty())
  2128. {
  2129. return null;
  2130. }
  2131. // if a match was found from the party, return one of them at random.
  2132. return candidates.get(Rnd.get(candidates.size()));
  2133. }
  2134. /**
  2135. * Auxiliary function for party quests.<br>
  2136. * Note: This function is only here because of how commonly it may be used by quest developers.<br>
  2137. * For any variations on this function, the quest script can always handle things on its own.
  2138. * @param player the player whose random party member is to be selected
  2139. * @param state the quest state required of the random party member
  2140. * @return {@code null} if nothing was selected or a random party member that has the specified quest state
  2141. */
  2142. public L2PcInstance getRandomPartyMemberState(L2PcInstance player, byte state)
  2143. {
  2144. // if no valid player instance is passed, there is nothing to check...
  2145. if (player == null)
  2146. {
  2147. return null;
  2148. }
  2149. // normal cases...if the player is not in a party check the player's state
  2150. QuestState temp = null;
  2151. L2Party party = player.getParty();
  2152. // if this player is not in a party, just check if this player instance matches the conditions itself
  2153. if ((party == null) || (party.getMembers().isEmpty()))
  2154. {
  2155. temp = player.getQuestState(getName());
  2156. if ((temp != null) && (temp.getState() == state))
  2157. {
  2158. return player; // match
  2159. }
  2160. return null; // no match
  2161. }
  2162. // if the player is in a party, gather a list of all matching party members (possibly
  2163. // including this player)
  2164. List<L2PcInstance> candidates = new ArrayList<>();
  2165. // get the target for enforcing distance limitations.
  2166. L2Object target = player.getTarget();
  2167. if (target == null)
  2168. {
  2169. target = player;
  2170. }
  2171. for (L2PcInstance partyMember : party.getMembers())
  2172. {
  2173. if (partyMember == null)
  2174. {
  2175. continue;
  2176. }
  2177. temp = partyMember.getQuestState(getName());
  2178. if ((temp != null) && (temp.getState() == state) && partyMember.isInsideRadius(target, 1500, true, false))
  2179. {
  2180. candidates.add(partyMember);
  2181. }
  2182. }
  2183. // if there was no match, return null...
  2184. if (candidates.isEmpty())
  2185. {
  2186. return null;
  2187. }
  2188. // if a match was found from the party, return one of them at random.
  2189. return candidates.get(Rnd.get(candidates.size()));
  2190. }
  2191. /**
  2192. * Get a random party member from the specified player's party.<br>
  2193. * If the player is not in a party, only the player himself is checked.<br>
  2194. * The lucky member is chosen by standard loot roll rules -<br>
  2195. * each member rolls a random number, the one with the highest roll wins.
  2196. * @param player the player whose party to check
  2197. * @param npc the NPC used for distance and other checks (if {@link #checkPartyMember(L2PcInstance, L2Npc)} is overriden)
  2198. * @return the random party member or {@code null}
  2199. */
  2200. public L2PcInstance getRandomPartyMember(L2PcInstance player, L2Npc npc)
  2201. {
  2202. if ((player == null) || !checkDistanceToTarget(player, npc))
  2203. {
  2204. return null;
  2205. }
  2206. final L2Party party = player.getParty();
  2207. L2PcInstance luckyPlayer = null;
  2208. if (party == null)
  2209. {
  2210. if (checkPartyMember(player, npc))
  2211. {
  2212. luckyPlayer = player;
  2213. }
  2214. }
  2215. else
  2216. {
  2217. int highestRoll = 0;
  2218. for (L2PcInstance member : party.getMembers())
  2219. {
  2220. final int rnd = getRandom(1000);
  2221. if ((rnd > highestRoll) && checkPartyMember(member, npc))
  2222. {
  2223. highestRoll = rnd;
  2224. luckyPlayer = member;
  2225. }
  2226. }
  2227. }
  2228. if ((luckyPlayer != null) && checkDistanceToTarget(luckyPlayer, npc))
  2229. {
  2230. return luckyPlayer;
  2231. }
  2232. return null;
  2233. }
  2234. /**
  2235. * This method is called for every party member in {@link #getRandomPartyMember(L2PcInstance, L2Npc)}.<br>
  2236. * It is intended to be overriden by the specific quest implementations.
  2237. * @param player the player to check
  2238. * @param npc the NPC that was passed to {@link #getRandomPartyMember(L2PcInstance, L2Npc)}
  2239. * @return {@code true} if this party member passes the check, {@code false} otherwise
  2240. */
  2241. public boolean checkPartyMember(L2PcInstance player, L2Npc npc)
  2242. {
  2243. return true;
  2244. }
  2245. /**
  2246. * Get a random party member from the player's party who has this quest at the specified quest progress.<br>
  2247. * If the player is not in a party, only the player himself is checked.
  2248. * @param player the player whose random party member state to get
  2249. * @param condition the quest progress step the random member should be at (-1 = check only if quest is started)
  2250. * @param playerChance how many times more chance does the player get compared to other party members (3 - 3x more chance).<br>
  2251. * On retail servers, the killer usually gets 2-3x more chance than other party members
  2252. * @param target the NPC to use for the distance check (can be null)
  2253. * @return the {@link QuestState} object of the random party member or {@code null} if none matched the condition
  2254. */
  2255. public QuestState getRandomPartyMemberState(L2PcInstance player, int condition, int playerChance, L2Npc target)
  2256. {
  2257. if ((player == null) || (playerChance < 1))
  2258. {
  2259. return null;
  2260. }
  2261. QuestState qs = player.getQuestState(getName());
  2262. if (!player.isInParty())
  2263. {
  2264. if (!checkPartyMemberConditions(qs, condition, target))
  2265. {
  2266. return null;
  2267. }
  2268. if (!checkDistanceToTarget(player, target))
  2269. {
  2270. return null;
  2271. }
  2272. return qs;
  2273. }
  2274. final List<QuestState> candidates = new ArrayList<>();
  2275. if (checkPartyMemberConditions(qs, condition, target) && (playerChance > 0))
  2276. {
  2277. for (int i = 0; i < playerChance; i++)
  2278. {
  2279. candidates.add(qs);
  2280. }
  2281. }
  2282. for (L2PcInstance member : player.getParty().getMembers())
  2283. {
  2284. if (member == player)
  2285. {
  2286. continue;
  2287. }
  2288. qs = member.getQuestState(getName());
  2289. if (checkPartyMemberConditions(qs, condition, target))
  2290. {
  2291. candidates.add(qs);
  2292. }
  2293. }
  2294. if (candidates.isEmpty())
  2295. {
  2296. return null;
  2297. }
  2298. qs = candidates.get(getRandom(candidates.size()));
  2299. if (!checkDistanceToTarget(qs.getPlayer(), target))
  2300. {
  2301. return null;
  2302. }
  2303. return qs;
  2304. }
  2305. private boolean checkPartyMemberConditions(QuestState qs, int condition, L2Npc npc)
  2306. {
  2307. return ((qs != null) && ((condition == -1) ? qs.isStarted() : qs.isCond(condition)) && checkPartyMember(qs, npc));
  2308. }
  2309. private static boolean checkDistanceToTarget(L2PcInstance player, L2Npc target)
  2310. {
  2311. return ((target == null) || com.l2jserver.gameserver.util.Util.checkIfInRange(1500, player, target, true));
  2312. }
  2313. /**
  2314. * This method is called for every party member in {@link #getRandomPartyMemberState(L2PcInstance, int, int, L2Npc)} if/after all the standard checks are passed.<br>
  2315. * It is intended to be overriden by the specific quest implementations.<br>
  2316. * It can be used in cases when there are more checks performed than simply a quest condition check,<br>
  2317. * for example, if an item is required in the player's inventory.
  2318. * @param qs the {@link QuestState} object of the party member
  2319. * @param npc the NPC that was passed as the last parameter to {@link #getRandomPartyMemberState(L2PcInstance, int, int, L2Npc)}
  2320. * @return {@code true} if this party member passes the check, {@code false} otherwise
  2321. */
  2322. public boolean checkPartyMember(QuestState qs, L2Npc npc)
  2323. {
  2324. return true;
  2325. }
  2326. /**
  2327. * Send an HTML file to the specified player.
  2328. * @param player the player to send the HTML to
  2329. * @param filename the name of the HTML file to show
  2330. * @return the contents of the HTML file that was sent to the player
  2331. * @see #showHtmlFile(L2PcInstance, String, L2Npc)
  2332. */
  2333. public String showHtmlFile(L2PcInstance player, String filename)
  2334. {
  2335. return showHtmlFile(player, filename, null);
  2336. }
  2337. /**
  2338. * Send an HTML file to the specified player.
  2339. * @param player the player to send the HTML file to
  2340. * @param filename the name of the HTML file to show
  2341. * @param npc the NPC that is showing the HTML file
  2342. * @return the contents of the HTML file that was sent to the player
  2343. * @see #showHtmlFile(L2PcInstance, String, L2Npc)
  2344. */
  2345. public String showHtmlFile(L2PcInstance player, String filename, L2Npc npc)
  2346. {
  2347. boolean questwindow = !filename.endsWith(".html");
  2348. int questId = getId();
  2349. // Create handler to file linked to the quest
  2350. String content = getHtm(player.getHtmlPrefix(), filename);
  2351. // Send message to client if message not empty
  2352. if (content != null)
  2353. {
  2354. if (npc != null)
  2355. {
  2356. content = content.replaceAll("%objectId%", Integer.toString(npc.getObjectId()));
  2357. }
  2358. if (questwindow && (questId > 0) && (questId < 20000) && (questId != 999))
  2359. {
  2360. NpcQuestHtmlMessage npcReply = new NpcQuestHtmlMessage(npc != null ? npc.getObjectId() : 0, questId);
  2361. npcReply.setHtml(content);
  2362. npcReply.replace("%playername%", player.getName());
  2363. player.sendPacket(npcReply);
  2364. }
  2365. else
  2366. {
  2367. final NpcHtmlMessage npcReply = new NpcHtmlMessage(npc != null ? npc.getObjectId() : 0, content);
  2368. npcReply.replace("%playername%", player.getName());
  2369. player.sendPacket(npcReply);
  2370. }
  2371. player.sendPacket(ActionFailed.STATIC_PACKET);
  2372. }
  2373. return content;
  2374. }
  2375. /**
  2376. * @param prefix player's language prefix.
  2377. * @param fileName the html file to be get.
  2378. * @return the HTML file contents
  2379. */
  2380. public String getHtm(String prefix, String fileName)
  2381. {
  2382. final HtmCache hc = HtmCache.getInstance();
  2383. String content = hc.getHtm(prefix, fileName.startsWith("data/") ? fileName : "data/scripts/" + getDescr().toLowerCase() + "/" + getName() + "/" + fileName);
  2384. if (content == null)
  2385. {
  2386. content = hc.getHtm(prefix, "data/scripts/" + getDescr() + "/" + getName() + "/" + fileName);
  2387. if (content == null)
  2388. {
  2389. content = hc.getHtmForce(prefix, "data/scripts/quests/" + getName() + "/" + fileName);
  2390. }
  2391. }
  2392. return content;
  2393. }
  2394. /**
  2395. * @return the registered quest items IDs.
  2396. */
  2397. public int[] getRegisteredItemIds()
  2398. {
  2399. return questItemIds;
  2400. }
  2401. /**
  2402. * Registers all items that have to be destroyed in case player abort the quest or finish it.
  2403. * @param items
  2404. */
  2405. public void registerQuestItems(int... items)
  2406. {
  2407. questItemIds = items;
  2408. }
  2409. /**
  2410. * Remove all quest items associated with this quest from the specified player's inventory.
  2411. * @param player the player whose quest items to remove
  2412. */
  2413. public void removeRegisteredQuestItems(L2PcInstance player)
  2414. {
  2415. takeItems(player, -1, questItemIds);
  2416. }
  2417. @Override
  2418. public String getScriptName()
  2419. {
  2420. return getName();
  2421. }
  2422. @Override
  2423. public void setActive(boolean status)
  2424. {
  2425. // TODO: Implement me.
  2426. }
  2427. @Override
  2428. public boolean reload()
  2429. {
  2430. unload();
  2431. return super.reload();
  2432. }
  2433. @Override
  2434. public boolean unload()
  2435. {
  2436. return unload(true);
  2437. }
  2438. /**
  2439. * @param removeFromList
  2440. * @return
  2441. */
  2442. public boolean unload(boolean removeFromList)
  2443. {
  2444. saveGlobalData();
  2445. // cancel all pending timers before reloading.
  2446. // if timers ought to be restarted, the quest can take care of it
  2447. // with its code (example: save global data indicating what timer must be restarted).
  2448. for (List<QuestTimer> timers : _allEventTimers.values())
  2449. {
  2450. _readLock.lock();
  2451. try
  2452. {
  2453. for (QuestTimer timer : timers)
  2454. {
  2455. timer.cancel();
  2456. }
  2457. }
  2458. finally
  2459. {
  2460. _readLock.unlock();
  2461. }
  2462. timers.clear();
  2463. }
  2464. _allEventTimers.clear();
  2465. if (removeFromList)
  2466. {
  2467. return QuestManager.getInstance().removeScript(this) && super.unload();
  2468. }
  2469. return super.unload();
  2470. }
  2471. @Override
  2472. public ScriptManager<?> getScriptManager()
  2473. {
  2474. return QuestManager.getInstance();
  2475. }
  2476. public void setOnEnterWorld(boolean state)
  2477. {
  2478. if (state)
  2479. {
  2480. setPlayerLoginId(event -> notifyEnterWorld(event.getActiveChar()));
  2481. }
  2482. else
  2483. {
  2484. getListeners().stream().filter(listener -> listener.getType() == EventType.ON_PLAYER_LOGIN).forEach(AbstractEventListener::unregisterMe);
  2485. }
  2486. }
  2487. /**
  2488. * If a quest is set as custom, it will display it's name in the NPC Quest List.<br>
  2489. * Retail quests are unhardcoded to display the name using a client string.
  2490. * @param val if {@code true} the quest script will be set as custom quest.
  2491. */
  2492. public void setIsCustom(boolean val)
  2493. {
  2494. _isCustom = val;
  2495. }
  2496. /**
  2497. * @return {@code true} if the quest script is a custom quest, {@code false} otherwise.
  2498. */
  2499. public boolean isCustomQuest()
  2500. {
  2501. return _isCustom;
  2502. }
  2503. }