Quest.java 114 KB

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