Quest.java 125 KB

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