Quest.java 112 KB

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