Quest.java 124 KB

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