Quest.java 112 KB

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