Quest.java 87 KB

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