Quest.java 86 KB

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