Quest.java 93 KB

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