Quest.java 106 KB

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