Quest.java 112 KB

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