Quest.java 95 KB

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