Quest.java 125 KB

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