Quest.java 115 KB

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