Quest.java 95 KB

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