Quest.java 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591
  1. /*
  2. * Copyright (C) 2004-2013 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.HashMap;
  26. import java.util.HashSet;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Set;
  30. import java.util.concurrent.locks.ReentrantReadWriteLock;
  31. import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
  32. import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
  33. import java.util.logging.Level;
  34. import java.util.logging.Logger;
  35. import com.l2jserver.Config;
  36. import com.l2jserver.L2DatabaseFactory;
  37. import com.l2jserver.gameserver.GameTimeController;
  38. import com.l2jserver.gameserver.ThreadPoolManager;
  39. import com.l2jserver.gameserver.cache.HtmCache;
  40. import com.l2jserver.gameserver.datatables.DoorTable;
  41. import com.l2jserver.gameserver.datatables.ItemTable;
  42. import com.l2jserver.gameserver.datatables.NpcTable;
  43. import com.l2jserver.gameserver.enums.QuestEventType;
  44. import com.l2jserver.gameserver.enums.QuestSound;
  45. import com.l2jserver.gameserver.enums.TrapAction;
  46. import com.l2jserver.gameserver.idfactory.IdFactory;
  47. import com.l2jserver.gameserver.instancemanager.InstanceManager;
  48. import com.l2jserver.gameserver.instancemanager.QuestManager;
  49. import com.l2jserver.gameserver.instancemanager.ZoneManager;
  50. import com.l2jserver.gameserver.model.L2Object;
  51. import com.l2jserver.gameserver.model.L2Party;
  52. import com.l2jserver.gameserver.model.L2Spawn;
  53. import com.l2jserver.gameserver.model.Location;
  54. import com.l2jserver.gameserver.model.actor.L2Character;
  55. import com.l2jserver.gameserver.model.actor.L2Npc;
  56. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  57. import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
  58. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  59. import com.l2jserver.gameserver.model.actor.instance.L2TrapInstance;
  60. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  61. import com.l2jserver.gameserver.model.base.AcquireSkillType;
  62. import com.l2jserver.gameserver.model.entity.Instance;
  63. import com.l2jserver.gameserver.model.holders.ItemHolder;
  64. import com.l2jserver.gameserver.model.interfaces.IIdentifiable;
  65. import com.l2jserver.gameserver.model.interfaces.IL2Procedure;
  66. import com.l2jserver.gameserver.model.itemcontainer.PcInventory;
  67. import com.l2jserver.gameserver.model.items.L2Item;
  68. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  69. import com.l2jserver.gameserver.model.olympiad.CompetitionType;
  70. import com.l2jserver.gameserver.model.quest.AITasks.AggroRangeEnter;
  71. import com.l2jserver.gameserver.model.quest.AITasks.SeeCreature;
  72. import com.l2jserver.gameserver.model.quest.AITasks.SkillSee;
  73. import com.l2jserver.gameserver.model.skills.L2Skill;
  74. import com.l2jserver.gameserver.model.stats.Stats;
  75. import com.l2jserver.gameserver.model.zone.L2ZoneType;
  76. import com.l2jserver.gameserver.network.NpcStringId;
  77. import com.l2jserver.gameserver.network.SystemMessageId;
  78. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  79. import com.l2jserver.gameserver.network.serverpackets.ExShowScreenMessage;
  80. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  81. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  82. import com.l2jserver.gameserver.network.serverpackets.NpcQuestHtmlMessage;
  83. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  84. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  85. import com.l2jserver.gameserver.scripting.ManagedScript;
  86. import com.l2jserver.gameserver.scripting.ScriptManager;
  87. import com.l2jserver.gameserver.util.MinionList;
  88. import com.l2jserver.util.L2FastMap;
  89. import com.l2jserver.util.Rnd;
  90. import com.l2jserver.util.Util;
  91. /**
  92. * Quest main class.
  93. * @author Luis Arias
  94. */
  95. public class Quest extends ManagedScript implements IIdentifiable
  96. {
  97. public static final Logger _log = Logger.getLogger(Quest.class.getName());
  98. /** Map containing events from String value of the event. */
  99. private static Map<String, Quest> _allEventsS = new HashMap<>();
  100. /** Map containing lists of timers from the name of the timer. */
  101. private final Map<String, List<QuestTimer>> _allEventTimers = new L2FastMap<>(true);
  102. private final Set<Integer> _questInvolvedNpcs = new HashSet<>();
  103. private final ReentrantReadWriteLock _rwLock = new ReentrantReadWriteLock();
  104. private final WriteLock _writeLock = _rwLock.writeLock();
  105. private final ReadLock _readLock = _rwLock.readLock();
  106. private final int _questId;
  107. private final String _name;
  108. private final String _descr;
  109. private final byte _initialState = State.CREATED;
  110. protected boolean _onEnterWorld = false;
  111. private boolean _isCustom = false;
  112. private boolean _isOlympiadUse = false;
  113. public int[] questItemIds = null;
  114. 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>";
  115. private static final String DEFAULT_ALREADY_COMPLETED_MSG = "<html><body>This quest has already been completed.</body></html>";
  116. private static final String QUEST_DELETE_FROM_CHAR_QUERY = "DELETE FROM character_quests WHERE charId=? AND name=?";
  117. private static final String QUEST_DELETE_FROM_CHAR_QUERY_NON_REPEATABLE_QUERY = "DELETE FROM character_quests WHERE charId=? AND name=? AND var!=?";
  118. private static final int RESET_HOUR = 6;
  119. private static final int RESET_MINUTES = 30;
  120. /**
  121. * @return the reset hour for a daily quest, could be overridden on a script.
  122. */
  123. public int getResetHour()
  124. {
  125. return RESET_HOUR;
  126. }
  127. /**
  128. * @return the reset minutes for a daily quest, could be overridden on a script.
  129. */
  130. public int getResetMinutes()
  131. {
  132. return RESET_MINUTES;
  133. }
  134. /**
  135. * @return a collection of the values contained in the _allEventsS map.
  136. */
  137. public static Collection<Quest> findAllEvents()
  138. {
  139. return _allEventsS.values();
  140. }
  141. /**
  142. * The Quest object constructor.<br>
  143. * Constructing a quest also calls the {@code init_LoadGlobalData} convenience method.
  144. * @param questId ID of the quest
  145. * @param name String corresponding to the name of the quest
  146. * @param descr String for the description of the quest
  147. */
  148. public Quest(int questId, String name, String descr)
  149. {
  150. _questId = questId;
  151. _name = name;
  152. _descr = descr;
  153. if (questId != 0)
  154. {
  155. QuestManager.getInstance().addQuest(this);
  156. }
  157. else
  158. {
  159. _allEventsS.put(name, this);
  160. }
  161. init_LoadGlobalData();
  162. }
  163. /**
  164. * The function init_LoadGlobalData is, by default, called by the constructor of all quests.<br>
  165. * Children of this class can implement this function in order to define what variables to load and what structures to save them in.<br>
  166. * By default, nothing is loaded.
  167. */
  168. protected void init_LoadGlobalData()
  169. {
  170. }
  171. /**
  172. * The function saveGlobalData is, by default, called at shutdown, for all quests, by the QuestManager.<br>
  173. * Children of this class can implement this function in order to convert their structures<br>
  174. * into <var, value> tuples and make calls to save them to the database, if needed.<br>
  175. * By default, nothing is saved.
  176. */
  177. public void saveGlobalData()
  178. {
  179. }
  180. /**
  181. * Gets the quest ID.
  182. * @return the quest ID
  183. */
  184. @Override
  185. public int getId()
  186. {
  187. return _questId;
  188. }
  189. /**
  190. * Add a new quest state of this quest to the database.
  191. * @param player the owner of the newly created quest state
  192. * @return the newly created {@link QuestState} object
  193. */
  194. public QuestState newQuestState(L2PcInstance player)
  195. {
  196. return new QuestState(this, player, getInitialState());
  197. }
  198. /**
  199. * @return the initial state of the quest
  200. */
  201. public byte getInitialState()
  202. {
  203. return _initialState;
  204. }
  205. /**
  206. * @return the name of the quest
  207. */
  208. public String getName()
  209. {
  210. return _name;
  211. }
  212. /**
  213. * @return the description of the quest
  214. */
  215. public String getDescr()
  216. {
  217. return _descr;
  218. }
  219. /**
  220. * Add a timer to the quest (if it doesn't exist already) and start it.
  221. * @param name the name of the timer (also passed back as "event" in {@link #onAdvEvent(String, L2Npc, L2PcInstance)})
  222. * @param time time in ms for when to fire the timer
  223. * @param npc the npc associated with this timer (can be null)
  224. * @param player the player associated with this timer (can be null)
  225. * @see #startQuestTimer(String, long, L2Npc, L2PcInstance, boolean)
  226. */
  227. public void startQuestTimer(String name, long time, L2Npc npc, L2PcInstance player)
  228. {
  229. startQuestTimer(name, time, npc, player, false);
  230. }
  231. /**
  232. * Add a timer to the quest (if it doesn't exist already) and start it.
  233. * @param name the name of the timer (also passed back as "event" in {@link #onAdvEvent(String, L2Npc, L2PcInstance)})
  234. * @param time time in ms for when to fire the timer
  235. * @param npc the npc associated with this timer (can be null)
  236. * @param player the player associated with this timer (can be null)
  237. * @param repeating indicates whether the timer is repeatable or one-time.<br>
  238. * If {@code true}, the task is repeated every {@code time} milliseconds until explicitly stopped.
  239. */
  240. public void startQuestTimer(String name, long time, L2Npc npc, L2PcInstance player, boolean repeating)
  241. {
  242. List<QuestTimer> timers = _allEventTimers.get(name);
  243. // Add quest timer if timer doesn't already exist
  244. if (timers == null)
  245. {
  246. timers = new ArrayList<>();
  247. timers.add(new QuestTimer(this, name, time, npc, player, repeating));
  248. _allEventTimers.put(name, timers);
  249. }
  250. // a timer with this name exists, but may not be for the same set of npc and player
  251. else
  252. {
  253. // if there exists a timer with this name, allow the timer only if the [npc, player] set is unique
  254. // nulls act as wildcards
  255. if (getQuestTimer(name, npc, player) == null)
  256. {
  257. _writeLock.lock();
  258. try
  259. {
  260. timers.add(new QuestTimer(this, name, time, npc, player, repeating));
  261. }
  262. finally
  263. {
  264. _writeLock.unlock();
  265. }
  266. }
  267. }
  268. }
  269. /**
  270. * Get a quest timer that matches the provided name and parameters.
  271. * @param name the name of the quest timer to get
  272. * @param npc the NPC associated with the quest timer to get
  273. * @param player the player associated with the quest timer to get
  274. * @return the quest timer that matches the parameters of this function or {@code null} if nothing was found
  275. */
  276. public QuestTimer getQuestTimer(String name, L2Npc npc, L2PcInstance player)
  277. {
  278. final List<QuestTimer> timers = _allEventTimers.get(name);
  279. if (timers != null)
  280. {
  281. _readLock.lock();
  282. try
  283. {
  284. for (QuestTimer timer : timers)
  285. {
  286. if (timer != null)
  287. {
  288. if (timer.isMatch(this, name, npc, player))
  289. {
  290. return timer;
  291. }
  292. }
  293. }
  294. }
  295. finally
  296. {
  297. _readLock.unlock();
  298. }
  299. }
  300. return null;
  301. }
  302. /**
  303. * Cancel all quest timers with the specified name.
  304. * @param name the name of the quest timers to cancel
  305. */
  306. public void cancelQuestTimers(String name)
  307. {
  308. final List<QuestTimer> timers = _allEventTimers.get(name);
  309. if (timers != null)
  310. {
  311. _writeLock.lock();
  312. try
  313. {
  314. for (QuestTimer timer : timers)
  315. {
  316. if (timer != null)
  317. {
  318. timer.cancel();
  319. }
  320. }
  321. timers.clear();
  322. }
  323. finally
  324. {
  325. _writeLock.unlock();
  326. }
  327. }
  328. }
  329. /**
  330. * Cancel the quest timer that matches the specified name and parameters.
  331. * @param name the name of the quest timer to cancel
  332. * @param npc the NPC associated with the quest timer to cancel
  333. * @param player the player associated with the quest timer to cancel
  334. */
  335. public void cancelQuestTimer(String name, L2Npc npc, L2PcInstance player)
  336. {
  337. final QuestTimer timer = getQuestTimer(name, npc, player);
  338. if (timer != null)
  339. {
  340. timer.cancelAndRemove();
  341. }
  342. }
  343. /**
  344. * Remove a quest timer from the list of all timers.<br>
  345. * Note: does not stop the timer itself!
  346. * @param timer the {@link QuestState} object to remove
  347. */
  348. public void removeQuestTimer(QuestTimer timer)
  349. {
  350. if (timer != null)
  351. {
  352. final List<QuestTimer> timers = _allEventTimers.get(timer.getName());
  353. if (timers != null)
  354. {
  355. _writeLock.lock();
  356. try
  357. {
  358. timers.remove(timer);
  359. }
  360. finally
  361. {
  362. _writeLock.unlock();
  363. }
  364. }
  365. }
  366. }
  367. public Map<String, List<QuestTimer>> getQuestTimers()
  368. {
  369. return _allEventTimers;
  370. }
  371. // These are methods to call within the core to call the quest events.
  372. /**
  373. * @param npc the NPC that was attacked
  374. * @param attacker the attacking player
  375. * @param damage the damage dealt to the NPC by the player
  376. * @param isSummon if {@code true}, the attack was actually made by the player's summon
  377. * @param skill the skill used to attack the NPC (can be null)
  378. */
  379. public final void notifyAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isSummon, L2Skill skill)
  380. {
  381. String res = null;
  382. try
  383. {
  384. res = onAttack(npc, attacker, damage, isSummon, skill);
  385. }
  386. catch (Exception e)
  387. {
  388. showError(attacker, e);
  389. return;
  390. }
  391. showResult(attacker, res);
  392. }
  393. /**
  394. * @param killer the character that killed the {@code victim}
  395. * @param victim the character that was killed by the {@code killer}
  396. * @param qs the quest state object of the player to be notified of this event
  397. */
  398. public final void notifyDeath(L2Character killer, L2Character victim, QuestState qs)
  399. {
  400. String res = null;
  401. try
  402. {
  403. res = onDeath(killer, victim, qs);
  404. }
  405. catch (Exception e)
  406. {
  407. showError(qs.getPlayer(), e);
  408. }
  409. showResult(qs.getPlayer(), res);
  410. }
  411. /**
  412. * @param item
  413. * @param player
  414. */
  415. public final void notifyItemUse(L2Item item, L2PcInstance player)
  416. {
  417. String res = null;
  418. try
  419. {
  420. res = onItemUse(item, player);
  421. }
  422. catch (Exception e)
  423. {
  424. showError(player, e);
  425. }
  426. showResult(player, res);
  427. }
  428. /**
  429. * @param instance
  430. * @param player
  431. * @param skill
  432. */
  433. public final void notifySpellFinished(L2Npc instance, L2PcInstance player, L2Skill skill)
  434. {
  435. String res = null;
  436. try
  437. {
  438. res = onSpellFinished(instance, player, skill);
  439. }
  440. catch (Exception e)
  441. {
  442. showError(player, e);
  443. }
  444. showResult(player, res);
  445. }
  446. /**
  447. * Notify quest script when something happens with a trap.
  448. * @param trap the trap instance which triggers the notification
  449. * @param trigger the character which makes effect on the trap
  450. * @param action 0: trap casting its skill. 1: trigger detects the trap. 2: trigger removes the trap
  451. */
  452. public final void notifyTrapAction(L2TrapInstance trap, L2Character trigger, TrapAction action)
  453. {
  454. String res = null;
  455. try
  456. {
  457. res = onTrapAction(trap, trigger, action);
  458. }
  459. catch (Exception e)
  460. {
  461. if (trigger.getActingPlayer() != null)
  462. {
  463. showError(trigger.getActingPlayer(), e);
  464. }
  465. _log.log(Level.WARNING, "Exception on onTrapAction() in notifyTrapAction(): " + e.getMessage(), e);
  466. return;
  467. }
  468. if (trigger.getActingPlayer() != null)
  469. {
  470. showResult(trigger.getActingPlayer(), res);
  471. }
  472. }
  473. /**
  474. * @param npc the spawned NPC
  475. */
  476. public final void notifySpawn(L2Npc npc)
  477. {
  478. try
  479. {
  480. onSpawn(npc);
  481. }
  482. catch (Exception e)
  483. {
  484. _log.log(Level.WARNING, "Exception on onSpawn() in notifySpawn(): " + e.getMessage(), e);
  485. }
  486. }
  487. /**
  488. * @param event
  489. * @param npc
  490. * @param player
  491. * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
  492. */
  493. public final boolean notifyEvent(String event, L2Npc npc, L2PcInstance player)
  494. {
  495. String res = null;
  496. try
  497. {
  498. res = onAdvEvent(event, npc, player);
  499. }
  500. catch (Exception e)
  501. {
  502. return showError(player, e);
  503. }
  504. return showResult(player, res);
  505. }
  506. /**
  507. * @param player the player entering the world
  508. */
  509. public final void notifyEnterWorld(L2PcInstance player)
  510. {
  511. String res = null;
  512. try
  513. {
  514. res = onEnterWorld(player);
  515. }
  516. catch (Exception e)
  517. {
  518. showError(player, e);
  519. }
  520. showResult(player, res);
  521. }
  522. /**
  523. * @param npc
  524. * @param killer
  525. * @param isSummon
  526. */
  527. public final void notifyKill(L2Npc npc, L2PcInstance killer, boolean isSummon)
  528. {
  529. String res = null;
  530. try
  531. {
  532. res = onKill(npc, killer, isSummon);
  533. }
  534. catch (Exception e)
  535. {
  536. showError(killer, e);
  537. }
  538. showResult(killer, res);
  539. }
  540. /**
  541. * @param npc
  542. * @param qs
  543. * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
  544. */
  545. public final boolean notifyTalk(L2Npc npc, QuestState qs)
  546. {
  547. String res = null;
  548. try
  549. {
  550. res = onTalk(npc, qs.getPlayer());
  551. }
  552. catch (Exception e)
  553. {
  554. return showError(qs.getPlayer(), e);
  555. }
  556. qs.getPlayer().setLastQuestNpcObject(npc.getObjectId());
  557. return showResult(qs.getPlayer(), res);
  558. }
  559. /**
  560. * Override the default NPC dialogs when a quest defines this for the given NPC.<br>
  561. * Note: If the default html for this npc needs to be shown, onFirstTalk should call npc.showChatWindow(player) and then return null.
  562. * @param npc the NPC whose dialogs to override
  563. * @param player the player talking to the NPC
  564. */
  565. public final void notifyFirstTalk(L2Npc npc, L2PcInstance player)
  566. {
  567. String res = null;
  568. try
  569. {
  570. res = onFirstTalk(npc, player);
  571. }
  572. catch (Exception e)
  573. {
  574. showError(player, e);
  575. }
  576. showResult(player, res);
  577. }
  578. /**
  579. * @param npc
  580. * @param player
  581. */
  582. public final void notifyAcquireSkillList(L2Npc npc, L2PcInstance player)
  583. {
  584. String res = null;
  585. try
  586. {
  587. res = onAcquireSkillList(npc, player);
  588. }
  589. catch (Exception e)
  590. {
  591. showError(player, e);
  592. }
  593. showResult(player, res);
  594. }
  595. /**
  596. * Notify the quest engine that an skill info has been acquired.
  597. * @param npc the NPC
  598. * @param player the player
  599. * @param skill the skill
  600. */
  601. public final void notifyAcquireSkillInfo(L2Npc npc, L2PcInstance player, L2Skill skill)
  602. {
  603. String res = null;
  604. try
  605. {
  606. res = onAcquireSkillInfo(npc, player, skill);
  607. }
  608. catch (Exception e)
  609. {
  610. showError(player, e);
  611. }
  612. showResult(player, res);
  613. }
  614. /**
  615. * Notify the quest engine that an skill has been acquired.
  616. * @param npc the NPC
  617. * @param player the player
  618. * @param skill the skill
  619. * @param type the skill learn type
  620. */
  621. public final void notifyAcquireSkill(L2Npc npc, L2PcInstance player, L2Skill skill, AcquireSkillType type)
  622. {
  623. String res = null;
  624. try
  625. {
  626. res = onAcquireSkill(npc, player, skill, type);
  627. }
  628. catch (Exception e)
  629. {
  630. showError(player, e);
  631. }
  632. showResult(player, res);
  633. }
  634. /**
  635. * @param item
  636. * @param player
  637. * @return
  638. */
  639. public final boolean notifyItemTalk(L2ItemInstance item, L2PcInstance player)
  640. {
  641. String res = null;
  642. try
  643. {
  644. res = onItemTalk(item, player);
  645. if (res != null)
  646. {
  647. if (res.equalsIgnoreCase("true"))
  648. {
  649. return true;
  650. }
  651. else if (res.equalsIgnoreCase("false"))
  652. {
  653. return false;
  654. }
  655. }
  656. }
  657. catch (Exception e)
  658. {
  659. return showError(player, e);
  660. }
  661. return showResult(player, res);
  662. }
  663. /**
  664. * @param item
  665. * @param player
  666. * @return
  667. */
  668. public String onItemTalk(L2ItemInstance item, L2PcInstance player)
  669. {
  670. return null;
  671. }
  672. /**
  673. * @param item
  674. * @param player
  675. * @param event
  676. * @return
  677. */
  678. public final boolean notifyItemEvent(L2ItemInstance item, L2PcInstance player, String event)
  679. {
  680. String res = null;
  681. try
  682. {
  683. res = onItemEvent(item, player, event);
  684. if (res != null)
  685. {
  686. if (res.equalsIgnoreCase("true"))
  687. {
  688. return true;
  689. }
  690. else if (res.equalsIgnoreCase("false"))
  691. {
  692. return false;
  693. }
  694. }
  695. }
  696. catch (Exception e)
  697. {
  698. return showError(player, e);
  699. }
  700. return showResult(player, res);
  701. }
  702. /**
  703. * @param npc
  704. * @param caster
  705. * @param skill
  706. * @param targets
  707. * @param isSummon
  708. */
  709. public final void notifySkillSee(L2Npc npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isSummon)
  710. {
  711. ThreadPoolManager.getInstance().executeAi(new SkillSee(this, npc, caster, skill, targets, isSummon));
  712. }
  713. /**
  714. * @param npc
  715. * @param caller
  716. * @param attacker
  717. * @param isSummon
  718. */
  719. public final void notifyFactionCall(L2Npc npc, L2Npc caller, L2PcInstance attacker, boolean isSummon)
  720. {
  721. String res = null;
  722. try
  723. {
  724. res = onFactionCall(npc, caller, attacker, isSummon);
  725. }
  726. catch (Exception e)
  727. {
  728. showError(attacker, e);
  729. }
  730. showResult(attacker, res);
  731. }
  732. /**
  733. * @param npc
  734. * @param player
  735. * @param isSummon
  736. */
  737. public final void notifyAggroRangeEnter(L2Npc npc, L2PcInstance player, boolean isSummon)
  738. {
  739. ThreadPoolManager.getInstance().executeAi(new AggroRangeEnter(this, npc, player, isSummon));
  740. }
  741. /**
  742. * @param npc the NPC that sees the creature
  743. * @param creature the creature seen by the NPC
  744. * @param isSummon
  745. */
  746. public final void notifySeeCreature(L2Npc npc, L2Character creature, boolean isSummon)
  747. {
  748. ThreadPoolManager.getInstance().executeAi(new SeeCreature(this, npc, creature, isSummon));
  749. }
  750. /**
  751. * @param eventName - name of event
  752. * @param sender - NPC, who sent event
  753. * @param receiver - NPC, who received event
  754. * @param reference - L2Object to pass, if needed
  755. */
  756. public final void notifyEventReceived(String eventName, L2Npc sender, L2Npc receiver, L2Object reference)
  757. {
  758. try
  759. {
  760. onEventReceived(eventName, sender, receiver, reference);
  761. }
  762. catch (Exception e)
  763. {
  764. _log.log(Level.WARNING, "Exception on onEventReceived() in notifyEventReceived(): " + e.getMessage(), e);
  765. }
  766. }
  767. /**
  768. * @param character
  769. * @param zone
  770. */
  771. public final void notifyEnterZone(L2Character character, L2ZoneType zone)
  772. {
  773. L2PcInstance player = character.getActingPlayer();
  774. String res = null;
  775. try
  776. {
  777. res = onEnterZone(character, zone);
  778. }
  779. catch (Exception e)
  780. {
  781. if (player != null)
  782. {
  783. showError(player, e);
  784. }
  785. }
  786. if (player != null)
  787. {
  788. showResult(player, res);
  789. }
  790. }
  791. /**
  792. * @param character
  793. * @param zone
  794. */
  795. public final void notifyExitZone(L2Character character, L2ZoneType zone)
  796. {
  797. L2PcInstance player = character.getActingPlayer();
  798. String res = null;
  799. try
  800. {
  801. res = onExitZone(character, zone);
  802. }
  803. catch (Exception e)
  804. {
  805. if (player != null)
  806. {
  807. showError(player, e);
  808. }
  809. }
  810. if (player != null)
  811. {
  812. showResult(player, res);
  813. }
  814. }
  815. /**
  816. * @param winner
  817. * @param type {@code false} if there was an error, {@code true} otherwise
  818. */
  819. public final void notifyOlympiadWin(L2PcInstance winner, CompetitionType type)
  820. {
  821. try
  822. {
  823. onOlympiadWin(winner, type);
  824. }
  825. catch (Exception e)
  826. {
  827. showError(winner, e);
  828. }
  829. }
  830. /**
  831. * @param loser
  832. * @param type {@code false} if there was an error, {@code true} otherwise
  833. */
  834. public final void notifyOlympiadLose(L2PcInstance loser, CompetitionType type)
  835. {
  836. try
  837. {
  838. onOlympiadLose(loser, type);
  839. }
  840. catch (Exception e)
  841. {
  842. showError(loser, e);
  843. }
  844. }
  845. /**
  846. * @param npc
  847. */
  848. public final void notifyMoveFinished(L2Npc npc)
  849. {
  850. try
  851. {
  852. onMoveFinished(npc);
  853. }
  854. catch (Exception e)
  855. {
  856. _log.log(Level.WARNING, "Exception on onMoveFinished() in notifyMoveFinished(): " + e.getMessage(), e);
  857. }
  858. }
  859. /**
  860. * @param npc
  861. */
  862. public final void notifyNodeArrived(L2Npc npc)
  863. {
  864. try
  865. {
  866. onNodeArrived(npc);
  867. }
  868. catch (Exception e)
  869. {
  870. _log.log(Level.WARNING, "Exception on onNodeArrived() in notifyNodeArrived(): " + e.getMessage(), e);
  871. }
  872. }
  873. /**
  874. * @param npc
  875. */
  876. public final void notifyRouteFinished(L2Npc npc)
  877. {
  878. try
  879. {
  880. onRouteFinished(npc);
  881. }
  882. catch (Exception e)
  883. {
  884. _log.log(Level.WARNING, "Exception on onRouteFinished() in notifyRouteFinished(): " + e.getMessage(), e);
  885. }
  886. }
  887. // These are methods that java calls to invoke scripts.
  888. /**
  889. * This function is called in place of {@link #onAttack(L2Npc, L2PcInstance, int, boolean, L2Skill)} if the former is not implemented.<br>
  890. * If a script contains both onAttack(..) implementations, then this method will never be called unless the script's {@link #onAttack(L2Npc, L2PcInstance, int, boolean, L2Skill)} explicitly calls this method.
  891. * @param npc this parameter contains a reference to the exact instance of the NPC that got attacked the NPC.
  892. * @param attacker this parameter contains a reference to the exact instance of the player who attacked.
  893. * @param damage this parameter represents the total damage that this attack has inflicted to the NPC.
  894. * @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.
  895. * @return
  896. */
  897. public String onAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isSummon)
  898. {
  899. return null;
  900. }
  901. /**
  902. * This function is called whenever a player attacks an NPC that is registered for the quest.<br>
  903. * 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>
  904. * @param npc this parameter contains a reference to the exact instance of the NPC that got attacked.
  905. * @param attacker this parameter contains a reference to the exact instance of the player who attacked the NPC.
  906. * @param damage this parameter represents the total damage that this attack has inflicted to the NPC.
  907. * @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
  908. * @param skill parameter is the skill that player used to attack NPC.
  909. * @return
  910. */
  911. public String onAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isSummon, L2Skill skill)
  912. {
  913. return onAttack(npc, attacker, damage, isSummon);
  914. }
  915. /**
  916. * This function is called whenever an <b>exact instance</b> of a character who was previously registered for this event dies.<br>
  917. * 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.
  918. * @param killer this parameter contains a reference to the exact instance of the NPC that <b>killed</b> the character.
  919. * @param victim this parameter contains a reference to the exact instance of the character that got killed.
  920. * @param qs this parameter contains a reference to the QuestState of whomever was interested (waiting) for this kill.
  921. * @return
  922. */
  923. public String onDeath(L2Character killer, L2Character victim, QuestState qs)
  924. {
  925. return onAdvEvent("", ((killer instanceof L2Npc) ? ((L2Npc) killer) : null), qs.getPlayer());
  926. }
  927. /**
  928. * This function is called whenever a player clicks on a link in a quest dialog and whenever a timer fires.<br>
  929. * If is not overridden by a subclass, then default to the returned value of the simpler (and older) {@link #onEvent(String, QuestState)} override.<br>
  930. * If the player has a quest state, use it as parameter in the next call, otherwise return null.
  931. * @param event this parameter contains a string identifier for the event.<br>
  932. * Generally, this string is passed directly via the link.<br>
  933. * For example:<br>
  934. * <code>
  935. * &lt;a action="bypass -h Quest 626_ADarkTwilight 31517-01.htm"&gt;hello&lt;/a&gt;
  936. * </code><br>
  937. * The above link sets the event variable to "31517-01.htm" for the quest 626_ADarkTwilight.<br>
  938. * In the case of timers, this will be the name of the timer.<br>
  939. * This parameter serves as a sort of identifier.
  940. * @param npc this parameter contains a reference to the instance of NPC associated with this event.<br>
  941. * This may be the NPC registered in a timer, or the NPC with whom a player is speaking, etc.<br>
  942. * This parameter may be {@code null} in certain circumstances.
  943. * @param player this parameter contains a reference to the player participating in this function.<br>
  944. * It may be the player speaking to the NPC, or the player who caused a timer to start (and owns that timer).<br>
  945. * This parameter may be {@code null} in certain circumstances.
  946. * @return the text returned by the event (may be {@code null}, a filename or just text)
  947. */
  948. public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  949. {
  950. if (player != null)
  951. {
  952. final QuestState qs = player.getQuestState(getName());
  953. if (qs != null)
  954. {
  955. return onEvent(event, qs);
  956. }
  957. }
  958. return null;
  959. }
  960. /**
  961. * This function is called in place of {@link #onAdvEvent(String, L2Npc, L2PcInstance)} if the former is not implemented.<br>
  962. * 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.
  963. * @param event this parameter contains a string identifier for the event.<br>
  964. * Generally, this string is passed directly via the link.<br>
  965. * For example:<br>
  966. * <code>
  967. * &lt;a action="bypass -h Quest 626_ADarkTwilight 31517-01.htm"&gt;hello&lt;/a&gt;
  968. * </code><br>
  969. * The above link sets the event variable to "31517-01.htm" for the quest 626_ADarkTwilight.<br>
  970. * In the case of timers, this will be the name of the timer.<br>
  971. * This parameter serves as a sort of identifier.
  972. * @param qs this parameter contains a reference to the quest state of the player who used the link or started the timer.
  973. * @return the text returned by the event (may be {@code null}, a filename or just text)
  974. */
  975. public String onEvent(String event, QuestState qs)
  976. {
  977. return null;
  978. }
  979. /**
  980. * This function is called whenever a player kills a NPC that is registered for the quest.
  981. * @param npc this parameter contains a reference to the exact instance of the NPC that got killed.
  982. * @param killer this parameter contains a reference to the exact instance of the player who killed the NPC.
  983. * @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.
  984. * @return the text returned by the event (may be {@code null}, a filename or just text)
  985. */
  986. public String onKill(L2Npc npc, L2PcInstance killer, boolean isSummon)
  987. {
  988. return null;
  989. }
  990. /**
  991. * This function is called whenever a player clicks to the "Quest" link of an NPC that is registered for the quest.
  992. * @param npc this parameter contains a reference to the exact instance of the NPC that the player is talking with.
  993. * @param talker this parameter contains a reference to the exact instance of the player who is talking to the NPC.
  994. * @return the text returned by the event (may be {@code null}, a filename or just text)
  995. */
  996. public String onTalk(L2Npc npc, L2PcInstance talker)
  997. {
  998. return null;
  999. }
  1000. /**
  1001. * This function is called whenever a player talks to an NPC that is registered for the quest.<br>
  1002. * That is, it is triggered from the very first click on the NPC, not via another dialog.<br>
  1003. * <b>Note 1:</b><br>
  1004. * Each NPC can be registered to at most one quest for triggering this function.<br>
  1005. * In other words, the same one NPC cannot respond to an "onFirstTalk" request from two different quests.<br>
  1006. * Attempting to register an NPC in two different quests for this function will result in one of the two registration being ignored.<br>
  1007. * <b>Note 2:</b><br>
  1008. * Since a Quest link isn't clicked in order to reach this, a quest state can be invalid within this function.<br>
  1009. * The coder of the script may need to create a new quest state (if necessary).<br>
  1010. * <b>Note 3:</b><br>
  1011. * 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>
  1012. * If you wish to show the default HTML, within onFirstTalk do npc.showChatWindow(player) and then return ""<br>
  1013. * @param npc this parameter contains a reference to the exact instance of the NPC that the player is talking with.
  1014. * @param player this parameter contains a reference to the exact instance of the player who is talking to the NPC.
  1015. * @return the text returned by the event (may be {@code null}, a filename or just text)
  1016. * @since <a href="http://trac.l2jserver.com/changeset/771">Jython AI support for "onFirstTalk"</a>
  1017. */
  1018. public String onFirstTalk(L2Npc npc, L2PcInstance player)
  1019. {
  1020. return null;
  1021. }
  1022. /**
  1023. * @param item
  1024. * @param player
  1025. * @param event
  1026. * @return
  1027. */
  1028. public String onItemEvent(L2ItemInstance item, L2PcInstance player, String event)
  1029. {
  1030. return null;
  1031. }
  1032. /**
  1033. * This function is called whenever a player request a skill list.<br>
  1034. * TODO: Re-implement, since Skill Trees rework it's support was removed.
  1035. * @param npc this parameter contains a reference to the exact instance of the NPC that the player requested the skill list.
  1036. * @param player this parameter contains a reference to the exact instance of the player who requested the skill list.
  1037. * @return
  1038. */
  1039. public String onAcquireSkillList(L2Npc npc, L2PcInstance player)
  1040. {
  1041. return null;
  1042. }
  1043. /**
  1044. * This function is called whenever a player request a skill info.
  1045. * @param npc this parameter contains a reference to the exact instance of the NPC that the player requested the skill info.
  1046. * @param player this parameter contains a reference to the exact instance of the player who requested the skill info.
  1047. * @param skill this parameter contains a reference to the skill that the player requested its info.
  1048. * @return
  1049. */
  1050. public String onAcquireSkillInfo(L2Npc npc, L2PcInstance player, L2Skill skill)
  1051. {
  1052. return null;
  1053. }
  1054. /**
  1055. * This function is called whenever a player acquire a skill.<br>
  1056. * TODO: Re-implement, since Skill Trees rework it's support was removed.
  1057. * @param npc this parameter contains a reference to the exact instance of the NPC that the player requested the skill.
  1058. * @param player this parameter contains a reference to the exact instance of the player who requested the skill.
  1059. * @param skill this parameter contains a reference to the skill that the player requested.
  1060. * @param type the skill learn type
  1061. * @return
  1062. */
  1063. public String onAcquireSkill(L2Npc npc, L2PcInstance player, L2Skill skill, AcquireSkillType type)
  1064. {
  1065. return null;
  1066. }
  1067. /**
  1068. * This function is called whenever a player uses a quest item that has a quest events list.<br>
  1069. * TODO: complete this documentation and unhardcode it to work with all item uses not with those listed.
  1070. * @param item the quest item that the player used
  1071. * @param player the player who used the item
  1072. * @return
  1073. */
  1074. public String onItemUse(L2Item item, L2PcInstance player)
  1075. {
  1076. return null;
  1077. }
  1078. /**
  1079. * This function is called whenever a player casts a skill near a registered NPC (1000 distance).<br>
  1080. * <b>Note:</b><br>
  1081. * If a skill does damage, both onSkillSee(..) and onAttack(..) will be triggered for the damaged NPC!<br>
  1082. * However, only onSkillSee(..) will be triggered if the skill does no damage,<br>
  1083. * or if it damages an NPC who has no onAttack(..) registration while near another NPC who has an onSkillSee registration.<br>
  1084. * TODO: confirm if the distance is 1000 and unhardcode.
  1085. * @param npc the NPC that saw the skill
  1086. * @param caster the player who cast the skill
  1087. * @param skill the actual skill that was used
  1088. * @param targets an array of all objects (can be any type of object, including mobs and players) that were affected by the skill
  1089. * @param isSummon if {@code true}, the skill was actually cast by the player's summon, not the player himself
  1090. * @return
  1091. */
  1092. public String onSkillSee(L2Npc npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isSummon)
  1093. {
  1094. return null;
  1095. }
  1096. /**
  1097. * This function is called whenever an NPC finishes casting a skill.
  1098. * @param npc the NPC that casted the skill.
  1099. * @param player the player who is the target of the skill. Can be {@code null}.
  1100. * @param skill the actual skill that was used by the NPC.
  1101. * @return
  1102. */
  1103. public String onSpellFinished(L2Npc npc, L2PcInstance player, L2Skill skill)
  1104. {
  1105. return null;
  1106. }
  1107. /**
  1108. * This function is called whenever a trap action is performed.
  1109. * @param trap this parameter contains a reference to the exact instance of the trap that was activated.
  1110. * @param trigger this parameter contains a reference to the exact instance of the character that triggered the action.
  1111. * @param action this parameter contains a reference to the action that was triggered.
  1112. * @return
  1113. */
  1114. public String onTrapAction(L2TrapInstance trap, L2Character trigger, TrapAction action)
  1115. {
  1116. return null;
  1117. }
  1118. /**
  1119. * This function is called whenever an NPC spawns or re-spawns and passes a reference to the newly (re)spawned NPC.<br>
  1120. * Currently the only function that has no reference to a player.<br>
  1121. * It is useful for initializations, starting quest timers, displaying chat (NpcSay), and more.
  1122. * @param npc this parameter contains a reference to the exact instance of the NPC who just (re)spawned.
  1123. * @return
  1124. */
  1125. public String onSpawn(L2Npc npc)
  1126. {
  1127. return null;
  1128. }
  1129. /**
  1130. * This function is called whenever an NPC is called by another NPC in the same faction.
  1131. * @param npc this parameter contains a reference to the exact instance of the NPC who is being asked for help.
  1132. * @param caller this parameter contains a reference to the exact instance of the NPC who is asking for help.<br>
  1133. * @param attacker this parameter contains a reference to the exact instance of the player who attacked.
  1134. * @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.
  1135. * @return
  1136. */
  1137. public String onFactionCall(L2Npc npc, L2Npc caller, L2PcInstance attacker, boolean isSummon)
  1138. {
  1139. return null;
  1140. }
  1141. /**
  1142. * This function is called whenever a player enters an NPC aggression range.
  1143. * @param npc this parameter contains a reference to the exact instance of the NPC whose aggression range is being transgressed.
  1144. * @param player this parameter contains a reference to the exact instance of the player who is entering the NPC's aggression range.
  1145. * @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.
  1146. * @return
  1147. */
  1148. public String onAggroRangeEnter(L2Npc npc, L2PcInstance player, boolean isSummon)
  1149. {
  1150. return null;
  1151. }
  1152. /**
  1153. * This function is called whenever a NPC "sees" a creature.
  1154. * @param npc the NPC who sees the creature
  1155. * @param creature the creature seen by the NPC
  1156. * @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
  1157. * @return
  1158. */
  1159. public String onSeeCreature(L2Npc npc, L2Character creature, boolean isSummon)
  1160. {
  1161. return null;
  1162. }
  1163. /**
  1164. * This function is called whenever a player enters the game.
  1165. * @param player this parameter contains a reference to the exact instance of the player who is entering to the world.
  1166. * @return
  1167. */
  1168. public String onEnterWorld(L2PcInstance player)
  1169. {
  1170. return null;
  1171. }
  1172. /**
  1173. * This function is called whenever a character enters a registered zone.
  1174. * @param character this parameter contains a reference to the exact instance of the character who is entering the zone.
  1175. * @param zone this parameter contains a reference to the zone.
  1176. * @return
  1177. */
  1178. public String onEnterZone(L2Character character, L2ZoneType zone)
  1179. {
  1180. return null;
  1181. }
  1182. /**
  1183. * This function is called whenever a character exits a registered zone.
  1184. * @param character this parameter contains a reference to the exact instance of the character who is exiting the zone.
  1185. * @param zone this parameter contains a reference to the zone.
  1186. * @return
  1187. */
  1188. public String onExitZone(L2Character character, L2ZoneType zone)
  1189. {
  1190. return null;
  1191. }
  1192. /**
  1193. * @param eventName - name of event
  1194. * @param sender - NPC, who sent event
  1195. * @param receiver - NPC, who received event
  1196. * @param reference - L2Object to pass, if needed
  1197. * @return
  1198. */
  1199. public String onEventReceived(String eventName, L2Npc sender, L2Npc receiver, L2Object reference)
  1200. {
  1201. return null;
  1202. }
  1203. /**
  1204. * This function is called whenever a player wins an Olympiad Game.
  1205. * @param winner this parameter contains a reference to the exact instance of the player who won the competition.
  1206. * @param type this parameter contains a reference to the competition type.
  1207. */
  1208. public void onOlympiadWin(L2PcInstance winner, CompetitionType type)
  1209. {
  1210. }
  1211. /**
  1212. * This function is called whenever a player looses an Olympiad Game.
  1213. * @param loser this parameter contains a reference to the exact instance of the player who lose the competition.
  1214. * @param type this parameter contains a reference to the competition type.
  1215. */
  1216. public void onOlympiadLose(L2PcInstance loser, CompetitionType type)
  1217. {
  1218. }
  1219. /**
  1220. * This function is called whenever a NPC finishes moving
  1221. * @param npc registered NPC
  1222. */
  1223. public void onMoveFinished(L2Npc npc)
  1224. {
  1225. }
  1226. /**
  1227. * This function is called whenever a walker NPC (controlled by WalkingManager) arrive a walking node
  1228. * @param npc registered NPC
  1229. */
  1230. public void onNodeArrived(L2Npc npc)
  1231. {
  1232. }
  1233. /**
  1234. * This function is called whenever a walker NPC (controlled by WalkingManager) arrive to last node
  1235. * @param npc registered NPC
  1236. */
  1237. public void onRouteFinished(L2Npc npc)
  1238. {
  1239. }
  1240. /**
  1241. * Show an error message to the specified player.
  1242. * @param player the player to whom to send the error (must be a GM)
  1243. * @param t the {@link Throwable} to get the message/stacktrace from
  1244. * @return {@code false}
  1245. */
  1246. public boolean showError(L2PcInstance player, Throwable t)
  1247. {
  1248. _log.log(Level.WARNING, getScriptFile().getAbsolutePath(), t);
  1249. if (t.getMessage() == null)
  1250. {
  1251. _log.warning(getClass().getSimpleName() + ": " + t.getMessage());
  1252. }
  1253. if ((player != null) && player.getAccessLevel().isGm())
  1254. {
  1255. String res = "<html><body><title>Script error</title>" + Util.getStackTrace(t) + "</body></html>";
  1256. return showResult(player, res);
  1257. }
  1258. return false;
  1259. }
  1260. /**
  1261. * Show a message to the specified player.<br>
  1262. * <u><i>Concept:</i></u><br>
  1263. * Three cases are managed according to the value of the {@code res} parameter:<br>
  1264. * <ul>
  1265. * <li><u>{@code res} ends with ".htm" or ".html":</u> the contents of the specified HTML file are shown in a dialog window</li>
  1266. * <li><u>{@code res} starts with "&lt;html&gt;":</u> the contents of the parameter are shown in a dialog window</li>
  1267. * <li><u>all other cases :</u> the text contained in the parameter is shown in chat</li>
  1268. * </ul>
  1269. * @param player the player to whom to show the result
  1270. * @param res the message to show to the player
  1271. * @return {@code false} if the message was sent, {@code true} otherwise
  1272. */
  1273. public boolean showResult(L2PcInstance player, String res)
  1274. {
  1275. if ((res == null) || res.isEmpty() || (player == null))
  1276. {
  1277. return true;
  1278. }
  1279. if (res.endsWith(".htm") || res.endsWith(".html"))
  1280. {
  1281. showHtmlFile(player, res);
  1282. }
  1283. else if (res.startsWith("<html>"))
  1284. {
  1285. final NpcHtmlMessage npcReply = new NpcHtmlMessage();
  1286. npcReply.setHtml(res);
  1287. npcReply.replace("%playername%", player.getName());
  1288. player.sendPacket(npcReply);
  1289. player.sendPacket(ActionFailed.STATIC_PACKET);
  1290. }
  1291. else
  1292. {
  1293. player.sendMessage(res);
  1294. }
  1295. return false;
  1296. }
  1297. /**
  1298. * Loads all quest states and variables for the specified player.
  1299. * @param player the player who is entering the world
  1300. */
  1301. public static final void playerEnter(L2PcInstance player)
  1302. {
  1303. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1304. PreparedStatement invalidQuestData = con.prepareStatement("DELETE FROM character_quests WHERE charId = ? AND name = ?");
  1305. PreparedStatement invalidQuestDataVar = con.prepareStatement("DELETE FROM character_quests WHERE charId = ? AND name = ? AND var = ?");
  1306. PreparedStatement ps1 = con.prepareStatement("SELECT name, value FROM character_quests WHERE charId = ? AND var = ?"))
  1307. {
  1308. // Get list of quests owned by the player from database
  1309. ps1.setInt(1, player.getObjectId());
  1310. ps1.setString(2, "<state>");
  1311. try (ResultSet rs = ps1.executeQuery())
  1312. {
  1313. while (rs.next())
  1314. {
  1315. // Get the ID of the quest and Id of its state
  1316. String questId = rs.getString("name");
  1317. String statename = rs.getString("value");
  1318. // Search quest associated with the ID
  1319. Quest q = QuestManager.getInstance().getQuest(questId);
  1320. if (q == null)
  1321. {
  1322. _log.finer("Unknown quest " + questId + " for player " + player.getName());
  1323. if (Config.AUTODELETE_INVALID_QUEST_DATA)
  1324. {
  1325. invalidQuestData.setInt(1, player.getObjectId());
  1326. invalidQuestData.setString(2, questId);
  1327. invalidQuestData.executeUpdate();
  1328. }
  1329. continue;
  1330. }
  1331. // Create a new QuestState for the player that will be added to the player's list of quests
  1332. new QuestState(q, player, State.getStateId(statename));
  1333. }
  1334. }
  1335. // Get list of quests owned by the player from the DB in order to add variables used in the quest.
  1336. try (PreparedStatement ps2 = con.prepareStatement("SELECT name, var, value FROM character_quests WHERE charId = ? AND var <> ?"))
  1337. {
  1338. ps2.setInt(1, player.getObjectId());
  1339. ps2.setString(2, "<state>");
  1340. try (ResultSet rs = ps2.executeQuery())
  1341. {
  1342. while (rs.next())
  1343. {
  1344. String questId = rs.getString("name");
  1345. String var = rs.getString("var");
  1346. String value = rs.getString("value");
  1347. // Get the QuestState saved in the loop before
  1348. QuestState qs = player.getQuestState(questId);
  1349. if (qs == null)
  1350. {
  1351. _log.finer("Lost variable " + var + " in quest " + questId + " for player " + player.getName());
  1352. if (Config.AUTODELETE_INVALID_QUEST_DATA)
  1353. {
  1354. invalidQuestDataVar.setInt(1, player.getObjectId());
  1355. invalidQuestDataVar.setString(2, questId);
  1356. invalidQuestDataVar.setString(3, var);
  1357. invalidQuestDataVar.executeUpdate();
  1358. }
  1359. continue;
  1360. }
  1361. // Add parameter to the quest
  1362. qs.setInternal(var, value);
  1363. }
  1364. }
  1365. }
  1366. }
  1367. catch (Exception e)
  1368. {
  1369. _log.log(Level.WARNING, "could not insert char quest:", e);
  1370. }
  1371. // events
  1372. for (String name : _allEventsS.keySet())
  1373. {
  1374. player.processQuestEvent(name, "enter");
  1375. }
  1376. }
  1377. /**
  1378. * Insert (or update) in the database variables that need to stay persistent for this quest after a reboot.<br>
  1379. * This function is for storage of values that do not related to a specific player but are global for all characters.<br>
  1380. * 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.
  1381. * @param var the name of the variable to save
  1382. * @param value the value of the variable
  1383. */
  1384. public final void saveGlobalQuestVar(String var, String value)
  1385. {
  1386. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1387. PreparedStatement statement = con.prepareStatement("REPLACE INTO quest_global_data (quest_name,var,value) VALUES (?,?,?)"))
  1388. {
  1389. statement.setString(1, getName());
  1390. statement.setString(2, var);
  1391. statement.setString(3, value);
  1392. statement.executeUpdate();
  1393. }
  1394. catch (Exception e)
  1395. {
  1396. _log.log(Level.WARNING, "could not insert global quest variable:", e);
  1397. }
  1398. }
  1399. /**
  1400. * Read from the database a previously saved variable for this quest.<br>
  1401. * Due to performance considerations, this function should best be used only when the quest is first loaded.<br>
  1402. * Subclasses of this class can define structures into which these loaded values can be saved.<br>
  1403. * However, on-demand usage of this function throughout the script is not prohibited, only not recommended.<br>
  1404. * Values read from this function were entered by calls to "saveGlobalQuestVar".
  1405. * @param var the name of the variable to load
  1406. * @return the current value of the specified variable, or an empty string if the variable does not exist
  1407. */
  1408. public final String loadGlobalQuestVar(String var)
  1409. {
  1410. String result = "";
  1411. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1412. PreparedStatement statement = con.prepareStatement("SELECT value FROM quest_global_data WHERE quest_name = ? AND var = ?"))
  1413. {
  1414. statement.setString(1, getName());
  1415. statement.setString(2, var);
  1416. try (ResultSet rs = statement.executeQuery())
  1417. {
  1418. if (rs.first())
  1419. {
  1420. result = rs.getString(1);
  1421. }
  1422. }
  1423. }
  1424. catch (Exception e)
  1425. {
  1426. _log.log(Level.WARNING, "could not load global quest variable:", e);
  1427. }
  1428. return result;
  1429. }
  1430. /**
  1431. * Permanently delete from the database a global quest variable that was previously saved for this quest.
  1432. * @param var the name of the variable to delete
  1433. */
  1434. public final void deleteGlobalQuestVar(String var)
  1435. {
  1436. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1437. PreparedStatement statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ? AND var = ?"))
  1438. {
  1439. statement.setString(1, getName());
  1440. statement.setString(2, var);
  1441. statement.executeUpdate();
  1442. }
  1443. catch (Exception e)
  1444. {
  1445. _log.log(Level.WARNING, "could not delete global quest variable:", e);
  1446. }
  1447. }
  1448. /**
  1449. * Permanently delete from the database all global quest variables that were previously saved for this quest.
  1450. */
  1451. public final void deleteAllGlobalQuestVars()
  1452. {
  1453. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1454. PreparedStatement statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ?"))
  1455. {
  1456. statement.setString(1, getName());
  1457. statement.executeUpdate();
  1458. }
  1459. catch (Exception e)
  1460. {
  1461. _log.log(Level.WARNING, "could not delete global quest variables:", e);
  1462. }
  1463. }
  1464. /**
  1465. * Insert in the database the quest for the player.
  1466. * @param qs the {@link QuestState} object whose variable to insert
  1467. * @param var the name of the variable
  1468. * @param value the value of the variable
  1469. */
  1470. public static void createQuestVarInDb(QuestState qs, String var, String value)
  1471. {
  1472. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1473. PreparedStatement statement = con.prepareStatement("INSERT INTO character_quests (charId,name,var,value) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE value=?"))
  1474. {
  1475. statement.setInt(1, qs.getPlayer().getObjectId());
  1476. statement.setString(2, qs.getQuestName());
  1477. statement.setString(3, var);
  1478. statement.setString(4, value);
  1479. statement.setString(5, value);
  1480. statement.executeUpdate();
  1481. }
  1482. catch (Exception e)
  1483. {
  1484. _log.log(Level.WARNING, "could not insert char quest:", e);
  1485. }
  1486. }
  1487. /**
  1488. * Update the value of the variable "var" for the specified quest in database
  1489. * @param qs the {@link QuestState} object whose variable to update
  1490. * @param var the name of the variable
  1491. * @param value the value of the variable
  1492. */
  1493. public static void updateQuestVarInDb(QuestState qs, String var, String value)
  1494. {
  1495. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1496. PreparedStatement statement = con.prepareStatement("UPDATE character_quests SET value=? WHERE charId=? AND name=? AND var = ?"))
  1497. {
  1498. statement.setString(1, value);
  1499. statement.setInt(2, qs.getPlayer().getObjectId());
  1500. statement.setString(3, qs.getQuestName());
  1501. statement.setString(4, var);
  1502. statement.executeUpdate();
  1503. }
  1504. catch (Exception e)
  1505. {
  1506. _log.log(Level.WARNING, "could not update char quest:", e);
  1507. }
  1508. }
  1509. /**
  1510. * Delete a variable of player's quest from the database.
  1511. * @param qs the {@link QuestState} object whose variable to delete
  1512. * @param var the name of the variable to delete
  1513. */
  1514. public static void deleteQuestVarInDb(QuestState qs, String var)
  1515. {
  1516. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1517. PreparedStatement statement = con.prepareStatement("DELETE FROM character_quests WHERE charId=? AND name=? AND var=?"))
  1518. {
  1519. statement.setInt(1, qs.getPlayer().getObjectId());
  1520. statement.setString(2, qs.getQuestName());
  1521. statement.setString(3, var);
  1522. statement.executeUpdate();
  1523. }
  1524. catch (Exception e)
  1525. {
  1526. _log.log(Level.WARNING, "could not delete char quest:", e);
  1527. }
  1528. }
  1529. /**
  1530. * Delete from the database all variables and states of the specified quest state.
  1531. * @param qs the {@link QuestState} object whose variables to delete
  1532. * @param repeatable if {@code false}, the state variable will be preserved, otherwise it will be deleted as well
  1533. */
  1534. public static void deleteQuestInDb(QuestState qs, boolean repeatable)
  1535. {
  1536. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1537. PreparedStatement ps = con.prepareStatement(repeatable ? QUEST_DELETE_FROM_CHAR_QUERY : QUEST_DELETE_FROM_CHAR_QUERY_NON_REPEATABLE_QUERY))
  1538. {
  1539. ps.setInt(1, qs.getPlayer().getObjectId());
  1540. ps.setString(2, qs.getQuestName());
  1541. if (!repeatable)
  1542. {
  1543. ps.setString(3, "<state>");
  1544. }
  1545. ps.executeUpdate();
  1546. }
  1547. catch (Exception e)
  1548. {
  1549. _log.log(Level.WARNING, "could not delete char quest:", e);
  1550. }
  1551. }
  1552. /**
  1553. * Create a database record for the specified quest state.
  1554. * @param qs the {@link QuestState} object whose data to write in the database
  1555. */
  1556. public static void createQuestInDb(QuestState qs)
  1557. {
  1558. createQuestVarInDb(qs, "<state>", State.getStateName(qs.getState()));
  1559. }
  1560. /**
  1561. * Update informations regarding quest in database.<br>
  1562. * Actions:<br>
  1563. * <ul>
  1564. * <li>Get ID state of the quest recorded in object qs</li>
  1565. * <li>Test if quest is completed. If true, add a star (*) before the ID state</li>
  1566. * <li>Save in database the ID state (with or without the star) for the variable called "&lt;state&gt;" of the quest</li>
  1567. * </ul>
  1568. * @param qs the quest state
  1569. */
  1570. public static void updateQuestInDb(QuestState qs)
  1571. {
  1572. updateQuestVarInDb(qs, "<state>", State.getStateName(qs.getState()));
  1573. }
  1574. /**
  1575. * @param player the player whose language settings to use in finding the html of the right language
  1576. * @return the default html for when no quest is available: "You are either not on a quest that involves this NPC.."
  1577. */
  1578. public static String getNoQuestMsg(L2PcInstance player)
  1579. {
  1580. final String result = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/noquest.htm");
  1581. if ((result != null) && (result.length() > 0))
  1582. {
  1583. return result;
  1584. }
  1585. return DEFAULT_NO_QUEST_MSG;
  1586. }
  1587. /**
  1588. * @param player the player whose language settings to use in finding the html of the right language
  1589. * @return the default html for when no quest is already completed: "This quest has already been completed."
  1590. */
  1591. public static String getAlreadyCompletedMsg(L2PcInstance player)
  1592. {
  1593. final String result = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/alreadycompleted.htm");
  1594. if ((result != null) && (result.length() > 0))
  1595. {
  1596. return result;
  1597. }
  1598. return DEFAULT_ALREADY_COMPLETED_MSG;
  1599. }
  1600. /**
  1601. * Add this quest to the list of quests that the passed mob will respond to for the specified Event type.
  1602. * @param eventType type of event being registered
  1603. * @param npcId NPC ID to register
  1604. */
  1605. public void addEventId(QuestEventType eventType, int npcId)
  1606. {
  1607. try
  1608. {
  1609. final L2NpcTemplate t = NpcTable.getInstance().getTemplate(npcId);
  1610. if (t != null)
  1611. {
  1612. t.addQuestEvent(eventType, this);
  1613. _questInvolvedNpcs.add(npcId);
  1614. }
  1615. }
  1616. catch (Exception e)
  1617. {
  1618. _log.log(Level.WARNING, "Exception on addEventId(): " + e.getMessage(), e);
  1619. }
  1620. }
  1621. /**
  1622. * Add this quest to the list of quests that the passed mob will respond to for the specified Event type.
  1623. * @param eventType type of event being registered
  1624. * @param npcIds NPC IDs to register
  1625. */
  1626. public void addEventId(QuestEventType eventType, int... npcIds)
  1627. {
  1628. for (int npcId : npcIds)
  1629. {
  1630. addEventId(eventType, npcId);
  1631. }
  1632. }
  1633. /**
  1634. * Add this quest to the list of quests that the passed mob will respond to for the specified Event type.
  1635. * @param eventType type of event being registered
  1636. * @param npcIds NPC IDs to register
  1637. */
  1638. public void addEventId(QuestEventType eventType, Collection<Integer> npcIds)
  1639. {
  1640. for (int npcId : npcIds)
  1641. {
  1642. addEventId(eventType, npcId);
  1643. }
  1644. }
  1645. // TODO: Remove after all Jython scripts are replaced with Java versions.
  1646. public void addStartNpc(int npcId)
  1647. {
  1648. addEventId(QuestEventType.QUEST_START, npcId);
  1649. }
  1650. public void addFirstTalkId(int npcId)
  1651. {
  1652. addEventId(QuestEventType.ON_FIRST_TALK, npcId);
  1653. }
  1654. public void addTalkId(int npcId)
  1655. {
  1656. addEventId(QuestEventType.ON_TALK, npcId);
  1657. }
  1658. public void addKillId(int killId)
  1659. {
  1660. addEventId(QuestEventType.ON_KILL, killId);
  1661. }
  1662. public void addAttackId(int npcId)
  1663. {
  1664. addEventId(QuestEventType.ON_ATTACK, npcId);
  1665. }
  1666. /**
  1667. * Add the quest to the NPC's startQuest
  1668. * @param npcIds
  1669. */
  1670. public void addStartNpc(int... npcIds)
  1671. {
  1672. addEventId(QuestEventType.QUEST_START, npcIds);
  1673. }
  1674. /**
  1675. * Add the quest to the NPC's startQuest
  1676. * @param npcIds
  1677. */
  1678. public void addStartNpc(Collection<Integer> npcIds)
  1679. {
  1680. addEventId(QuestEventType.QUEST_START, npcIds);
  1681. }
  1682. /**
  1683. * Add the quest to the NPC's first-talk (default action dialog)
  1684. * @param npcIds
  1685. */
  1686. public void addFirstTalkId(int... npcIds)
  1687. {
  1688. addEventId(QuestEventType.ON_FIRST_TALK, npcIds);
  1689. }
  1690. /**
  1691. * Add the quest to the NPC's first-talk (default action dialog)
  1692. * @param npcIds
  1693. */
  1694. public void addFirstTalkId(Collection<Integer> npcIds)
  1695. {
  1696. addEventId(QuestEventType.ON_FIRST_TALK, npcIds);
  1697. }
  1698. /**
  1699. * Add the NPC to the AcquireSkill dialog
  1700. * @param npcIds
  1701. */
  1702. public void addAcquireSkillId(int... npcIds)
  1703. {
  1704. addEventId(QuestEventType.ON_SKILL_LEARN, npcIds);
  1705. }
  1706. /**
  1707. * Add the NPC to the AcquireSkill dialog
  1708. * @param npcIds
  1709. */
  1710. public void addAcquireSkillId(Collection<Integer> npcIds)
  1711. {
  1712. addEventId(QuestEventType.ON_SKILL_LEARN, npcIds);
  1713. }
  1714. /**
  1715. * Add this quest to the list of quests that the passed mob will respond to for Attack Events.
  1716. * @param npcIds
  1717. */
  1718. public void addAttackId(int... npcIds)
  1719. {
  1720. addEventId(QuestEventType.ON_ATTACK, npcIds);
  1721. }
  1722. /**
  1723. * Add this quest to the list of quests that the passed mob will respond to for Attack Events.
  1724. * @param npcIds
  1725. */
  1726. public void addAttackId(Collection<Integer> npcIds)
  1727. {
  1728. addEventId(QuestEventType.ON_ATTACK, npcIds);
  1729. }
  1730. /**
  1731. * Add this quest to the list of quests that the passed mob will respond to for Kill Events.
  1732. * @param killIds
  1733. */
  1734. public void addKillId(int... killIds)
  1735. {
  1736. addEventId(QuestEventType.ON_KILL, killIds);
  1737. }
  1738. /**
  1739. * Add this quest event to the collection of NPC IDs that will respond to for on kill events.
  1740. * @param killIds the collection of NPC IDs
  1741. */
  1742. public void addKillId(Collection<Integer> killIds)
  1743. {
  1744. addEventId(QuestEventType.ON_KILL, killIds);
  1745. }
  1746. /**
  1747. * Add this quest to the list of quests that the passed npc will respond to for Talk Events.
  1748. * @param npcIds Id of the NPC
  1749. */
  1750. public void addTalkId(int... npcIds)
  1751. {
  1752. addEventId(QuestEventType.ON_TALK, npcIds);
  1753. }
  1754. public void addTalkId(Collection<Integer> npcIds)
  1755. {
  1756. addEventId(QuestEventType.ON_TALK, npcIds);
  1757. }
  1758. /**
  1759. * Add this quest to the list of quests that the passed npc will respond to for Spawn Events.
  1760. * @param npcIds Id of the NPC
  1761. */
  1762. public void addSpawnId(int... npcIds)
  1763. {
  1764. addEventId(QuestEventType.ON_SPAWN, npcIds);
  1765. }
  1766. /**
  1767. * Add this quest to the list of quests that the passed npc will respond to for Spawn Events.
  1768. * @param npcIds ID of the NPC
  1769. */
  1770. public void addSpawnId(Collection<Integer> npcIds)
  1771. {
  1772. addEventId(QuestEventType.ON_SPAWN, npcIds);
  1773. }
  1774. /**
  1775. * Add this quest to the list of quests that the passed npc will respond to for Skill-See Events.
  1776. * @param npcIds ID of the NPC
  1777. */
  1778. public void addSkillSeeId(int... npcIds)
  1779. {
  1780. addEventId(QuestEventType.ON_SKILL_SEE, npcIds);
  1781. }
  1782. /**
  1783. * Add this quest to the list of quests that the passed npc will respond to for Skill-See Events.
  1784. * @param npcIds ID of the NPC
  1785. */
  1786. public void addSkillSeeId(Collection<Integer> npcIds)
  1787. {
  1788. addEventId(QuestEventType.ON_SKILL_SEE, npcIds);
  1789. }
  1790. /**
  1791. * @param npcIds
  1792. */
  1793. public void addSpellFinishedId(int... npcIds)
  1794. {
  1795. addEventId(QuestEventType.ON_SPELL_FINISHED, npcIds);
  1796. }
  1797. /**
  1798. * @param npcIds
  1799. */
  1800. public void addSpellFinishedId(Collection<Integer> npcIds)
  1801. {
  1802. addEventId(QuestEventType.ON_SPELL_FINISHED, npcIds);
  1803. }
  1804. /**
  1805. * @param npcIds
  1806. */
  1807. public void addTrapActionId(int... npcIds)
  1808. {
  1809. addEventId(QuestEventType.ON_TRAP_ACTION, npcIds);
  1810. }
  1811. /**
  1812. * @param npcIds
  1813. */
  1814. public void addTrapActionId(Collection<Integer> npcIds)
  1815. {
  1816. addEventId(QuestEventType.ON_TRAP_ACTION, npcIds);
  1817. }
  1818. /**
  1819. * Add this quest to the list of quests that the passed npc will respond to for Faction Call Events.
  1820. * @param npcIds ID of the NPC
  1821. */
  1822. public void addFactionCallId(int... npcIds)
  1823. {
  1824. addEventId(QuestEventType.ON_FACTION_CALL, npcIds);
  1825. }
  1826. /**
  1827. * Add this quest to the list of quests that the passed npc will respond to for Faction Call Events.
  1828. * @param npcIds ID of the NPC
  1829. */
  1830. public void addFactionCallId(Collection<Integer> npcIds)
  1831. {
  1832. addEventId(QuestEventType.ON_FACTION_CALL, npcIds);
  1833. }
  1834. /**
  1835. * Add this quest to the list of quests that the passed npc will respond to for Character See Events.
  1836. * @param npcIds ID of the NPC
  1837. */
  1838. public void addAggroRangeEnterId(int... npcIds)
  1839. {
  1840. addEventId(QuestEventType.ON_AGGRO_RANGE_ENTER, npcIds);
  1841. }
  1842. /**
  1843. * Add this quest to the list of quests that the passed npc will respond to for Character See Events.
  1844. * @param npcIds ID of the NPC
  1845. */
  1846. public void addAggroRangeEnterId(Collection<Integer> npcIds)
  1847. {
  1848. addEventId(QuestEventType.ON_AGGRO_RANGE_ENTER, npcIds);
  1849. }
  1850. /**
  1851. * @param npcIds NPC IDs to register to on see creature event
  1852. */
  1853. public void addSeeCreatureId(int... npcIds)
  1854. {
  1855. addEventId(QuestEventType.ON_SEE_CREATURE, npcIds);
  1856. }
  1857. /**
  1858. * @param npcIds NPC IDs to register to on see creature event
  1859. */
  1860. public void addSeeCreatureId(Collection<Integer> npcIds)
  1861. {
  1862. addEventId(QuestEventType.ON_SEE_CREATURE, npcIds);
  1863. }
  1864. /**
  1865. * Register onEnterZone trigger for Zone
  1866. * @param zoneId
  1867. */
  1868. public void addEnterZoneId(int zoneId)
  1869. {
  1870. final L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
  1871. if (zone != null)
  1872. {
  1873. zone.addQuestEvent(QuestEventType.ON_ENTER_ZONE, this);
  1874. }
  1875. }
  1876. /**
  1877. * Register onEnterZone trigger for Zones
  1878. * @param zoneIds
  1879. */
  1880. public void addEnterZoneId(int... zoneIds)
  1881. {
  1882. for (int zoneId : zoneIds)
  1883. {
  1884. addEnterZoneId(zoneId);
  1885. }
  1886. }
  1887. /**
  1888. * Register onEnterZone trigger for Zones
  1889. * @param zoneIds
  1890. */
  1891. public void addEnterZoneId(Collection<Integer> zoneIds)
  1892. {
  1893. for (int zoneId : zoneIds)
  1894. {
  1895. addEnterZoneId(zoneId);
  1896. }
  1897. }
  1898. /**
  1899. * Register onExitZone trigger for Zone
  1900. * @param zoneId
  1901. */
  1902. public void addExitZoneId(int zoneId)
  1903. {
  1904. L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
  1905. if (zone != null)
  1906. {
  1907. zone.addQuestEvent(QuestEventType.ON_EXIT_ZONE, this);
  1908. }
  1909. }
  1910. /**
  1911. * Register onExitZone trigger for Zones
  1912. * @param zoneIds
  1913. */
  1914. public void addExitZoneId(int... zoneIds)
  1915. {
  1916. for (int zoneId : zoneIds)
  1917. {
  1918. addExitZoneId(zoneId);
  1919. }
  1920. }
  1921. /**
  1922. * Register onExitZone trigger for Zones
  1923. * @param zoneIds
  1924. */
  1925. public void addExitZoneId(Collection<Integer> zoneIds)
  1926. {
  1927. for (int zoneId : zoneIds)
  1928. {
  1929. addExitZoneId(zoneId);
  1930. }
  1931. }
  1932. /**
  1933. * Register onEventReceived trigger for NPC
  1934. * @param npcIds
  1935. */
  1936. public void addEventReceivedId(int... npcIds)
  1937. {
  1938. addEventId(QuestEventType.ON_EVENT_RECEIVED, npcIds);
  1939. }
  1940. /**
  1941. * Register onEventReceived trigger for NPC
  1942. * @param npcIds
  1943. */
  1944. public void addEventReceivedId(Collection<Integer> npcIds)
  1945. {
  1946. addEventId(QuestEventType.ON_EVENT_RECEIVED, npcIds);
  1947. }
  1948. /**
  1949. * Register onMoveFinished trigger for NPC
  1950. * @param npcIds
  1951. */
  1952. public void addMoveFinishedId(int... npcIds)
  1953. {
  1954. addEventId(QuestEventType.ON_MOVE_FINISHED, npcIds);
  1955. }
  1956. /**
  1957. * Register onMoveFinished trigger for NPC
  1958. * @param npcIds
  1959. */
  1960. public void addMoveFinishedId(Collection<Integer> npcIds)
  1961. {
  1962. addEventId(QuestEventType.ON_MOVE_FINISHED, npcIds);
  1963. }
  1964. /**
  1965. * Register onNodeArrived trigger for NPC
  1966. * @param npcIds ID of NPC to register
  1967. */
  1968. public void addNodeArrivedId(int... npcIds)
  1969. {
  1970. addEventId(QuestEventType.ON_NODE_ARRIVED, npcIds);
  1971. }
  1972. /**
  1973. * Register onNodeArrived trigger for NPC
  1974. * @param npcIds ID of NPC to register
  1975. */
  1976. public void addNodeArrivedId(Collection<Integer> npcIds)
  1977. {
  1978. addEventId(QuestEventType.ON_NODE_ARRIVED, npcIds);
  1979. }
  1980. /**
  1981. * Register onRouteFinished trigger for NPC
  1982. * @param npcIds ID of NPC to register
  1983. */
  1984. public void addRouteFinishedId(int... npcIds)
  1985. {
  1986. addEventId(QuestEventType.ON_ROUTE_FINISHED, npcIds);
  1987. }
  1988. /**
  1989. * Register onRouteFinished trigger for NPC
  1990. * @param npcIds ID of NPC to register
  1991. */
  1992. public void addRouteFinishedId(Collection<Integer> npcIds)
  1993. {
  1994. addEventId(QuestEventType.ON_ROUTE_FINISHED, npcIds);
  1995. }
  1996. /**
  1997. * Use this method to get a random party member from a player's party.<br>
  1998. * Useful when distributing rewards after killing an NPC.
  1999. * @param player this parameter represents the player whom the party will taken.
  2000. * @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
  2001. */
  2002. public L2PcInstance getRandomPartyMember(L2PcInstance player)
  2003. {
  2004. if (player == null)
  2005. {
  2006. return null;
  2007. }
  2008. final L2Party party = player.getParty();
  2009. if ((party == null) || (party.getMembers().isEmpty()))
  2010. {
  2011. return player;
  2012. }
  2013. return party.getMembers().get(Rnd.get(party.getMembers().size()));
  2014. }
  2015. /**
  2016. * Get a random party member with required cond value.
  2017. * @param player the instance of a player whose party is to be searched
  2018. * @param cond the value of the "cond" variable that must be matched
  2019. * @return a random party member that matches the specified condition, or {@code null} if no match was found
  2020. */
  2021. public L2PcInstance getRandomPartyMember(L2PcInstance player, int cond)
  2022. {
  2023. return getRandomPartyMember(player, "cond", String.valueOf(cond));
  2024. }
  2025. /**
  2026. * Auxiliary function for party quests.<br>
  2027. * Note: This function is only here because of how commonly it may be used by quest developers.<br>
  2028. * For any variations on this function, the quest script can always handle things on its own.
  2029. * @param player the instance of a player whose party is to be searched
  2030. * @param var the quest variable to look for in party members. If {@code null}, it simply unconditionally returns a random party member
  2031. * @param value the value of the specified quest variable the random party member must have
  2032. * @return a random party member that matches the specified conditions or {@code null} if no match was found.<br>
  2033. * If the {@code var} parameter is {@code null}, a random party member is selected without any conditions.<br>
  2034. * 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
  2035. */
  2036. public L2PcInstance getRandomPartyMember(L2PcInstance player, String var, String value)
  2037. {
  2038. // if no valid player instance is passed, there is nothing to check...
  2039. if (player == null)
  2040. {
  2041. return null;
  2042. }
  2043. // for null var condition, return any random party member.
  2044. if (var == null)
  2045. {
  2046. return getRandomPartyMember(player);
  2047. }
  2048. // normal cases...if the player is not in a party, check the player's state
  2049. QuestState temp = null;
  2050. L2Party party = player.getParty();
  2051. // if this player is not in a party, just check if this player instance matches the conditions itself
  2052. if ((party == null) || (party.getMembers().isEmpty()))
  2053. {
  2054. temp = player.getQuestState(getName());
  2055. if ((temp != null) && temp.isSet(var) && temp.get(var).equalsIgnoreCase(value))
  2056. {
  2057. return player; // match
  2058. }
  2059. return null; // no match
  2060. }
  2061. // if the player is in a party, gather a list of all matching party members (possibly including this player)
  2062. List<L2PcInstance> candidates = new ArrayList<>();
  2063. // get the target for enforcing distance limitations.
  2064. L2Object target = player.getTarget();
  2065. if (target == null)
  2066. {
  2067. target = player;
  2068. }
  2069. for (L2PcInstance partyMember : party.getMembers())
  2070. {
  2071. if (partyMember == null)
  2072. {
  2073. continue;
  2074. }
  2075. temp = partyMember.getQuestState(getName());
  2076. if ((temp != null) && (temp.get(var) != null) && (temp.get(var)).equalsIgnoreCase(value) && partyMember.isInsideRadius(target, 1500, true, false))
  2077. {
  2078. candidates.add(partyMember);
  2079. }
  2080. }
  2081. // if there was no match, return null...
  2082. if (candidates.isEmpty())
  2083. {
  2084. return null;
  2085. }
  2086. // if a match was found from the party, return one of them at random.
  2087. return candidates.get(Rnd.get(candidates.size()));
  2088. }
  2089. /**
  2090. * Auxiliary function for party quests.<br>
  2091. * Note: This function is only here because of how commonly it may be used by quest developers.<br>
  2092. * For any variations on this function, the quest script can always handle things on its own.
  2093. * @param player the player whose random party member is to be selected
  2094. * @param state the quest state required of the random party member
  2095. * @return {@code null} if nothing was selected or a random party member that has the specified quest state
  2096. */
  2097. public L2PcInstance getRandomPartyMemberState(L2PcInstance player, byte state)
  2098. {
  2099. // if no valid player instance is passed, there is nothing to check...
  2100. if (player == null)
  2101. {
  2102. return null;
  2103. }
  2104. // normal cases...if the player is not in a party check the player's state
  2105. QuestState temp = null;
  2106. L2Party party = player.getParty();
  2107. // if this player is not in a party, just check if this player instance matches the conditions itself
  2108. if ((party == null) || (party.getMembers().isEmpty()))
  2109. {
  2110. temp = player.getQuestState(getName());
  2111. if ((temp != null) && (temp.getState() == state))
  2112. {
  2113. return player; // match
  2114. }
  2115. return null; // no match
  2116. }
  2117. // if the player is in a party, gather a list of all matching party members (possibly
  2118. // including this player)
  2119. List<L2PcInstance> candidates = new ArrayList<>();
  2120. // get the target for enforcing distance limitations.
  2121. L2Object target = player.getTarget();
  2122. if (target == null)
  2123. {
  2124. target = player;
  2125. }
  2126. for (L2PcInstance partyMember : party.getMembers())
  2127. {
  2128. if (partyMember == null)
  2129. {
  2130. continue;
  2131. }
  2132. temp = partyMember.getQuestState(getName());
  2133. if ((temp != null) && (temp.getState() == state) && partyMember.isInsideRadius(target, 1500, true, false))
  2134. {
  2135. candidates.add(partyMember);
  2136. }
  2137. }
  2138. // if there was no match, return null...
  2139. if (candidates.isEmpty())
  2140. {
  2141. return null;
  2142. }
  2143. // if a match was found from the party, return one of them at random.
  2144. return candidates.get(Rnd.get(candidates.size()));
  2145. }
  2146. /**
  2147. * Get a random party member from the player's party who has this quest at the specified quest progress.<br>
  2148. * If the player is not in a party, only the player himself is checked.
  2149. * @param player the player whose random party member state to get
  2150. * @param condition the quest progress step the random member should be at (-1 = check only if quest is started)
  2151. * @param playerChance how many times more chance does the player get compared to other party members (3 - 3x more chance).<br>
  2152. * On retail servers, the killer usually gets 2-3x more chance than other party members
  2153. * @param target the NPC to use for the distance check (can be null)
  2154. * @return the {@link QuestState} object of the random party member or {@code null} if none matched the condition
  2155. */
  2156. public QuestState getRandomPartyMemberState(L2PcInstance player, int condition, int playerChance, L2Npc target)
  2157. {
  2158. if ((player == null) || (playerChance < 1))
  2159. {
  2160. return null;
  2161. }
  2162. QuestState qs = player.getQuestState(getName());
  2163. if (!player.isInParty())
  2164. {
  2165. if (!checkPartyMemberConditions(qs, condition))
  2166. {
  2167. return null;
  2168. }
  2169. if (!checkDistanceToTarget(player, target))
  2170. {
  2171. return null;
  2172. }
  2173. return qs;
  2174. }
  2175. final List<QuestState> candidates = new ArrayList<>();
  2176. if (checkPartyMemberConditions(qs, condition) && (playerChance > 0))
  2177. {
  2178. for (int i = 0; i < playerChance; i++)
  2179. {
  2180. candidates.add(qs);
  2181. }
  2182. }
  2183. for (L2PcInstance member : player.getParty().getMembers())
  2184. {
  2185. if (member == player)
  2186. {
  2187. continue;
  2188. }
  2189. qs = member.getQuestState(getName());
  2190. if (checkPartyMemberConditions(qs, condition))
  2191. {
  2192. candidates.add(qs);
  2193. }
  2194. }
  2195. if (candidates.isEmpty())
  2196. {
  2197. return null;
  2198. }
  2199. qs = candidates.get(getRandom(candidates.size()));
  2200. if (!checkDistanceToTarget(qs.getPlayer(), target))
  2201. {
  2202. return null;
  2203. }
  2204. return qs;
  2205. }
  2206. private static boolean checkPartyMemberConditions(QuestState qs, int condition)
  2207. {
  2208. return ((qs != null) && ((condition == -1) ? qs.isStarted() : qs.isCond(condition)));
  2209. }
  2210. private static boolean checkDistanceToTarget(L2PcInstance player, L2Npc target)
  2211. {
  2212. return ((target == null) || com.l2jserver.gameserver.util.Util.checkIfInRange(1500, player, target, true));
  2213. }
  2214. /**
  2215. * Show an on screen message to the player.
  2216. * @param player the player to display the message
  2217. * @param text the message
  2218. * @param time the display time
  2219. */
  2220. public static void showOnScreenMsg(L2PcInstance player, String text, int time)
  2221. {
  2222. player.sendPacket(new ExShowScreenMessage(text, time));
  2223. }
  2224. /**
  2225. * Show an on screen message to the player.
  2226. * @param player the player to display the message
  2227. * @param npcString the NPC String to display
  2228. * @param position the position in the screen
  2229. * @param time the display time
  2230. * @param params parameters values to replace in the NPC String
  2231. */
  2232. public static void showOnScreenMsg(L2PcInstance player, NpcStringId npcString, int position, int time, String... params)
  2233. {
  2234. player.sendPacket(new ExShowScreenMessage(npcString, position, time, params));
  2235. }
  2236. /**
  2237. * Show an on screen message to the player.
  2238. * @param player the player to display the message
  2239. * @param systemMsg the System Message to display
  2240. * @param position the position in the screen
  2241. * @param time the display time
  2242. * @param params parameters values to replace in the System Message
  2243. */
  2244. public static void showOnScreenMsg(L2PcInstance player, SystemMessageId systemMsg, int position, int time, String... params)
  2245. {
  2246. player.sendPacket(new ExShowScreenMessage(systemMsg, position, time, params));
  2247. }
  2248. /**
  2249. * Show HTML file to client
  2250. * @param player
  2251. * @param fileName
  2252. * @return String message sent to client
  2253. */
  2254. public String showHtmlFile(L2PcInstance player, String fileName)
  2255. {
  2256. boolean questwindow = !fileName.endsWith(".html");
  2257. int questId = getId();
  2258. // Create handler to file linked to the quest
  2259. String content = getHtm(player.getHtmlPrefix(), fileName);
  2260. // Send message to client if message not empty
  2261. if (content != null)
  2262. {
  2263. if (player.getTarget() != null)
  2264. {
  2265. content = content.replaceAll("%objectId%", Integer.toString(player.getTargetId()));
  2266. }
  2267. if (questwindow && (questId > 0) && (questId < 20000) && (questId != 999))
  2268. {
  2269. NpcQuestHtmlMessage npcReply = new NpcQuestHtmlMessage(player.getTargetId(), questId);
  2270. npcReply.setHtml(content);
  2271. npcReply.replace("%playername%", player.getName());
  2272. player.sendPacket(npcReply);
  2273. }
  2274. else
  2275. {
  2276. final NpcHtmlMessage npcReply = new NpcHtmlMessage(player.getTargetId());
  2277. npcReply.setHtml(content);
  2278. npcReply.replace("%playername%", player.getName());
  2279. player.sendPacket(npcReply);
  2280. }
  2281. player.sendPacket(ActionFailed.STATIC_PACKET);
  2282. }
  2283. return content;
  2284. }
  2285. /**
  2286. * @param prefix player's language prefix.
  2287. * @param fileName the html file to be get.
  2288. * @return the HTML file contents
  2289. */
  2290. public String getHtm(String prefix, String fileName)
  2291. {
  2292. final HtmCache hc = HtmCache.getInstance();
  2293. String content = hc.getHtm(prefix, fileName.startsWith("data/") ? fileName : "data/scripts/" + getDescr().toLowerCase() + "/" + getName() + "/" + fileName);
  2294. if (content == null)
  2295. {
  2296. content = hc.getHtm(prefix, "data/scripts/" + getDescr() + "/" + getName() + "/" + fileName);
  2297. if (content == null)
  2298. {
  2299. content = hc.getHtmForce(prefix, "data/scripts/quests/" + getName() + "/" + fileName);
  2300. }
  2301. }
  2302. return content;
  2303. }
  2304. /**
  2305. * Add a temporary (quest) spawn
  2306. * @param npcId
  2307. * @param cha
  2308. * @return instance of newly spawned npc
  2309. */
  2310. public static L2Npc addSpawn(int npcId, L2Character cha)
  2311. {
  2312. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), false, 0, false, 0);
  2313. }
  2314. /**
  2315. * Add a temporary (quest) spawn
  2316. * @param npcId
  2317. * @param cha
  2318. * @param isSummonSpawn
  2319. * @return instance of newly spawned npc with summon animation
  2320. */
  2321. public static L2Npc addSpawn(int npcId, L2Character cha, boolean isSummonSpawn)
  2322. {
  2323. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), false, 0, isSummonSpawn, 0);
  2324. }
  2325. /**
  2326. * @param npcId
  2327. * @param x
  2328. * @param y
  2329. * @param z
  2330. * @param heading
  2331. * @param randomOffSet
  2332. * @param despawnDelay
  2333. * @return
  2334. */
  2335. public static L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffSet, long despawnDelay)
  2336. {
  2337. return addSpawn(npcId, x, y, z, heading, randomOffSet, despawnDelay, false, 0);
  2338. }
  2339. /**
  2340. * @param npcId
  2341. * @param loc
  2342. * @param randomOffSet
  2343. * @param despawnDelay
  2344. * @return
  2345. */
  2346. public static L2Npc addSpawn(int npcId, Location loc, boolean randomOffSet, long despawnDelay)
  2347. {
  2348. return addSpawn(npcId, loc.getX(), loc.getY(), loc.getZ(), loc.getHeading(), randomOffSet, despawnDelay, false, 0);
  2349. }
  2350. /**
  2351. * @param npcId
  2352. * @param x
  2353. * @param y
  2354. * @param z
  2355. * @param heading
  2356. * @param randomOffset
  2357. * @param despawnDelay
  2358. * @param isSummonSpawn
  2359. * @return
  2360. */
  2361. public static L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, long despawnDelay, boolean isSummonSpawn)
  2362. {
  2363. return addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, isSummonSpawn, 0);
  2364. }
  2365. /**
  2366. * @param npcId
  2367. * @param loc
  2368. * @param randomOffset
  2369. * @param despawnDelay
  2370. * @param isSummonSpawn
  2371. * @return
  2372. */
  2373. public static L2Npc addSpawn(int npcId, Location loc, boolean randomOffset, long despawnDelay, boolean isSummonSpawn)
  2374. {
  2375. return addSpawn(npcId, loc.getX(), loc.getY(), loc.getZ(), loc.getHeading(), randomOffset, despawnDelay, isSummonSpawn, 0);
  2376. }
  2377. /**
  2378. * @param npcId
  2379. * @param loc
  2380. * @param randomOffset
  2381. * @param despawnDelay
  2382. * @param isSummonSpawn
  2383. * @param instanceId
  2384. * @return
  2385. */
  2386. public static L2Npc addSpawn(int npcId, Location loc, boolean randomOffset, long despawnDelay, boolean isSummonSpawn, int instanceId)
  2387. {
  2388. return addSpawn(npcId, loc.getX(), loc.getY(), loc.getZ(), loc.getHeading(), randomOffset, despawnDelay, isSummonSpawn, instanceId);
  2389. }
  2390. /**
  2391. * @param npcId
  2392. * @param x
  2393. * @param y
  2394. * @param z
  2395. * @param heading
  2396. * @param randomOffset
  2397. * @param despawnDelay
  2398. * @param isSummonSpawn
  2399. * @param instanceId
  2400. * @return
  2401. */
  2402. public static L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, long despawnDelay, boolean isSummonSpawn, int instanceId)
  2403. {
  2404. L2Npc result = null;
  2405. try
  2406. {
  2407. L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId);
  2408. if (template != null)
  2409. {
  2410. // Sometimes, even if the quest script specifies some xyz (for example npc.getX() etc) by the time the code
  2411. // reaches here, xyz have become 0! Also, a questdev might have purposely set xy to 0,0...however,
  2412. // the spawn code is coded such that if x=y=0, it looks into location for the spawn loc! This will NOT work
  2413. // with quest spawns! For both of the above cases, we need a fail-safe spawn. For this, we use the
  2414. // default spawn location, which is at the player's loc.
  2415. if ((x == 0) && (y == 0))
  2416. {
  2417. _log.log(Level.SEVERE, "Failed to adjust bad locks for quest spawn! Spawn aborted!");
  2418. return null;
  2419. }
  2420. if (randomOffset)
  2421. {
  2422. int offset;
  2423. offset = Rnd.get(2); // Get the direction of the offset
  2424. if (offset == 0)
  2425. {
  2426. // make offset negative
  2427. offset = -1;
  2428. }
  2429. offset *= Rnd.get(50, 100);
  2430. x += offset;
  2431. offset = Rnd.get(2); // Get the direction of the offset
  2432. if (offset == 0)
  2433. {
  2434. // make offset negative
  2435. offset = -1;
  2436. }
  2437. offset *= Rnd.get(50, 100);
  2438. y += offset;
  2439. }
  2440. L2Spawn spawn = new L2Spawn(template);
  2441. spawn.setInstanceId(instanceId);
  2442. spawn.setHeading(heading);
  2443. spawn.setX(x);
  2444. spawn.setY(y);
  2445. spawn.setZ(z);
  2446. spawn.stopRespawn();
  2447. result = spawn.spawnOne(isSummonSpawn);
  2448. if (despawnDelay > 0)
  2449. {
  2450. result.scheduleDespawn(despawnDelay);
  2451. }
  2452. return result;
  2453. }
  2454. }
  2455. catch (Exception e1)
  2456. {
  2457. _log.warning("Could not spawn Npc " + npcId + " Error: " + e1.getMessage());
  2458. }
  2459. return null;
  2460. }
  2461. /**
  2462. * @param trapId
  2463. * @param x
  2464. * @param y
  2465. * @param z
  2466. * @param heading
  2467. * @param skill
  2468. * @param instanceId
  2469. * @return
  2470. */
  2471. public L2TrapInstance addTrap(int trapId, int x, int y, int z, int heading, L2Skill skill, int instanceId)
  2472. {
  2473. final L2NpcTemplate npcTemplate = NpcTable.getInstance().getTemplate(trapId);
  2474. L2TrapInstance trap = new L2TrapInstance(IdFactory.getInstance().getNextId(), npcTemplate, instanceId, -1);
  2475. trap.setCurrentHp(trap.getMaxHp());
  2476. trap.setCurrentMp(trap.getMaxMp());
  2477. trap.setIsInvul(true);
  2478. trap.setHeading(heading);
  2479. trap.spawnMe(x, y, z);
  2480. return trap;
  2481. }
  2482. /**
  2483. * @param master
  2484. * @param minionId
  2485. * @return
  2486. */
  2487. public L2Npc addMinion(L2MonsterInstance master, int minionId)
  2488. {
  2489. return MinionList.spawnMinion(master, minionId);
  2490. }
  2491. /**
  2492. * @return the registered quest items IDs.
  2493. */
  2494. public int[] getRegisteredItemIds()
  2495. {
  2496. return questItemIds;
  2497. }
  2498. /**
  2499. * Registers all items that have to be destroyed in case player abort the quest or finish it.
  2500. * @param items
  2501. */
  2502. public void registerQuestItems(int... items)
  2503. {
  2504. questItemIds = items;
  2505. }
  2506. @Override
  2507. public String getScriptName()
  2508. {
  2509. return getName();
  2510. }
  2511. @Override
  2512. public void setActive(boolean status)
  2513. {
  2514. // TODO: Implement me.
  2515. }
  2516. @Override
  2517. public boolean reload()
  2518. {
  2519. unload();
  2520. return super.reload();
  2521. }
  2522. @Override
  2523. public boolean unload()
  2524. {
  2525. return unload(true);
  2526. }
  2527. /**
  2528. * @param removeFromList
  2529. * @return
  2530. */
  2531. public boolean unload(boolean removeFromList)
  2532. {
  2533. saveGlobalData();
  2534. // cancel all pending timers before reloading.
  2535. // if timers ought to be restarted, the quest can take care of it
  2536. // with its code (example: save global data indicating what timer must be restarted).
  2537. for (List<QuestTimer> timers : _allEventTimers.values())
  2538. {
  2539. _readLock.lock();
  2540. try
  2541. {
  2542. for (QuestTimer timer : timers)
  2543. {
  2544. timer.cancel();
  2545. }
  2546. }
  2547. finally
  2548. {
  2549. _readLock.unlock();
  2550. }
  2551. timers.clear();
  2552. }
  2553. _allEventTimers.clear();
  2554. for (Integer npcId : _questInvolvedNpcs)
  2555. {
  2556. L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId.intValue());
  2557. if (template != null)
  2558. {
  2559. template.removeQuest(this);
  2560. }
  2561. }
  2562. _questInvolvedNpcs.clear();
  2563. if (removeFromList)
  2564. {
  2565. return QuestManager.getInstance().removeQuest(this);
  2566. }
  2567. return true;
  2568. }
  2569. public Set<Integer> getQuestInvolvedNpcs()
  2570. {
  2571. return _questInvolvedNpcs;
  2572. }
  2573. @Override
  2574. public ScriptManager<?> getScriptManager()
  2575. {
  2576. return QuestManager.getInstance();
  2577. }
  2578. /**
  2579. * @param val
  2580. */
  2581. public void setOnEnterWorld(boolean val)
  2582. {
  2583. _onEnterWorld = val;
  2584. }
  2585. /**
  2586. * @return
  2587. */
  2588. public boolean getOnEnterWorld()
  2589. {
  2590. return _onEnterWorld;
  2591. }
  2592. /**
  2593. * If a quest is set as custom, it will display it's name in the NPC Quest List.<br>
  2594. * Retail quests are unhardcoded to display the name using a client string.
  2595. * @param val if {@code true} the quest script will be set as custom quest.
  2596. */
  2597. public void setIsCustom(boolean val)
  2598. {
  2599. _isCustom = val;
  2600. }
  2601. /**
  2602. * @return {@code true} if the quest script is a custom quest, {@code false} otherwise.
  2603. */
  2604. public boolean isCustomQuest()
  2605. {
  2606. return _isCustom;
  2607. }
  2608. /**
  2609. * @param val
  2610. */
  2611. public void setOlympiadUse(boolean val)
  2612. {
  2613. _isOlympiadUse = val;
  2614. }
  2615. /**
  2616. * @return {@code true} if the quest script is used for Olympiad quests, {@code false} otherwise.
  2617. */
  2618. public boolean isOlympiadUse()
  2619. {
  2620. return _isOlympiadUse;
  2621. }
  2622. /**
  2623. * Get the amount of an item in player's inventory.
  2624. * @param player the player whose inventory to check
  2625. * @param itemId the ID of the item whose amount to get
  2626. * @return the amount of the specified item in player's inventory
  2627. */
  2628. public static long getQuestItemsCount(L2PcInstance player, int itemId)
  2629. {
  2630. return player.getInventory().getInventoryItemCount(itemId, -1);
  2631. }
  2632. /**
  2633. * Get the total amount of all specified items in player's inventory.
  2634. * @param player the player whose inventory to check
  2635. * @param itemIds a list of IDs of items whose amount to get
  2636. * @return the summary amount of all listed items in player's inventory
  2637. */
  2638. public long getQuestItemsCount(L2PcInstance player, int... itemIds)
  2639. {
  2640. long count = 0;
  2641. for (L2ItemInstance item : player.getInventory().getItems())
  2642. {
  2643. if (item == null)
  2644. {
  2645. continue;
  2646. }
  2647. for (int itemId : itemIds)
  2648. {
  2649. if (item.getId() == itemId)
  2650. {
  2651. if ((count + item.getCount()) > Long.MAX_VALUE)
  2652. {
  2653. return Long.MAX_VALUE;
  2654. }
  2655. count += item.getCount();
  2656. }
  2657. }
  2658. }
  2659. return count;
  2660. }
  2661. /**
  2662. * Check for an item in player's inventory.
  2663. * @param player the player whose inventory to check for quest items
  2664. * @param itemId the ID of the item to check for
  2665. * @return {@code true} if the item exists in player's inventory, {@code false} otherwise
  2666. */
  2667. public static boolean hasQuestItems(L2PcInstance player, int itemId)
  2668. {
  2669. return player.getInventory().getItemByItemId(itemId) != null;
  2670. }
  2671. /**
  2672. * Check for multiple items in player's inventory.
  2673. * @param player the player whose inventory to check for quest items
  2674. * @param itemIds a list of item IDs to check for
  2675. * @return {@code true} if all items exist in player's inventory, {@code false} otherwise
  2676. */
  2677. public static boolean hasQuestItems(L2PcInstance player, int... itemIds)
  2678. {
  2679. final PcInventory inv = player.getInventory();
  2680. for (int itemId : itemIds)
  2681. {
  2682. if (inv.getItemByItemId(itemId) == null)
  2683. {
  2684. return false;
  2685. }
  2686. }
  2687. return true;
  2688. }
  2689. /**
  2690. * Check for multiple items in player's inventory.
  2691. * @param player the player whose inventory to check for quest items
  2692. * @param itemIds a list of item IDs to check for
  2693. * @return {@code true} if at least one items exist in player's inventory, {@code false} otherwise
  2694. */
  2695. public boolean hasAtLeastOneQuestItem(L2PcInstance player, int... itemIds)
  2696. {
  2697. final PcInventory inv = player.getInventory();
  2698. for (int itemId : itemIds)
  2699. {
  2700. if (inv.getItemByItemId(itemId) != null)
  2701. {
  2702. return true;
  2703. }
  2704. }
  2705. return false;
  2706. }
  2707. /**
  2708. * Get the enchantment level of an item in player's inventory.
  2709. * @param player the player whose item to check
  2710. * @param itemId the ID of the item whose enchantment level to get
  2711. * @return the enchantment level of the item or 0 if the item was not found
  2712. */
  2713. public static int getEnchantLevel(L2PcInstance player, int itemId)
  2714. {
  2715. final L2ItemInstance enchantedItem = player.getInventory().getItemByItemId(itemId);
  2716. if (enchantedItem == null)
  2717. {
  2718. return 0;
  2719. }
  2720. return enchantedItem.getEnchantLevel();
  2721. }
  2722. /**
  2723. * Give Adena to the player.
  2724. * @param player the player to whom to give the Adena
  2725. * @param count the amount of Adena to give
  2726. * @param applyRates if {@code true} quest rates will be applied to the amount
  2727. */
  2728. public void giveAdena(L2PcInstance player, long count, boolean applyRates)
  2729. {
  2730. if (applyRates)
  2731. {
  2732. rewardItems(player, PcInventory.ADENA_ID, count);
  2733. }
  2734. else
  2735. {
  2736. giveItems(player, PcInventory.ADENA_ID, count);
  2737. }
  2738. }
  2739. /**
  2740. * Give a reward to player using multipliers.
  2741. * @param player the player to whom to give the item
  2742. * @param holder
  2743. */
  2744. public static void rewardItems(L2PcInstance player, ItemHolder holder)
  2745. {
  2746. rewardItems(player, holder.getId(), holder.getCount());
  2747. }
  2748. /**
  2749. * Give a reward to player using multipliers.
  2750. * @param player the player to whom to give the item
  2751. * @param itemId the ID of the item to give
  2752. * @param count the amount of items to give
  2753. */
  2754. public static void rewardItems(L2PcInstance player, int itemId, long count)
  2755. {
  2756. if (count <= 0)
  2757. {
  2758. return;
  2759. }
  2760. final L2ItemInstance _tmpItem = ItemTable.getInstance().createDummyItem(itemId);
  2761. if (_tmpItem == null)
  2762. {
  2763. return;
  2764. }
  2765. try
  2766. {
  2767. if (itemId == PcInventory.ADENA_ID)
  2768. {
  2769. count *= Config.RATE_QUEST_REWARD_ADENA;
  2770. }
  2771. else if (Config.RATE_QUEST_REWARD_USE_MULTIPLIERS)
  2772. {
  2773. if (_tmpItem.isEtcItem())
  2774. {
  2775. switch (_tmpItem.getEtcItem().getItemType())
  2776. {
  2777. case POTION:
  2778. count *= Config.RATE_QUEST_REWARD_POTION;
  2779. break;
  2780. case SCRL_ENCHANT_WP:
  2781. case SCRL_ENCHANT_AM:
  2782. case SCROLL:
  2783. count *= Config.RATE_QUEST_REWARD_SCROLL;
  2784. break;
  2785. case RECIPE:
  2786. count *= Config.RATE_QUEST_REWARD_RECIPE;
  2787. break;
  2788. case MATERIAL:
  2789. count *= Config.RATE_QUEST_REWARD_MATERIAL;
  2790. break;
  2791. default:
  2792. count *= Config.RATE_QUEST_REWARD;
  2793. }
  2794. }
  2795. }
  2796. else
  2797. {
  2798. count *= Config.RATE_QUEST_REWARD;
  2799. }
  2800. }
  2801. catch (Exception e)
  2802. {
  2803. count = Long.MAX_VALUE;
  2804. }
  2805. // Add items to player's inventory
  2806. L2ItemInstance item = player.getInventory().addItem("Quest", itemId, count, player, player.getTarget());
  2807. if (item == null)
  2808. {
  2809. return;
  2810. }
  2811. sendItemGetMessage(player, item, count);
  2812. }
  2813. /**
  2814. * Send the system message and the status update packets to the player.
  2815. * @param player the player that has got the item
  2816. * @param item the item obtain by the player
  2817. * @param count the item count
  2818. */
  2819. private static void sendItemGetMessage(L2PcInstance player, L2ItemInstance item, long count)
  2820. {
  2821. // If item for reward is gold, send message of gold reward to client
  2822. if (item.getId() == PcInventory.ADENA_ID)
  2823. {
  2824. SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S1_ADENA);
  2825. smsg.addItemNumber(count);
  2826. player.sendPacket(smsg);
  2827. }
  2828. // Otherwise, send message of object reward to client
  2829. else
  2830. {
  2831. if (count > 1)
  2832. {
  2833. SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S2_S1_S);
  2834. smsg.addItemName(item);
  2835. smsg.addItemNumber(count);
  2836. player.sendPacket(smsg);
  2837. }
  2838. else
  2839. {
  2840. SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
  2841. smsg.addItemName(item);
  2842. player.sendPacket(smsg);
  2843. }
  2844. }
  2845. // send packets
  2846. StatusUpdate su = new StatusUpdate(player);
  2847. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  2848. player.sendPacket(su);
  2849. }
  2850. /**
  2851. * Give item/reward to the player
  2852. * @param player
  2853. * @param itemId
  2854. * @param count
  2855. */
  2856. public static void giveItems(L2PcInstance player, int itemId, long count)
  2857. {
  2858. giveItems(player, itemId, count, 0);
  2859. }
  2860. /**
  2861. * Give item/reward to the player
  2862. * @param player
  2863. * @param holder
  2864. */
  2865. protected static void giveItems(L2PcInstance player, ItemHolder holder)
  2866. {
  2867. giveItems(player, holder.getId(), holder.getCount());
  2868. }
  2869. /**
  2870. * @param player
  2871. * @param itemId
  2872. * @param count
  2873. * @param enchantlevel
  2874. */
  2875. public static void giveItems(L2PcInstance player, int itemId, long count, int enchantlevel)
  2876. {
  2877. if (count <= 0)
  2878. {
  2879. return;
  2880. }
  2881. // Add items to player's inventory
  2882. final L2ItemInstance item = player.getInventory().addItem("Quest", itemId, count, player, player.getTarget());
  2883. if (item == null)
  2884. {
  2885. return;
  2886. }
  2887. // set enchant level for item if that item is not adena
  2888. if ((enchantlevel > 0) && (itemId != PcInventory.ADENA_ID))
  2889. {
  2890. item.setEnchantLevel(enchantlevel);
  2891. }
  2892. sendItemGetMessage(player, item, count);
  2893. }
  2894. /**
  2895. * @param player
  2896. * @param itemId
  2897. * @param count
  2898. * @param attributeId
  2899. * @param attributeLevel
  2900. */
  2901. public static void giveItems(L2PcInstance player, int itemId, long count, byte attributeId, int attributeLevel)
  2902. {
  2903. if (count <= 0)
  2904. {
  2905. return;
  2906. }
  2907. // Add items to player's inventory
  2908. final L2ItemInstance item = player.getInventory().addItem("Quest", itemId, count, player, player.getTarget());
  2909. if (item == null)
  2910. {
  2911. return;
  2912. }
  2913. // set enchant level for item if that item is not adena
  2914. if ((attributeId >= 0) && (attributeLevel > 0))
  2915. {
  2916. item.setElementAttr(attributeId, attributeLevel);
  2917. if (item.isEquipped())
  2918. {
  2919. item.updateElementAttrBonus(player);
  2920. }
  2921. InventoryUpdate iu = new InventoryUpdate();
  2922. iu.addModifiedItem(item);
  2923. player.sendPacket(iu);
  2924. }
  2925. sendItemGetMessage(player, item, count);
  2926. }
  2927. /**
  2928. * Give the specified player a set amount of items if he is lucky enough.<br>
  2929. * Not recommended to use this for non-stacking items.
  2930. * @param player the player to give the item(s) to
  2931. * @param itemId the ID of the item to give
  2932. * @param amountToGive the amount of items to give
  2933. * @param limit the maximum amount of items the player can have. Won't give more if this limit is reached. 0 - no limit.
  2934. * @param dropChance the drop chance as a decimal digit from 0 to 1
  2935. * @param playSound if true, plays ItemSound.quest_itemget when items are given and ItemSound.quest_middle when the limit is reached
  2936. * @return {@code true} if limit > 0 and the limit was reached or if limit <= 0 and items were given; {@code false} in all other cases
  2937. */
  2938. public static boolean giveItemRandomly(L2PcInstance player, int itemId, long amountToGive, long limit, double dropChance, boolean playSound)
  2939. {
  2940. return giveItemRandomly(player, null, itemId, amountToGive, amountToGive, limit, dropChance, playSound);
  2941. }
  2942. /**
  2943. * Give the specified player a set amount of items if he is lucky enough.<br>
  2944. * Not recommended to use this for non-stacking items.
  2945. * @param player the player to give the item(s) to
  2946. * @param npc the NPC that "dropped" the item (can be null)
  2947. * @param itemId the ID of the item to give
  2948. * @param amountToGive the amount of items to give
  2949. * @param limit the maximum amount of items the player can have. Won't give more if this limit is reached. 0 - no limit.
  2950. * @param dropChance the drop chance as a decimal digit from 0 to 1
  2951. * @param playSound if true, plays ItemSound.quest_itemget when items are given and ItemSound.quest_middle when the limit is reached
  2952. * @return {@code true} if limit > 0 and the limit was reached or if limit <= 0 and items were given; {@code false} in all other cases
  2953. */
  2954. public static boolean giveItemRandomly(L2PcInstance player, L2Npc npc, int itemId, long amountToGive, long limit, double dropChance, boolean playSound)
  2955. {
  2956. return giveItemRandomly(player, npc, itemId, amountToGive, amountToGive, limit, dropChance, playSound);
  2957. }
  2958. /**
  2959. * Give the specified player a random amount of items if he is lucky enough.<br>
  2960. * Not recommended to use this for non-stacking items.
  2961. * @param player the player to give the item(s) to
  2962. * @param npc the NPC that "dropped" the item (can be null)
  2963. * @param itemId the ID of the item to give
  2964. * @param minAmount the minimum amount of items to give
  2965. * @param maxAmount the maximum amount of items to give (will give a random amount between min/maxAmount multiplied by quest rates)
  2966. * @param limit the maximum amount of items the player can have. Won't give more if this limit is reached. 0 - no limit.
  2967. * @param dropChance the drop chance as a decimal digit from 0 to 1
  2968. * @param playSound if true, plays ItemSound.quest_itemget when items are given and ItemSound.quest_middle when the limit is reached
  2969. * @return {@code true} if limit > 0 and the limit was reached or if limit <= 0 and items were given; {@code false} in all other cases
  2970. */
  2971. public static boolean giveItemRandomly(L2PcInstance player, L2Npc npc, int itemId, long minAmount, long maxAmount, long limit, double dropChance, boolean playSound)
  2972. {
  2973. final long currentCount = getQuestItemsCount(player, itemId);
  2974. if ((limit > 0) && (currentCount >= limit))
  2975. {
  2976. return true;
  2977. }
  2978. minAmount *= Config.RATE_QUEST_DROP;
  2979. maxAmount *= Config.RATE_QUEST_DROP;
  2980. dropChance *= Config.RATE_QUEST_DROP; // TODO separate configs for rate and amount
  2981. if ((npc != null) && Config.L2JMOD_CHAMPION_ENABLE && npc.isChampion())
  2982. {
  2983. dropChance *= Config.L2JMOD_CHAMPION_REWARDS;
  2984. if ((itemId == PcInventory.ADENA_ID) || (itemId == PcInventory.ANCIENT_ADENA_ID))
  2985. {
  2986. minAmount *= Config.L2JMOD_CHAMPION_ADENAS_REWARDS;
  2987. maxAmount *= Config.L2JMOD_CHAMPION_ADENAS_REWARDS;
  2988. }
  2989. else
  2990. {
  2991. minAmount *= Config.L2JMOD_CHAMPION_REWARDS;
  2992. maxAmount *= Config.L2JMOD_CHAMPION_REWARDS;
  2993. }
  2994. }
  2995. long amountToGive = ((minAmount == maxAmount) ? minAmount : Rnd.get(minAmount, maxAmount));
  2996. final double random = Rnd.nextDouble();
  2997. // Inventory slot check (almost useless for non-stacking items)
  2998. if ((dropChance >= random) && (amountToGive > 0) && player.getInventory().validateCapacityByItemId(itemId))
  2999. {
  3000. if ((limit > 0) && ((currentCount + amountToGive) > limit))
  3001. {
  3002. amountToGive = limit - currentCount;
  3003. }
  3004. // Give the item to player
  3005. L2ItemInstance item = player.addItem("Quest", itemId, amountToGive, npc, true);
  3006. if (item != null)
  3007. {
  3008. // limit reached (if there is no limit, this block doesn't execute)
  3009. if ((currentCount + amountToGive) == limit)
  3010. {
  3011. if (playSound)
  3012. {
  3013. playSound(player, QuestSound.ITEMSOUND_QUEST_MIDDLE);
  3014. }
  3015. return true;
  3016. }
  3017. if (playSound)
  3018. {
  3019. playSound(player, QuestSound.ITEMSOUND_QUEST_ITEMGET);
  3020. }
  3021. // if there is no limit, return true every time an item is given
  3022. if (limit <= 0)
  3023. {
  3024. return true;
  3025. }
  3026. }
  3027. }
  3028. return false;
  3029. }
  3030. /**
  3031. * Take an amount of a specified item from player's inventory.
  3032. * @param player the player whose item to take
  3033. * @param itemId the ID of the item to take
  3034. * @param amount the amount to take
  3035. * @return {@code true} if any items were taken, {@code false} otherwise
  3036. */
  3037. public static boolean takeItems(L2PcInstance player, int itemId, long amount)
  3038. {
  3039. // Get object item from player's inventory list
  3040. final L2ItemInstance item = player.getInventory().getItemByItemId(itemId);
  3041. if (item == null)
  3042. {
  3043. return false;
  3044. }
  3045. // Tests on count value in order not to have negative value
  3046. if ((amount < 0) || (amount > item.getCount()))
  3047. {
  3048. amount = item.getCount();
  3049. }
  3050. // Destroy the quantity of items wanted
  3051. if (item.isEquipped())
  3052. {
  3053. final L2ItemInstance[] unequiped = player.getInventory().unEquipItemInBodySlotAndRecord(item.getItem().getBodyPart());
  3054. InventoryUpdate iu = new InventoryUpdate();
  3055. for (L2ItemInstance itm : unequiped)
  3056. {
  3057. iu.addModifiedItem(itm);
  3058. }
  3059. player.sendPacket(iu);
  3060. player.broadcastUserInfo();
  3061. }
  3062. return player.destroyItemByItemId("Quest", itemId, amount, player, true);
  3063. }
  3064. /**
  3065. * Take an amount of a specified item from player's inventory.
  3066. * @param player
  3067. * @param holder
  3068. * @return {@code true} if any items were taken, {@code false} otherwise
  3069. */
  3070. protected static boolean takeItems(L2PcInstance player, ItemHolder holder)
  3071. {
  3072. return takeItems(player, holder.getId(), holder.getCount());
  3073. }
  3074. /**
  3075. * Take an amount of all specified items from player's inventory.
  3076. * @param player the player whose items to take
  3077. * @param amount the amount to take of each item
  3078. * @param itemIds a list or an array of IDs of the items to take
  3079. * @return {@code true} if all items were taken, {@code false} otherwise
  3080. */
  3081. public static boolean takeItems(L2PcInstance player, int amount, int... itemIds)
  3082. {
  3083. boolean check = true;
  3084. if (itemIds != null)
  3085. {
  3086. for (int item : itemIds)
  3087. {
  3088. check &= takeItems(player, item, amount);
  3089. }
  3090. }
  3091. return check;
  3092. }
  3093. /**
  3094. * Remove all quest items associated with this quest from the specified player's inventory.
  3095. * @param player the player whose quest items to remove
  3096. */
  3097. public void removeRegisteredQuestItems(L2PcInstance player)
  3098. {
  3099. takeItems(player, -1, questItemIds);
  3100. }
  3101. /**
  3102. * Send a packet in order to play a sound to the player.
  3103. * @param player the player whom to send the packet
  3104. * @param sound the name of the sound to play
  3105. */
  3106. public static void playSound(L2PcInstance player, String sound)
  3107. {
  3108. player.sendPacket(QuestSound.getSound(sound));
  3109. }
  3110. /**
  3111. * Send a packet in order to play a sound to the player.
  3112. * @param player the player whom to send the packet
  3113. * @param sound the {@link QuestSound} object of the sound to play
  3114. */
  3115. public static void playSound(L2PcInstance player, QuestSound sound)
  3116. {
  3117. player.sendPacket(sound.getPacket());
  3118. }
  3119. /**
  3120. * Add EXP and SP as quest reward.
  3121. * @param player the player whom to reward with the EXP/SP
  3122. * @param exp the amount of EXP to give to the player
  3123. * @param sp the amount of SP to give to the player
  3124. */
  3125. public static void addExpAndSp(L2PcInstance player, long exp, int sp)
  3126. {
  3127. player.addExpAndSp((long) player.calcStat(Stats.EXPSP_RATE, exp * Config.RATE_QUEST_REWARD_XP, null, null), (int) player.calcStat(Stats.EXPSP_RATE, sp * Config.RATE_QUEST_REWARD_SP, null, null));
  3128. }
  3129. /**
  3130. * Get a random integer from 0 (inclusive) to {@code max} (exclusive).<br>
  3131. * Use this method instead of importing {@link com.l2jserver.util.Rnd} utility.
  3132. * @param max the maximum value for randomization
  3133. * @return a random integer number from 0 to {@code max - 1}
  3134. */
  3135. public static int getRandom(int max)
  3136. {
  3137. return Rnd.get(max);
  3138. }
  3139. /**
  3140. * Get a random integer from {@code min} (inclusive) to {@code max} (inclusive).<br>
  3141. * Use this method instead of importing {@link com.l2jserver.util.Rnd} utility.
  3142. * @param min the minimum value for randomization
  3143. * @param max the maximum value for randomization
  3144. * @return a random integer number from {@code min} to {@code max}
  3145. */
  3146. public static int getRandom(int min, int max)
  3147. {
  3148. return Rnd.get(min, max);
  3149. }
  3150. /**
  3151. * Get a random boolean.
  3152. * @return {@code true} or {@code false} randomly
  3153. */
  3154. public static boolean getRandomBoolean()
  3155. {
  3156. return Rnd.nextBoolean();
  3157. }
  3158. /**
  3159. * Get the ID of the item equipped in the specified inventory slot of the player.
  3160. * @param player the player whose inventory to check
  3161. * @param slot the location in the player's inventory to check
  3162. * @return the ID of the item equipped in the specified inventory slot or 0 if the slot is empty or item is {@code null}.
  3163. */
  3164. public static int getItemEquipped(L2PcInstance player, int slot)
  3165. {
  3166. return player.getInventory().getPaperdollItemId(slot);
  3167. }
  3168. /**
  3169. * @return the number of ticks from the {@link com.l2jserver.gameserver.GameTimeController}.
  3170. */
  3171. public static int getGameTicks()
  3172. {
  3173. return GameTimeController.getInstance().getGameTicks();
  3174. }
  3175. /**
  3176. * Executes a procedure for each player, depending on the parameters.
  3177. * @param player the player were the procedure will be executed
  3178. * @param npc the related Npc
  3179. * @param isSummon {@code true} if the event that call this method was originated by the player's summon
  3180. * @param includeParty if {@code true} #actionForEachPlayer(L2PcInstance, L2Npc, boolean) will be called with the player's party members
  3181. * @param includeCommandChannel if {@code true} {@link #actionForEachPlayer(L2PcInstance, L2Npc, boolean)} will be called with the player's command channel members
  3182. * @see #actionForEachPlayer(L2PcInstance, L2Npc, boolean)
  3183. */
  3184. public final void executeForEachPlayer(L2PcInstance player, final L2Npc npc, final boolean isSummon, boolean includeParty, boolean includeCommandChannel)
  3185. {
  3186. if ((includeParty || includeCommandChannel) && player.isInParty())
  3187. {
  3188. if (includeCommandChannel && player.getParty().isInCommandChannel())
  3189. {
  3190. player.getParty().getCommandChannel().forEachMember(new IL2Procedure<L2PcInstance>()
  3191. {
  3192. @Override
  3193. public boolean execute(L2PcInstance member)
  3194. {
  3195. actionForEachPlayer(member, npc, isSummon);
  3196. return true;
  3197. }
  3198. });
  3199. }
  3200. else if (includeParty)
  3201. {
  3202. player.getParty().forEachMember(new IL2Procedure<L2PcInstance>()
  3203. {
  3204. @Override
  3205. public boolean execute(L2PcInstance member)
  3206. {
  3207. actionForEachPlayer(member, npc, isSummon);
  3208. return true;
  3209. }
  3210. });
  3211. }
  3212. }
  3213. else
  3214. {
  3215. actionForEachPlayer(player, npc, isSummon);
  3216. }
  3217. }
  3218. /**
  3219. * Overridable method called from {@link #executeForEachPlayer(L2PcInstance, L2Npc, boolean, boolean, boolean)}
  3220. * @param player the player where the action will be run
  3221. * @param npc the Npc related to this action
  3222. * @param isSummon {@code true} if the event that call this method was originated by the player's summon
  3223. */
  3224. public void actionForEachPlayer(L2PcInstance player, L2Npc npc, boolean isSummon)
  3225. {
  3226. // To be overridden in quest scripts.
  3227. }
  3228. /**
  3229. * Opens the door if presents on the instance and its not open.
  3230. * @param doorId
  3231. * @param instanceId
  3232. */
  3233. public void openDoor(int doorId, int instanceId)
  3234. {
  3235. final L2DoorInstance door = getDoor(doorId, instanceId);
  3236. if (door == null)
  3237. {
  3238. _log.log(Level.WARNING, getClass().getSimpleName() + ": called openDoor(" + doorId + ", " + instanceId + "); but door wasnt found!", new NullPointerException());
  3239. }
  3240. else if (!door.getOpen())
  3241. {
  3242. door.openMe();
  3243. }
  3244. }
  3245. /**
  3246. * Closes the door if presents on the instance and its open
  3247. * @param doorId
  3248. * @param instanceId
  3249. */
  3250. public void closeDoor(int doorId, int instanceId)
  3251. {
  3252. final L2DoorInstance door = getDoor(doorId, instanceId);
  3253. if (door == null)
  3254. {
  3255. _log.log(Level.WARNING, getClass().getSimpleName() + ": called closeDoor(" + doorId + ", " + instanceId + "); but door wasnt found!", new NullPointerException());
  3256. }
  3257. else if (door.getOpen())
  3258. {
  3259. door.closeMe();
  3260. }
  3261. }
  3262. /**
  3263. * Retriving Door from instances or from the real world.
  3264. * @param doorId
  3265. * @param instanceId
  3266. * @return {@link L2DoorInstance}
  3267. */
  3268. public L2DoorInstance getDoor(int doorId, int instanceId)
  3269. {
  3270. L2DoorInstance door = null;
  3271. if (instanceId <= 0)
  3272. {
  3273. door = DoorTable.getInstance().getDoor(doorId);
  3274. }
  3275. else
  3276. {
  3277. final Instance inst = InstanceManager.getInstance().getInstance(instanceId);
  3278. if (inst != null)
  3279. {
  3280. door = inst.getDoor(doorId);
  3281. }
  3282. }
  3283. return door;
  3284. }
  3285. /**
  3286. * Teleport player to/from instance
  3287. * @param player
  3288. * @param loc
  3289. * @param instanceId
  3290. */
  3291. public void teleportPlayer(L2PcInstance player, Location loc, int instanceId)
  3292. {
  3293. teleportPlayer(player, loc, instanceId, true);
  3294. }
  3295. /**
  3296. * Teleport player to/from instance
  3297. * @param player
  3298. * @param loc
  3299. * @param instanceId
  3300. * @param allowRandomOffset
  3301. */
  3302. public void teleportPlayer(L2PcInstance player, Location loc, int instanceId, boolean allowRandomOffset)
  3303. {
  3304. loc.setInstanceId(instanceId);
  3305. player.teleToLocation(loc, allowRandomOffset);
  3306. }
  3307. }