Quest.java 90 KB

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