Quest.java 93 KB

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