Quest.java 120 KB

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