Quest.java 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167
  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. public class TmpOnSkillSee implements Runnable
  697. {
  698. private final L2Npc _npc;
  699. private final L2PcInstance _caster;
  700. private final L2Skill _skill;
  701. private final L2Object[] _targets;
  702. private final boolean _isPet;
  703. public TmpOnSkillSee(L2Npc npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
  704. {
  705. _npc = npc;
  706. _caster = caster;
  707. _skill = skill;
  708. _targets = targets;
  709. _isPet = isPet;
  710. }
  711. @Override
  712. public void run()
  713. {
  714. String res = null;
  715. try
  716. {
  717. res = onSkillSee(_npc, _caster, _skill, _targets, _isPet);
  718. }
  719. catch (Exception e)
  720. {
  721. showError(_caster, e);
  722. }
  723. showResult(_caster, res);
  724. }
  725. }
  726. /**
  727. * @param npc
  728. * @param caster
  729. * @param skill
  730. * @param targets
  731. * @param isPet
  732. * @return {@code true}
  733. */
  734. public final boolean notifySkillSee(L2Npc npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
  735. {
  736. ThreadPoolManager.getInstance().executeAi(new TmpOnSkillSee(npc, caster, skill, targets, isPet));
  737. return true;
  738. }
  739. /**
  740. * @param npc
  741. * @param caller
  742. * @param attacker
  743. * @param isPet
  744. * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
  745. */
  746. public final boolean notifyFactionCall(L2Npc npc, L2Npc caller, L2PcInstance attacker, boolean isPet)
  747. {
  748. String res = null;
  749. try
  750. {
  751. res = onFactionCall(npc, caller, attacker, isPet);
  752. }
  753. catch (Exception e)
  754. {
  755. return showError(attacker, e);
  756. }
  757. return showResult(attacker, res);
  758. }
  759. public class TmpOnAggroEnter implements Runnable
  760. {
  761. private final L2Npc _npc;
  762. private final L2PcInstance _pc;
  763. private final boolean _isPet;
  764. public TmpOnAggroEnter(L2Npc npc, L2PcInstance pc, boolean isPet)
  765. {
  766. _npc = npc;
  767. _pc = pc;
  768. _isPet = isPet;
  769. }
  770. @Override
  771. public void run()
  772. {
  773. String res = null;
  774. try
  775. {
  776. res = onAggroRangeEnter(_npc, _pc, _isPet);
  777. }
  778. catch (Exception e)
  779. {
  780. showError(_pc, e);
  781. }
  782. showResult(_pc, res);
  783. }
  784. }
  785. /**
  786. * @param npc
  787. * @param player
  788. * @param isPet
  789. * @return {@code true}
  790. */
  791. public final boolean notifyAggroRangeEnter(L2Npc npc, L2PcInstance player, boolean isPet)
  792. {
  793. ThreadPoolManager.getInstance().executeAi(new TmpOnAggroEnter(npc, player, isPet));
  794. return true;
  795. }
  796. /**
  797. * @param character
  798. * @param zone
  799. * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
  800. */
  801. public final boolean notifyEnterZone(L2Character character, L2ZoneType zone)
  802. {
  803. L2PcInstance player = character.getActingPlayer();
  804. String res = null;
  805. try
  806. {
  807. res = onEnterZone(character, zone);
  808. }
  809. catch (Exception e)
  810. {
  811. if (player != null)
  812. {
  813. return showError(player, e);
  814. }
  815. }
  816. if (player != null)
  817. {
  818. return showResult(player, res);
  819. }
  820. return true;
  821. }
  822. /**
  823. * @param character
  824. * @param zone
  825. * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
  826. */
  827. public final boolean notifyExitZone(L2Character character, L2ZoneType zone)
  828. {
  829. L2PcInstance player = character.getActingPlayer();
  830. String res = null;
  831. try
  832. {
  833. res = onExitZone(character, zone);
  834. }
  835. catch (Exception e)
  836. {
  837. if (player != null)
  838. {
  839. return showError(player, e);
  840. }
  841. }
  842. if (player != null)
  843. {
  844. return showResult(player, res);
  845. }
  846. return true;
  847. }
  848. /**
  849. * @param winner
  850. * @param type {@code false} if there was an error, {@code true} otherwise
  851. */
  852. public final void notifyOlympiadWin(L2PcInstance winner, CompetitionType type)
  853. {
  854. try
  855. {
  856. onOlympiadWin(winner, type);
  857. }
  858. catch (Exception e)
  859. {
  860. showError(winner, e);
  861. }
  862. }
  863. /**
  864. * @param loser
  865. * @param type {@code false} if there was an error, {@code true} otherwise
  866. */
  867. public final void notifyOlympiadLose(L2PcInstance loser, CompetitionType type)
  868. {
  869. try
  870. {
  871. onOlympiadLose(loser, type);
  872. }
  873. catch (Exception e)
  874. {
  875. showError(loser, e);
  876. }
  877. }
  878. // These are methods that java calls to invoke scripts.
  879. /**
  880. * This function is called in place of {@link #onAttack(L2Npc, L2PcInstance, int, boolean, L2Skill)} if the former is not implemented.<br>
  881. * 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.
  882. * @param npc this parameter contains a reference to the exact instance of the NPC that got attacked the NPC.
  883. * @param attacker this parameter contains a reference to the exact instance of the player who attacked.
  884. * @param damage this parameter represents the total damage that this attack has inflicted to the NPC.
  885. * @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.
  886. * @return
  887. */
  888. public String onAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isPet)
  889. {
  890. return null;
  891. }
  892. /**
  893. * This function is called whenever a player attacks an NPC that is registered for the quest.<br>
  894. * 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>
  895. * @param npc this parameter contains a reference to the exact instance of the NPC that got attacked.
  896. * @param attacker this parameter contains a reference to the exact instance of the player who attacked the NPC.
  897. * @param damage this parameter represents the total damage that this attack has inflicted to the NPC.
  898. * @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.
  899. * @param skill parameter is the skill that player used to attack NPC.
  900. * @return
  901. */
  902. public String onAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isPet, L2Skill skill)
  903. {
  904. return onAttack(npc, attacker, damage, isPet);
  905. }
  906. /**
  907. * This function is called whenever an <b>exact instance</b> of a character who was previously registered for this event dies.<br>
  908. * 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.
  909. * @param killer this parameter contains a reference to the exact instance of the NPC that <b>killed</b> the character.
  910. * @param victim this parameter contains a reference to the exact instance of the character that got killed.
  911. * @param qs this parameter contains a reference to the QuestState of whomever was interested (waiting) for this kill.
  912. * @return
  913. */
  914. public String onDeath(L2Character killer, L2Character victim, QuestState qs)
  915. {
  916. if (killer instanceof L2Npc)
  917. {
  918. return onAdvEvent("", (L2Npc) killer, qs.getPlayer());
  919. }
  920. return onAdvEvent("", null, qs.getPlayer());
  921. }
  922. /**
  923. * This function is called whenever a player clicks on a link in a quest dialog and whenever a timer fires.<br>
  924. * If is not overridden by a subclass, then default to the returned value of the simpler (and older) {@link #onEvent(String, QuestState)} override.<br>
  925. * If the player has a quest state, use it as parameter in the next call, otherwise return null.
  926. * @param event this parameter contains a string identifier for the event.<br>
  927. * Generally, this string is passed directly via the link.<br>
  928. * For example:<br>
  929. * <code>
  930. * &lt;a action="bypass -h Quest 626_ADarkTwilight 31517-01.htm"&gt;hello&lt;/a&gt;
  931. * </code><br>
  932. * The above link sets the event variable to "31517-01.htm" for the quest 626_ADarkTwilight.<br>
  933. * In the case of timers, this will be the name of the timer.<br>
  934. * This parameter serves as a sort of identifier.
  935. * @param npc this parameter contains a reference to the instance of NPC associated with this event.<br>
  936. * This may be the NPC registered in a timer, or the NPC with whom a player is speaking, etc.<br>
  937. * This parameter may be {@code null} in certain circumstances.
  938. * @param player this parameter contains a reference to the player participating in this function.<br>
  939. * It may be the player speaking to the NPC, or the player who caused a timer to start (and owns that timer).<br>
  940. * This parameter may be {@code null} in certain circumstances.
  941. * @return the text returned by the event (may be {@code null}, a filename or just text)
  942. */
  943. public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  944. {
  945. final QuestState qs = player.getQuestState(getName());
  946. if (qs != null)
  947. {
  948. return onEvent(event, qs);
  949. }
  950. return null;
  951. }
  952. /**
  953. * This function is called in place of {@link #onAdvEvent(String, L2Npc, L2PcInstance)} if the former is not implemented.<br>
  954. * 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.
  955. * @param event this parameter contains a string identifier for the event.<br>
  956. * Generally, this string is passed directly via the link.<br>
  957. * For example:<br>
  958. * <code>
  959. * &lt;a action="bypass -h Quest 626_ADarkTwilight 31517-01.htm"&gt;hello&lt;/a&gt;
  960. * </code><br>
  961. * The above link sets the event variable to "31517-01.htm" for the quest 626_ADarkTwilight.<br>
  962. * In the case of timers, this will be the name of the timer.<br>
  963. * This parameter serves as a sort of identifier.
  964. * @param qs this parameter contains a reference to the quest state of the player who used the link or started the timer.
  965. * @return the text returned by the event (may be {@code null}, a filename or just text)
  966. */
  967. public String onEvent(String event, QuestState qs)
  968. {
  969. return null;
  970. }
  971. /**
  972. * This function is called whenever a player kills a NPC that is registered for the quest.
  973. * @param npc this parameter contains a reference to the exact instance of the NPC that got killed.
  974. * @param killer this parameter contains a reference to the exact instance of the player who killed the NPC.
  975. * @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.
  976. * @return the text returned by the event (may be {@code null}, a filename or just text)
  977. */
  978. public String onKill(L2Npc npc, L2PcInstance killer, boolean isPet)
  979. {
  980. return null;
  981. }
  982. /**
  983. * This function is called whenever a player clicks to the "Quest" link of an NPC that is registered for the quest.
  984. * @param npc this parameter contains a reference to the exact instance of the NPC that the player is talking with.
  985. * @param talker this parameter contains a reference to the exact instance of the player who is talking to the NPC.
  986. * @return the text returned by the event (may be {@code null}, a filename or just text)
  987. */
  988. public String onTalk(L2Npc npc, L2PcInstance talker)
  989. {
  990. return null;
  991. }
  992. /**
  993. * This function is called whenever a player talks to an NPC that is registered for the quest.<br>
  994. * That is, it is triggered from the very first click on the NPC, not via another dialog.<br>
  995. * <b>Note 1:</b><br>
  996. * Each NPC can be registered to at most one quest for triggering this function.<br>
  997. * In other words, the same one NPC cannot respond to an "onFirstTalk" request from two different quests.<br>
  998. * Attempting to register an NPC in two different quests for this function will result in one of the two registration being ignored.<br>
  999. * <b>Note 2:</b><br>
  1000. * Since a Quest link isn't clicked in order to reach this, a quest state can be invalid within this function.<br>
  1001. * The coder of the script may need to create a new quest state (if necessary).<br>
  1002. * <b>Note 3:</b><br>
  1003. * 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>
  1004. * If you wish to show the default HTML, within onFirstTalk do npc.showChatWindow(player) and then return ""<br>
  1005. * @param npc this parameter contains a reference to the exact instance of the NPC that the player is talking with.
  1006. * @param player this parameter contains a reference to the exact instance of the player who is talking to the NPC.
  1007. * @return the text returned by the event (may be {@code null}, a filename or just text)
  1008. * @since <a href="http://trac.l2jserver.com/changeset/771">Jython AI support for "onFirstTalk"</a>
  1009. */
  1010. public String onFirstTalk(L2Npc npc, L2PcInstance player)
  1011. {
  1012. return null;
  1013. }
  1014. /**
  1015. * This function is called whenever a player request a skill list.<br>
  1016. * TODO: Cleanup or re-implement, since Skill Trees rework it's support was removed.
  1017. * @param npc this parameter contains a reference to the exact instance of the NPC that the player requested the skill list.
  1018. * @param player this parameter contains a reference to the exact instance of the player who requested the skill list.
  1019. * @return
  1020. */
  1021. public String onAcquireSkillList(L2Npc npc, L2PcInstance player)
  1022. {
  1023. return null;
  1024. }
  1025. /**
  1026. * This function is called whenever a player request a skill info.<br>
  1027. * TODO: Cleanup or re-implement, since Skill Trees rework it's support was removed.
  1028. * @param npc this parameter contains a reference to the exact instance of the NPC that the player requested the skill info.
  1029. * @param player this parameter contains a reference to the exact instance of the player who requested the skill info.
  1030. * @param skill this parameter contains a reference to the skill that the player requested its info.
  1031. * @return
  1032. */
  1033. public String onAcquireSkillInfo(L2Npc npc, L2PcInstance player, L2Skill skill)
  1034. {
  1035. return null;
  1036. }
  1037. /**
  1038. * This function is called whenever a player acquire a skill.<br>
  1039. * TODO: Cleanup or re-implement, since Skill Trees rework it's support was removed.
  1040. * @param npc this parameter contains a reference to the exact instance of the NPC that the player requested the skill.
  1041. * @param player this parameter contains a reference to the exact instance of the player who requested the skill.
  1042. * @param skill this parameter contains a reference to the skill that the player requested.
  1043. * @return
  1044. */
  1045. public String onAcquireSkill(L2Npc npc, L2PcInstance player, L2Skill skill)
  1046. {
  1047. return null;
  1048. }
  1049. /**
  1050. * This function is called whenever a player uses a quest item that has a quest events list<br>
  1051. * TODO: complete this documentation and unhardcode it to work with all item uses not with those listed.
  1052. * @param item the quest item that the player used
  1053. * @param player the player who used the item
  1054. * @return
  1055. */
  1056. public String onItemUse(L2Item item, L2PcInstance player)
  1057. {
  1058. return null;
  1059. }
  1060. /**
  1061. * This function is called whenever a player casts a skill near a registered NPC (1000 distance).<br>
  1062. * <b>Note:</b><br>
  1063. * If a skill does damage, both onSkillSee(..) and onAttack(..) will be triggered for the damaged NPC!<br>
  1064. * However, only onSkillSee(..) will be triggered if the skill does no damage,<br>
  1065. * or if it damages an NPC who has no onAttack(..) registration while near another NPC who has an onSkillSee registration.<br>
  1066. * TODO: confirm if the distance is 1000 and unhardcode.
  1067. * @param npc the NPC that saw the skill
  1068. * @param caster the player who cast the skill
  1069. * @param skill the actual skill that was used
  1070. * @param targets an array of all objects (can be any type of object, including mobs and players) that were affected by the skill
  1071. * @param isPet if {@code true}, the skill was actually cast by the player's pet, not the player himself
  1072. * @return
  1073. */
  1074. public String onSkillSee(L2Npc npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
  1075. {
  1076. return null;
  1077. }
  1078. /**
  1079. * This function is called whenever an NPC finishes casting a skill.
  1080. * @param npc the NPC that casted the skill.
  1081. * @param player the player who is the target of the skill. Can be {@code null}.
  1082. * @param skill the actual skill that was used by the NPC.
  1083. * @return
  1084. */
  1085. public String onSpellFinished(L2Npc npc, L2PcInstance player, L2Skill skill)
  1086. {
  1087. return null;
  1088. }
  1089. /**
  1090. * This function is called whenever a trap action is performed.
  1091. * @param trap this parameter contains a reference to the exact instance of the trap that was activated.
  1092. * @param trigger this parameter contains a reference to the exact instance of the character that triggered the action.
  1093. * @param action this parameter contains a reference to the action that was triggered.
  1094. * @return
  1095. */
  1096. public String onTrapAction(L2Trap trap, L2Character trigger, TrapAction action)
  1097. {
  1098. return null;
  1099. }
  1100. /**
  1101. * This function is called whenever an NPC spawns or re-spawns and passes a reference to the newly (re)spawned NPC.<br>
  1102. * Currently the only function that has no reference to a player.<br>
  1103. * It is useful for initializations, starting quest timers, displaying chat (NpcSay), and more.
  1104. * @param npc this parameter contains a reference to the exact instance of the NPC who just (re)spawned.
  1105. * @return
  1106. */
  1107. public String onSpawn(L2Npc npc)
  1108. {
  1109. return null;
  1110. }
  1111. /**
  1112. * This function is called whenever an NPC is called by another NPC in the same faction.
  1113. * @param npc this parameter contains a reference to the exact instance of the NPC who is being asked for help.
  1114. * @param caller this parameter contains a reference to the exact instance of the NPC who is asking for help.<br>
  1115. * @param attacker this parameter contains a reference to the exact instance of the player who attacked.
  1116. * @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.
  1117. * @return
  1118. */
  1119. public String onFactionCall(L2Npc npc, L2Npc caller, L2PcInstance attacker, boolean isPet)
  1120. {
  1121. return null;
  1122. }
  1123. /**
  1124. * This function is called whenever a player enters an NPC aggression range.
  1125. * @param npc this parameter contains a reference to the exact instance of the NPC whose aggression range is being transgressed.
  1126. * @param player this parameter contains a reference to the exact instance of the player who is entering the NPC's aggression range.
  1127. * @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.
  1128. * @return
  1129. */
  1130. public String onAggroRangeEnter(L2Npc npc, L2PcInstance player, boolean isPet)
  1131. {
  1132. return null;
  1133. }
  1134. /**
  1135. * This function is called whenever a player enters the game.
  1136. * @param player this parameter contains a reference to the exact instance of the player who is entering to the world.
  1137. * @return
  1138. */
  1139. public String onEnterWorld(L2PcInstance player)
  1140. {
  1141. return null;
  1142. }
  1143. /**
  1144. * This function is called whenever a character enters a registered zone.
  1145. * @param character this parameter contains a reference to the exact instance of the character who is entering the zone.
  1146. * @param zone this parameter contains a reference to the zone.
  1147. * @return
  1148. */
  1149. public String onEnterZone(L2Character character, L2ZoneType zone)
  1150. {
  1151. return null;
  1152. }
  1153. /**
  1154. * This function is called whenever a character exits a registered zone.
  1155. * @param character this parameter contains a reference to the exact instance of the character who is exiting the zone.
  1156. * @param zone this parameter contains a reference to the zone.
  1157. * @return
  1158. */
  1159. public String onExitZone(L2Character character, L2ZoneType zone)
  1160. {
  1161. return null;
  1162. }
  1163. /**
  1164. * This function is called whenever a player wins an Olympiad Game.
  1165. * @param winner this parameter contains a reference to the exact instance of the player who won the competition.
  1166. * @param type this parameter contains a reference to the competition type.
  1167. */
  1168. public void onOlympiadWin(L2PcInstance winner, CompetitionType type)
  1169. {
  1170. }
  1171. /**
  1172. * This function is called whenever a player looses an Olympiad Game.
  1173. * @param loser this parameter contains a reference to the exact instance of the player who lose the competition.
  1174. * @param type this parameter contains a reference to the competition type.
  1175. */
  1176. public void onOlympiadLose(L2PcInstance loser, CompetitionType type)
  1177. {
  1178. }
  1179. /**
  1180. * Show an error message to the specified player.
  1181. * @param player the player to whom to send the error (must be a GM)
  1182. * @param t the {@link Throwable} to get the message/stacktrace from
  1183. * @return {@code false}
  1184. */
  1185. public boolean showError(L2PcInstance player, Throwable t)
  1186. {
  1187. _log.log(Level.WARNING, getScriptFile().getAbsolutePath(), t);
  1188. if (t.getMessage() == null)
  1189. {
  1190. _log.warning(getClass().getSimpleName() + ": " + t.getMessage());
  1191. }
  1192. if ((player != null) && player.getAccessLevel().isGm())
  1193. {
  1194. String res = "<html><body><title>Script error</title>" + Util.getStackTrace(t) + "</body></html>";
  1195. return showResult(player, res);
  1196. }
  1197. return false;
  1198. }
  1199. /**
  1200. * Show a message to the specified player.<br>
  1201. * <u><i>Concept:</i></u><br>
  1202. * Three cases are managed according to the value of the {@code res} parameter:<br>
  1203. * <ul>
  1204. * <li><u>{@code res} ends with ".htm" or ".html":</u> the contents of the specified HTML file are shown in a dialog window</li>
  1205. * <li><u>{@code res} starts with "&lt;html&gt;":</u> the contents of the parameter are shown in a dialog window</li>
  1206. * <li><u>all other cases :</u> the text contained in the parameter is shown in chat</li>
  1207. * </ul>
  1208. * @param player the player to whom to show the result
  1209. * @param res the message to show to the player
  1210. * @return {@code false} if the message was sent, {@code true} otherwise
  1211. */
  1212. public boolean showResult(L2PcInstance player, String res)
  1213. {
  1214. if ((res == null) || res.isEmpty() || (player == null))
  1215. {
  1216. return true;
  1217. }
  1218. if (res.endsWith(".htm") || res.endsWith(".html"))
  1219. {
  1220. showHtmlFile(player, res);
  1221. }
  1222. else if (res.startsWith("<html>"))
  1223. {
  1224. NpcHtmlMessage npcReply = new NpcHtmlMessage(5);
  1225. npcReply.setHtml(res);
  1226. npcReply.replace("%playername%", player.getName());
  1227. player.sendPacket(npcReply);
  1228. player.sendPacket(ActionFailed.STATIC_PACKET);
  1229. }
  1230. else
  1231. {
  1232. player.sendMessage(res);
  1233. }
  1234. return false;
  1235. }
  1236. /**
  1237. * Loads all quest states and variables for the specified player.
  1238. * @param player the player who is entering the world
  1239. */
  1240. public static final void playerEnter(L2PcInstance player)
  1241. {
  1242. Connection con = null;
  1243. try
  1244. {
  1245. // Get list of quests owned by the player from database
  1246. con = L2DatabaseFactory.getInstance().getConnection();
  1247. PreparedStatement invalidQuestData = con.prepareStatement("DELETE FROM character_quests WHERE charId=? AND name=?");
  1248. PreparedStatement invalidQuestDataVar = con.prepareStatement("DELETE FROM character_quests WHERE charId=? AND name=? AND var=?");
  1249. PreparedStatement statement = con.prepareStatement("SELECT name,value FROM character_quests WHERE charId=? AND var=?");
  1250. statement.setInt(1, player.getObjectId());
  1251. statement.setString(2, "<state>");
  1252. ResultSet rs = statement.executeQuery();
  1253. while (rs.next())
  1254. {
  1255. // Get ID of the quest and ID of its state
  1256. String questId = rs.getString("name");
  1257. String statename = rs.getString("value");
  1258. // Search quest associated with the ID
  1259. Quest q = QuestManager.getInstance().getQuest(questId);
  1260. if (q == null)
  1261. {
  1262. _log.finer("Unknown quest " + questId + " for player " + player.getName());
  1263. if (Config.AUTODELETE_INVALID_QUEST_DATA)
  1264. {
  1265. invalidQuestData.setInt(1, player.getObjectId());
  1266. invalidQuestData.setString(2, questId);
  1267. invalidQuestData.executeUpdate();
  1268. }
  1269. continue;
  1270. }
  1271. // Create a new QuestState for the player that will be added to the player's list of quests
  1272. new QuestState(q, player, State.getStateId(statename));
  1273. }
  1274. rs.close();
  1275. invalidQuestData.close();
  1276. statement.close();
  1277. // Get list of quests owned by the player from the DB in order to add variables used in the quest.
  1278. statement = con.prepareStatement("SELECT name,var,value FROM character_quests WHERE charId=? AND var<>?");
  1279. statement.setInt(1, player.getObjectId());
  1280. statement.setString(2, "<state>");
  1281. rs = statement.executeQuery();
  1282. while (rs.next())
  1283. {
  1284. String questId = rs.getString("name");
  1285. String var = rs.getString("var");
  1286. String value = rs.getString("value");
  1287. // Get the QuestState saved in the loop before
  1288. QuestState qs = player.getQuestState(questId);
  1289. if (qs == null)
  1290. {
  1291. _log.finer("Lost variable " + var + " in quest " + questId + " for player " + player.getName());
  1292. if (Config.AUTODELETE_INVALID_QUEST_DATA)
  1293. {
  1294. invalidQuestDataVar.setInt(1, player.getObjectId());
  1295. invalidQuestDataVar.setString(2, questId);
  1296. invalidQuestDataVar.setString(3, var);
  1297. invalidQuestDataVar.executeUpdate();
  1298. }
  1299. continue;
  1300. }
  1301. // Add parameter to the quest
  1302. qs.setInternal(var, value);
  1303. }
  1304. rs.close();
  1305. invalidQuestDataVar.close();
  1306. statement.close();
  1307. }
  1308. catch (Exception e)
  1309. {
  1310. _log.log(Level.WARNING, "could not insert char quest:", e);
  1311. }
  1312. finally
  1313. {
  1314. L2DatabaseFactory.close(con);
  1315. }
  1316. // events
  1317. for (String name : _allEventsS.keySet())
  1318. {
  1319. player.processQuestEvent(name, "enter");
  1320. }
  1321. }
  1322. /**
  1323. * Insert (or update) in the database variables that need to stay persistent for this quest after a reboot.<br>
  1324. * This function is for storage of values that do not related to a specific player but are global for all characters.<br>
  1325. * 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.
  1326. * @param var the name of the variable to save
  1327. * @param value the value of the variable
  1328. */
  1329. public final void saveGlobalQuestVar(String var, String value)
  1330. {
  1331. Connection con = null;
  1332. try
  1333. {
  1334. con = L2DatabaseFactory.getInstance().getConnection();
  1335. PreparedStatement statement = con.prepareStatement("REPLACE INTO quest_global_data (quest_name,var,value) VALUES (?,?,?)");
  1336. statement.setString(1, getName());
  1337. statement.setString(2, var);
  1338. statement.setString(3, value);
  1339. statement.executeUpdate();
  1340. statement.close();
  1341. }
  1342. catch (Exception e)
  1343. {
  1344. _log.log(Level.WARNING, "could not insert global quest variable:", e);
  1345. }
  1346. finally
  1347. {
  1348. L2DatabaseFactory.close(con);
  1349. }
  1350. }
  1351. /**
  1352. * Read from the database a previously saved variable for this quest.<br>
  1353. * Due to performance considerations, this function should best be used only when the quest is first loaded.<br>
  1354. * Subclasses of this class can define structures into which these loaded values can be saved.<br>
  1355. * However, on-demand usage of this function throughout the script is not prohibited, only not recommended.<br>
  1356. * Values read from this function were entered by calls to "saveGlobalQuestVar".
  1357. * @param var the name of the variable to load
  1358. * @return the current value of the specified variable, or an empty string if the variable does not exist
  1359. */
  1360. public final String loadGlobalQuestVar(String var)
  1361. {
  1362. String result = "";
  1363. Connection con = null;
  1364. try
  1365. {
  1366. con = L2DatabaseFactory.getInstance().getConnection();
  1367. PreparedStatement statement;
  1368. statement = con.prepareStatement("SELECT value FROM quest_global_data WHERE quest_name = ? AND var = ?");
  1369. statement.setString(1, getName());
  1370. statement.setString(2, var);
  1371. ResultSet rs = statement.executeQuery();
  1372. if (rs.first())
  1373. {
  1374. result = rs.getString(1);
  1375. }
  1376. rs.close();
  1377. statement.close();
  1378. }
  1379. catch (Exception e)
  1380. {
  1381. _log.log(Level.WARNING, "could not load global quest variable:", e);
  1382. }
  1383. finally
  1384. {
  1385. L2DatabaseFactory.close(con);
  1386. }
  1387. return result;
  1388. }
  1389. /**
  1390. * Permanently delete from the database a global quest variable that was previously saved for this quest.
  1391. * @param var the name of the variable to delete
  1392. */
  1393. public final void deleteGlobalQuestVar(String var)
  1394. {
  1395. Connection con = null;
  1396. try
  1397. {
  1398. con = L2DatabaseFactory.getInstance().getConnection();
  1399. PreparedStatement statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ? AND var = ?");
  1400. statement.setString(1, getName());
  1401. statement.setString(2, var);
  1402. statement.executeUpdate();
  1403. statement.close();
  1404. }
  1405. catch (Exception e)
  1406. {
  1407. _log.log(Level.WARNING, "could not delete global quest variable:", e);
  1408. }
  1409. finally
  1410. {
  1411. L2DatabaseFactory.close(con);
  1412. }
  1413. }
  1414. /**
  1415. * Permanently delete from the database all global quest variables that were previously saved for this quest.
  1416. */
  1417. public final void deleteAllGlobalQuestVars()
  1418. {
  1419. Connection con = null;
  1420. try
  1421. {
  1422. con = L2DatabaseFactory.getInstance().getConnection();
  1423. PreparedStatement statement;
  1424. statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ?");
  1425. statement.setString(1, getName());
  1426. statement.executeUpdate();
  1427. statement.close();
  1428. }
  1429. catch (Exception e)
  1430. {
  1431. _log.log(Level.WARNING, "could not delete global quest variables:", e);
  1432. }
  1433. finally
  1434. {
  1435. L2DatabaseFactory.close(con);
  1436. }
  1437. }
  1438. /**
  1439. * Insert in the database the quest for the player.
  1440. * @param qs the {@link QuestState} object whose variable to insert
  1441. * @param var the name of the variable
  1442. * @param value the value of the variable
  1443. */
  1444. public static void createQuestVarInDb(QuestState qs, String var, String value)
  1445. {
  1446. Connection con = null;
  1447. try
  1448. {
  1449. con = L2DatabaseFactory.getInstance().getConnection();
  1450. PreparedStatement statement = con.prepareStatement("INSERT INTO character_quests (charId,name,var,value) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE value=?");
  1451. statement.setInt(1, qs.getPlayer().getObjectId());
  1452. statement.setString(2, qs.getQuestName());
  1453. statement.setString(3, var);
  1454. statement.setString(4, value);
  1455. statement.setString(5, value);
  1456. statement.executeUpdate();
  1457. statement.close();
  1458. }
  1459. catch (Exception e)
  1460. {
  1461. _log.log(Level.WARNING, "could not insert char quest:", e);
  1462. }
  1463. finally
  1464. {
  1465. L2DatabaseFactory.close(con);
  1466. }
  1467. }
  1468. /**
  1469. * Update the value of the variable "var" for the specified quest in database
  1470. * @param qs the {@link QuestState} object whose variable to update
  1471. * @param var the name of the variable
  1472. * @param value the value of the variable
  1473. */
  1474. public static void updateQuestVarInDb(QuestState qs, String var, String value)
  1475. {
  1476. Connection con = null;
  1477. try
  1478. {
  1479. con = L2DatabaseFactory.getInstance().getConnection();
  1480. PreparedStatement statement = con.prepareStatement("UPDATE character_quests SET value=? WHERE charId=? AND name=? AND var = ?");
  1481. statement.setString(1, value);
  1482. statement.setInt(2, qs.getPlayer().getObjectId());
  1483. statement.setString(3, qs.getQuestName());
  1484. statement.setString(4, var);
  1485. statement.executeUpdate();
  1486. statement.close();
  1487. }
  1488. catch (Exception e)
  1489. {
  1490. _log.log(Level.WARNING, "could not update char quest:", e);
  1491. }
  1492. finally
  1493. {
  1494. L2DatabaseFactory.close(con);
  1495. }
  1496. }
  1497. /**
  1498. * Delete a variable of player's quest from the database.
  1499. * @param qs the {@link QuestState} object whose variable to delete
  1500. * @param var the name of the variable to delete
  1501. */
  1502. public static void deleteQuestVarInDb(QuestState qs, String var)
  1503. {
  1504. Connection con = null;
  1505. try
  1506. {
  1507. con = L2DatabaseFactory.getInstance().getConnection();
  1508. PreparedStatement statement;
  1509. statement = con.prepareStatement("DELETE FROM character_quests WHERE charId=? AND name=? AND var=?");
  1510. statement.setInt(1, qs.getPlayer().getObjectId());
  1511. statement.setString(2, qs.getQuestName());
  1512. statement.setString(3, var);
  1513. statement.executeUpdate();
  1514. statement.close();
  1515. }
  1516. catch (Exception e)
  1517. {
  1518. _log.log(Level.WARNING, "could not delete char quest:", e);
  1519. }
  1520. finally
  1521. {
  1522. L2DatabaseFactory.close(con);
  1523. }
  1524. }
  1525. /**
  1526. * Delete from the database all variables and states of the specified quest state.
  1527. * @param qs the {@link QuestState} object whose variables to delete
  1528. * @param repeatable if {@code false}, the state variable will be preserved, otherwise it will be deleted as well
  1529. */
  1530. public static void deleteQuestInDb(QuestState qs, boolean repeatable)
  1531. {
  1532. Connection con = null;
  1533. try
  1534. {
  1535. con = L2DatabaseFactory.getInstance().getConnection();
  1536. final PreparedStatement statement;
  1537. if (repeatable)
  1538. {
  1539. statement = con.prepareStatement(QUEST_DELETE_FROM_CHAR_QUERY);
  1540. statement.setInt(1, qs.getPlayer().getObjectId());
  1541. statement.setString(2, qs.getQuestName());
  1542. }
  1543. else
  1544. {
  1545. statement = con.prepareStatement(QUEST_DELETE_FROM_CHAR_QUERY_NON_REPEATABLE_QUERY);
  1546. statement.setInt(1, qs.getPlayer().getObjectId());
  1547. statement.setString(2, qs.getQuestName());
  1548. statement.setString(3, "<state>");
  1549. }
  1550. statement.executeUpdate();
  1551. statement.close();
  1552. }
  1553. catch (Exception e)
  1554. {
  1555. _log.log(Level.WARNING, "could not delete char quest:", e);
  1556. }
  1557. finally
  1558. {
  1559. L2DatabaseFactory.close(con);
  1560. }
  1561. }
  1562. /**
  1563. * Create a database record for the specified quest state.
  1564. * @param qs the {@link QuestState} object whose data to write in the database
  1565. */
  1566. public static void createQuestInDb(QuestState qs)
  1567. {
  1568. createQuestVarInDb(qs, "<state>", State.getStateName(qs.getState()));
  1569. }
  1570. /**
  1571. * Update informations regarding quest in database.<br>
  1572. * Actions:<br>
  1573. * <ul>
  1574. * <li>Get ID state of the quest recorded in object qs</li>
  1575. * <li>Test if quest is completed. If true, add a star (*) before the ID state</li>
  1576. * <li>Save in database the ID state (with or without the star) for the variable called "&lt;state&gt;" of the quest</li>
  1577. * </ul>
  1578. * @param qs : QuestState
  1579. */
  1580. public static void updateQuestInDb(QuestState qs)
  1581. {
  1582. String val = State.getStateName(qs.getState());
  1583. updateQuestVarInDb(qs, "<state>", val);
  1584. }
  1585. /**
  1586. * @param player the player whose language settings to use in finding the html of the right language
  1587. * @return the default html for when no quest is available: "You are either not on a quest that involves this NPC.."
  1588. */
  1589. public static String getNoQuestMsg(L2PcInstance player)
  1590. {
  1591. final String result = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/noquest.htm");
  1592. if ((result != null) && (result.length() > 0))
  1593. {
  1594. return result;
  1595. }
  1596. return DEFAULT_NO_QUEST_MSG;
  1597. }
  1598. /**
  1599. * @param player the player whose language settings to use in finding the html of the right language
  1600. * @return the default html for when no quest is already completed: "This quest has already been completed."
  1601. */
  1602. public static String getAlreadyCompletedMsg(L2PcInstance player)
  1603. {
  1604. final String result = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/alreadycompleted.htm");
  1605. if ((result != null) && (result.length() > 0))
  1606. {
  1607. return result;
  1608. }
  1609. return DEFAULT_ALREADY_COMPLETED_MSG;
  1610. }
  1611. /**
  1612. * Add this quest to the list of quests that the passed mob will respond to for the specified Event type.
  1613. * @param npcId : id of the NPC to register
  1614. * @param eventType : type of event being registered
  1615. * @return L2NpcTemplate : Npc Template corresponding to the npcId, or null if the id is invalid
  1616. */
  1617. public L2NpcTemplate addEventId(int npcId, QuestEventType eventType)
  1618. {
  1619. try
  1620. {
  1621. L2NpcTemplate t = NpcTable.getInstance().getTemplate(npcId);
  1622. if (t != null)
  1623. {
  1624. t.addQuestEvent(eventType, this);
  1625. }
  1626. if (!_questInvolvedNpcs.contains(Integer.valueOf(npcId)))
  1627. {
  1628. _questInvolvedNpcs.add(npcId);
  1629. }
  1630. return t;
  1631. }
  1632. catch (Exception e)
  1633. {
  1634. _log.log(Level.WARNING, "Exception on addEventId(): " + e.getMessage(), e);
  1635. return null;
  1636. }
  1637. }
  1638. /**
  1639. * Add the quest to the NPC's startQuest
  1640. * @param npcIds
  1641. * @return L2NpcTemplate : Start NPC
  1642. */
  1643. public L2NpcTemplate[] addStartNpc(int... npcIds)
  1644. {
  1645. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1646. int i = 0;
  1647. for (int npcId : npcIds)
  1648. {
  1649. value[i++] = addEventId(npcId, QuestEventType.QUEST_START);
  1650. }
  1651. return value;
  1652. }
  1653. public L2NpcTemplate addStartNpc(int npcId)
  1654. {
  1655. return addEventId(npcId, QuestEventType.QUEST_START);
  1656. }
  1657. /**
  1658. * Add the quest to the NPC's first-talk (default action dialog)
  1659. * @param npcIds
  1660. * @return L2NpcTemplate : Start NPC
  1661. */
  1662. public L2NpcTemplate[] addFirstTalkId(int... npcIds)
  1663. {
  1664. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1665. int i = 0;
  1666. for (int npcId : npcIds)
  1667. {
  1668. value[i++] = addEventId(npcId, QuestEventType.ON_FIRST_TALK);
  1669. }
  1670. return value;
  1671. }
  1672. /**
  1673. * @param npcId
  1674. * @return
  1675. */
  1676. public L2NpcTemplate addFirstTalkId(int npcId)
  1677. {
  1678. return addEventId(npcId, QuestEventType.ON_FIRST_TALK);
  1679. }
  1680. /**
  1681. * Add the NPC to the AcquireSkill dialog
  1682. * @param npcIds
  1683. * @return L2NpcTemplate : NPC
  1684. */
  1685. public L2NpcTemplate[] addAcquireSkillId(int... npcIds)
  1686. {
  1687. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1688. int i = 0;
  1689. for (int npcId : npcIds)
  1690. {
  1691. value[i++] = addEventId(npcId, QuestEventType.ON_SKILL_LEARN);
  1692. }
  1693. return value;
  1694. }
  1695. /**
  1696. * @param npcId
  1697. * @return
  1698. */
  1699. public L2NpcTemplate addAcquireSkillId(int npcId)
  1700. {
  1701. return addEventId(npcId, QuestEventType.ON_SKILL_LEARN);
  1702. }
  1703. /**
  1704. * Add this quest to the list of quests that the passed mob will respond to for Attack Events.
  1705. * @param npcIds
  1706. * @return int : attackId
  1707. */
  1708. public L2NpcTemplate[] addAttackId(int... npcIds)
  1709. {
  1710. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1711. int i = 0;
  1712. for (int npcId : npcIds)
  1713. {
  1714. value[i++] = addEventId(npcId, QuestEventType.ON_ATTACK);
  1715. }
  1716. return value;
  1717. }
  1718. /**
  1719. * @param npcId
  1720. * @return
  1721. */
  1722. public L2NpcTemplate addAttackId(int npcId)
  1723. {
  1724. return addEventId(npcId, QuestEventType.ON_ATTACK);
  1725. }
  1726. /**
  1727. * Add this quest to the list of quests that the passed mob will respond to for Kill Events.
  1728. * @param killIds
  1729. * @return int : killId
  1730. */
  1731. public L2NpcTemplate[] addKillId(int... killIds)
  1732. {
  1733. L2NpcTemplate[] value = new L2NpcTemplate[killIds.length];
  1734. int i = 0;
  1735. for (int killId : killIds)
  1736. {
  1737. value[i++] = addEventId(killId, QuestEventType.ON_KILL);
  1738. }
  1739. return value;
  1740. }
  1741. /**
  1742. * @param npcId
  1743. * @return
  1744. */
  1745. public L2NpcTemplate addKillId(int npcId)
  1746. {
  1747. return addEventId(npcId, QuestEventType.ON_KILL);
  1748. }
  1749. /**
  1750. * Add this quest to the list of quests that the passed npc will respond to for Talk Events.
  1751. * @param talkIds : ID of the NPC
  1752. * @return int : ID of the NPC
  1753. */
  1754. public L2NpcTemplate[] addTalkId(int... talkIds)
  1755. {
  1756. L2NpcTemplate[] value = new L2NpcTemplate[talkIds.length];
  1757. int i = 0;
  1758. for (int talkId : talkIds)
  1759. {
  1760. value[i++] = addEventId(talkId, QuestEventType.ON_TALK);
  1761. }
  1762. return value;
  1763. }
  1764. /**
  1765. * @param npcId
  1766. * @return
  1767. */
  1768. public L2NpcTemplate addTalkId(int npcId)
  1769. {
  1770. return addEventId(npcId, QuestEventType.ON_TALK);
  1771. }
  1772. /**
  1773. * Add this quest to the list of quests that the passed npc will respond to for Spawn Events.
  1774. * @param npcIds : ID of the NPC
  1775. * @return int : ID of the NPC
  1776. */
  1777. public L2NpcTemplate[] addSpawnId(int... npcIds)
  1778. {
  1779. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1780. int i = 0;
  1781. for (int npcId : npcIds)
  1782. {
  1783. value[i++] = addEventId(npcId, QuestEventType.ON_SPAWN);
  1784. }
  1785. return value;
  1786. }
  1787. /**
  1788. * @param npcId
  1789. * @return
  1790. */
  1791. public L2NpcTemplate addSpawnId(int npcId)
  1792. {
  1793. return addEventId(npcId, QuestEventType.ON_SPAWN);
  1794. }
  1795. /**
  1796. * Add this quest to the list of quests that the passed npc will respond to for Skill-See Events.
  1797. * @param npcIds : ID of the NPC
  1798. * @return int : ID of the NPC
  1799. */
  1800. public L2NpcTemplate[] addSkillSeeId(int... npcIds)
  1801. {
  1802. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1803. int i = 0;
  1804. for (int npcId : npcIds)
  1805. {
  1806. value[i++] = addEventId(npcId, QuestEventType.ON_SKILL_SEE);
  1807. }
  1808. return value;
  1809. }
  1810. /**
  1811. * @param npcId
  1812. * @return
  1813. */
  1814. public L2NpcTemplate addSkillSeeId(int npcId)
  1815. {
  1816. return addEventId(npcId, QuestEventType.ON_SKILL_SEE);
  1817. }
  1818. /**
  1819. * @param npcIds
  1820. * @return
  1821. */
  1822. public L2NpcTemplate[] addSpellFinishedId(int... npcIds)
  1823. {
  1824. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1825. int i = 0;
  1826. for (int npcId : npcIds)
  1827. {
  1828. value[i++] = addEventId(npcId, QuestEventType.ON_SPELL_FINISHED);
  1829. }
  1830. return value;
  1831. }
  1832. /**
  1833. * @param npcId
  1834. * @return
  1835. */
  1836. public L2NpcTemplate addSpellFinishedId(int npcId)
  1837. {
  1838. return addEventId(npcId, QuestEventType.ON_SPELL_FINISHED);
  1839. }
  1840. /**
  1841. * @param npcIds
  1842. * @return
  1843. */
  1844. public L2NpcTemplate[] addTrapActionId(int... npcIds)
  1845. {
  1846. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1847. int i = 0;
  1848. for (int npcId : npcIds)
  1849. {
  1850. value[i++] = addEventId(npcId, QuestEventType.ON_TRAP_ACTION);
  1851. }
  1852. return value;
  1853. }
  1854. /**
  1855. * @param npcId
  1856. * @return
  1857. */
  1858. public L2NpcTemplate addTrapActionId(int npcId)
  1859. {
  1860. return addEventId(npcId, QuestEventType.ON_TRAP_ACTION);
  1861. }
  1862. /**
  1863. * Add this quest to the list of quests that the passed npc will respond to for Faction Call Events.
  1864. * @param npcIds : ID of the NPC
  1865. * @return int : ID of the NPC
  1866. */
  1867. public L2NpcTemplate[] addFactionCallId(int... npcIds)
  1868. {
  1869. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1870. int i = 0;
  1871. for (int npcId : npcIds)
  1872. {
  1873. value[i++] = addEventId(npcId, QuestEventType.ON_FACTION_CALL);
  1874. }
  1875. return value;
  1876. }
  1877. /**
  1878. * @param npcId
  1879. * @return
  1880. */
  1881. public L2NpcTemplate addFactionCallId(int npcId)
  1882. {
  1883. return addEventId(npcId, QuestEventType.ON_FACTION_CALL);
  1884. }
  1885. /**
  1886. * Add this quest to the list of quests that the passed npc will respond to for Character See Events.
  1887. * @param npcIds : ID of the NPC
  1888. * @return int : ID of the NPC
  1889. */
  1890. public L2NpcTemplate[] addAggroRangeEnterId(int... npcIds)
  1891. {
  1892. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1893. int i = 0;
  1894. for (int npcId : npcIds)
  1895. {
  1896. value[i++] = addEventId(npcId, QuestEventType.ON_AGGRO_RANGE_ENTER);
  1897. }
  1898. return value;
  1899. }
  1900. /**
  1901. * @param npcId
  1902. * @return
  1903. */
  1904. public L2NpcTemplate addAggroRangeEnterId(int npcId)
  1905. {
  1906. return addEventId(npcId, QuestEventType.ON_AGGRO_RANGE_ENTER);
  1907. }
  1908. /**
  1909. * @param zoneIds
  1910. * @return
  1911. */
  1912. public L2ZoneType[] addEnterZoneId(int... zoneIds)
  1913. {
  1914. L2ZoneType[] value = new L2ZoneType[zoneIds.length];
  1915. int i = 0;
  1916. for (int zoneId : zoneIds)
  1917. {
  1918. try
  1919. {
  1920. L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
  1921. if (zone != null)
  1922. {
  1923. zone.addQuestEvent(QuestEventType.ON_ENTER_ZONE, this);
  1924. }
  1925. value[i++] = zone;
  1926. }
  1927. catch (Exception e)
  1928. {
  1929. _log.log(Level.WARNING, "Exception on addEnterZoneId(): " + e.getMessage(), e);
  1930. continue;
  1931. }
  1932. }
  1933. return value;
  1934. }
  1935. /**
  1936. * @param zoneId
  1937. * @return
  1938. */
  1939. public L2ZoneType addEnterZoneId(int zoneId)
  1940. {
  1941. try
  1942. {
  1943. L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
  1944. if (zone != null)
  1945. {
  1946. zone.addQuestEvent(QuestEventType.ON_ENTER_ZONE, this);
  1947. }
  1948. return zone;
  1949. }
  1950. catch (Exception e)
  1951. {
  1952. _log.log(Level.WARNING, "Exception on addEnterZoneId(): " + e.getMessage(), e);
  1953. return null;
  1954. }
  1955. }
  1956. /**
  1957. * @param zoneIds
  1958. * @return
  1959. */
  1960. public L2ZoneType[] addExitZoneId(int... zoneIds)
  1961. {
  1962. L2ZoneType[] value = new L2ZoneType[zoneIds.length];
  1963. int i = 0;
  1964. for (int zoneId : zoneIds)
  1965. {
  1966. try
  1967. {
  1968. L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
  1969. if (zone != null)
  1970. {
  1971. zone.addQuestEvent(QuestEventType.ON_EXIT_ZONE, this);
  1972. }
  1973. value[i++] = zone;
  1974. }
  1975. catch (Exception e)
  1976. {
  1977. _log.log(Level.WARNING, "Exception on addEnterZoneId(): " + e.getMessage(), e);
  1978. continue;
  1979. }
  1980. }
  1981. return value;
  1982. }
  1983. /**
  1984. * @param zoneId
  1985. * @return
  1986. */
  1987. public L2ZoneType addExitZoneId(int zoneId)
  1988. {
  1989. try
  1990. {
  1991. L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
  1992. if (zone != null)
  1993. {
  1994. zone.addQuestEvent(QuestEventType.ON_EXIT_ZONE, this);
  1995. }
  1996. return zone;
  1997. }
  1998. catch (Exception e)
  1999. {
  2000. _log.log(Level.WARNING, "Exception on addExitZoneId(): " + e.getMessage(), e);
  2001. return null;
  2002. }
  2003. }
  2004. /**
  2005. * Use this method to get a random party member from a player's party.<br>
  2006. * Useful when distributing rewards after killing an NPC.
  2007. * @param player this parameter represents the player whom the party will taken.
  2008. * @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
  2009. */
  2010. public L2PcInstance getRandomPartyMember(L2PcInstance player)
  2011. {
  2012. if (player == null)
  2013. {
  2014. return null;
  2015. }
  2016. final L2Party party = player.getParty();
  2017. if ((party == null) || (party.getMembers().isEmpty()))
  2018. {
  2019. return player;
  2020. }
  2021. return party.getMembers().get(Rnd.get(party.getMembers().size()));
  2022. }
  2023. /**
  2024. * Auxiliary function for party quests.<br>
  2025. * Note: This function is only here because of how commonly it may be used by quest developers.<br>
  2026. * For any variations on this function, the quest script can always handle things on its own.
  2027. * @param player the instance of a player whose party is to be searched
  2028. * @param value the value of the "cond" variable that must be matched
  2029. * @return L2PcInstance: L2PcInstance for a random party member that matches the specified condition, or {@code null} if no match was found
  2030. */
  2031. public L2PcInstance getRandomPartyMember(L2PcInstance player, String value)
  2032. {
  2033. return getRandomPartyMember(player, "cond", value);
  2034. }
  2035. /**
  2036. * Auxiliary function for party quests.<br>
  2037. * Note: This function is only here because of how commonly it may be used by quest developers.<br>
  2038. * For any variations on this function, the quest script can always handle things on its own.
  2039. * @param player the instance of a player whose party is to be searched
  2040. * @param var the quest variable to look for in party members. If {@code null}, it simply unconditionally returns a random party member
  2041. * @param value the value of the specified quest variable the random party member must have
  2042. * @return a random party member that matches the specified conditions or {@code null} if no match was found.<br>
  2043. * If the {@code var} parameter is {@code null}, a random party member is selected without any conditions.<br>
  2044. * 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
  2045. */
  2046. public L2PcInstance getRandomPartyMember(L2PcInstance player, String var, String value)
  2047. {
  2048. // if no valid player instance is passed, there is nothing to check...
  2049. if (player == null)
  2050. {
  2051. return null;
  2052. }
  2053. // for null var condition, return any random party member.
  2054. if (var == null)
  2055. {
  2056. return getRandomPartyMember(player);
  2057. }
  2058. // normal cases...if the player is not in a party, check the player's state
  2059. QuestState temp = null;
  2060. L2Party party = player.getParty();
  2061. // if this player is not in a party, just check if this player instance matches the conditions itself
  2062. if ((party == null) || (party.getMembers().isEmpty()))
  2063. {
  2064. temp = player.getQuestState(getName());
  2065. if ((temp != null) && (temp.get(var) != null) && (temp.get(var)).equalsIgnoreCase(value))
  2066. {
  2067. return player; // match
  2068. }
  2069. return null; // no match
  2070. }
  2071. // if the player is in a party, gather a list of all matching party members (possibly
  2072. // including this player)
  2073. List<L2PcInstance> candidates = new ArrayList<>();
  2074. // get the target for enforcing distance limitations.
  2075. L2Object target = player.getTarget();
  2076. if (target == null)
  2077. {
  2078. target = player;
  2079. }
  2080. for (L2PcInstance partyMember : party.getMembers())
  2081. {
  2082. if (partyMember == null)
  2083. {
  2084. continue;
  2085. }
  2086. temp = partyMember.getQuestState(getName());
  2087. if ((temp != null) && (temp.get(var) != null) && (temp.get(var)).equalsIgnoreCase(value) && partyMember.isInsideRadius(target, 1500, true, false))
  2088. {
  2089. candidates.add(partyMember);
  2090. }
  2091. }
  2092. // if there was no match, return null...
  2093. if (candidates.isEmpty())
  2094. {
  2095. return null;
  2096. }
  2097. // TODO where's the range check?
  2098. // if a match was found from the party, return one of them at random.
  2099. return candidates.get(Rnd.get(candidates.size()));
  2100. }
  2101. /**
  2102. * Auxiliary function for party quests.<br>
  2103. * Note: This function is only here because of how commonly it may be used by quest developers.<br>
  2104. * For any variations on this function, the quest script can always handle things on its own.
  2105. * @param player the player whose random party member is to be selected
  2106. * @param state the quest state required of the random party member
  2107. * @return {@code null} if nothing was selected or a random party member that has the specified quest state
  2108. */
  2109. public L2PcInstance getRandomPartyMemberState(L2PcInstance player, byte state)
  2110. {
  2111. // if no valid player instance is passed, there is nothing to check...
  2112. if (player == null)
  2113. {
  2114. return null;
  2115. }
  2116. // normal cases...if the player is not in a partym check the player's state
  2117. QuestState temp = null;
  2118. L2Party party = player.getParty();
  2119. // if this player is not in a party, just check if this player instance matches the conditions itself
  2120. if ((party == null) || (party.getMembers().isEmpty()))
  2121. {
  2122. temp = player.getQuestState(getName());
  2123. if ((temp != null) && (temp.getState() == state))
  2124. {
  2125. return player; // match
  2126. }
  2127. return null; // no match
  2128. }
  2129. // if the player is in a party, gather a list of all matching party members (possibly
  2130. // including this player)
  2131. List<L2PcInstance> candidates = new ArrayList<>();
  2132. // get the target for enforcing distance limitations.
  2133. L2Object target = player.getTarget();
  2134. if (target == null)
  2135. {
  2136. target = player;
  2137. }
  2138. for (L2PcInstance partyMember : party.getMembers())
  2139. {
  2140. if (partyMember == null)
  2141. {
  2142. continue;
  2143. }
  2144. temp = partyMember.getQuestState(getName());
  2145. if ((temp != null) && (temp.getState() == state) && partyMember.isInsideRadius(target, 1500, true, false))
  2146. {
  2147. candidates.add(partyMember);
  2148. }
  2149. }
  2150. // if there was no match, return null...
  2151. if (candidates.isEmpty())
  2152. {
  2153. return null;
  2154. }
  2155. // if a match was found from the party, return one of them at random.
  2156. return candidates.get(Rnd.get(candidates.size()));
  2157. }
  2158. /**
  2159. * Show HTML file to client
  2160. * @param player
  2161. * @param fileName
  2162. * @return String : message sent to client
  2163. */
  2164. public String showHtmlFile(L2PcInstance player, String fileName)
  2165. {
  2166. boolean questwindow = true;
  2167. if (fileName.endsWith(".html"))
  2168. {
  2169. questwindow = false;
  2170. }
  2171. int questId = getQuestIntId();
  2172. // Create handler to file linked to the quest
  2173. String content = getHtm(player.getHtmlPrefix(), fileName);
  2174. if (player.getTarget() != null)
  2175. {
  2176. content = content.replaceAll("%objectId%", String.valueOf(player.getTarget().getObjectId()));
  2177. }
  2178. // Send message to client if message not empty
  2179. if (content != null)
  2180. {
  2181. if (questwindow && (questId > 0) && (questId < 20000) && (questId != 999))
  2182. {
  2183. NpcQuestHtmlMessage npcReply = new NpcQuestHtmlMessage(5, questId);
  2184. npcReply.setHtml(content);
  2185. npcReply.replace("%playername%", player.getName());
  2186. player.sendPacket(npcReply);
  2187. }
  2188. else
  2189. {
  2190. NpcHtmlMessage npcReply = new NpcHtmlMessage(5);
  2191. npcReply.setHtml(content);
  2192. npcReply.replace("%playername%", player.getName());
  2193. player.sendPacket(npcReply);
  2194. }
  2195. player.sendPacket(ActionFailed.STATIC_PACKET);
  2196. }
  2197. return content;
  2198. }
  2199. /**
  2200. * Return HTML file contents
  2201. * @param prefix player's language prefix.
  2202. * @param fileName the html file to be get.
  2203. * @return
  2204. */
  2205. public String getHtm(String prefix, String fileName)
  2206. {
  2207. String content = HtmCache.getInstance().getHtm(prefix, "data/scripts/" + getDescr().toLowerCase() + "/" + getName() + "/" + fileName);
  2208. if (content == null)
  2209. {
  2210. content = HtmCache.getInstance().getHtm(prefix, "data/scripts/quests/Q" + getName() + "/" + fileName);
  2211. if (content == null)
  2212. {
  2213. content = HtmCache.getInstance().getHtmForce(prefix, "data/scripts/quests/" + getName() + "/" + fileName);
  2214. }
  2215. }
  2216. return content;
  2217. }
  2218. /**
  2219. * Add a temporary (quest) spawn
  2220. * @param npcId
  2221. * @param cha
  2222. * @return instance of newly spawned npc
  2223. */
  2224. public L2Npc addSpawn(int npcId, L2Character cha)
  2225. {
  2226. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), false, 0, false);
  2227. }
  2228. /**
  2229. * Add a temporary (quest) spawn
  2230. * @param npcId
  2231. * @param cha
  2232. * @param isSummonSpawn
  2233. * @return instance of newly spawned npc with summon animation
  2234. */
  2235. public L2Npc addSpawn(int npcId, L2Character cha, boolean isSummonSpawn)
  2236. {
  2237. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), false, 0, isSummonSpawn);
  2238. }
  2239. /**
  2240. * @param npcId
  2241. * @param x
  2242. * @param y
  2243. * @param z
  2244. * @param heading
  2245. * @param randomOffSet
  2246. * @param despawnDelay
  2247. * @return
  2248. */
  2249. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffSet, long despawnDelay)
  2250. {
  2251. return addSpawn(npcId, x, y, z, heading, randomOffSet, despawnDelay, false);
  2252. }
  2253. /**
  2254. * @param npcId
  2255. * @param loc
  2256. * @param randomOffSet
  2257. * @param despawnDelay
  2258. * @return
  2259. */
  2260. public L2Npc addSpawn(int npcId, Location loc, boolean randomOffSet, long despawnDelay)
  2261. {
  2262. return addSpawn(npcId, loc.getX(), loc.getY(), loc.getZ(), loc.getHeading(), randomOffSet, despawnDelay, false);
  2263. }
  2264. /**
  2265. * @param npcId
  2266. * @param x
  2267. * @param y
  2268. * @param z
  2269. * @param heading
  2270. * @param randomOffset
  2271. * @param despawnDelay
  2272. * @param isSummonSpawn
  2273. * @return
  2274. */
  2275. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, long despawnDelay, boolean isSummonSpawn)
  2276. {
  2277. return addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, isSummonSpawn, 0);
  2278. }
  2279. /**
  2280. * @param npcId
  2281. * @param loc
  2282. * @param randomOffset
  2283. * @param despawnDelay
  2284. * @param isSummonSpawn
  2285. * @return
  2286. */
  2287. public L2Npc addSpawn(int npcId, Location loc, boolean randomOffset, long despawnDelay, boolean isSummonSpawn)
  2288. {
  2289. return addSpawn(npcId, loc.getX(), loc.getY(), loc.getZ(), loc.getHeading(), randomOffset, despawnDelay, isSummonSpawn, 0);
  2290. }
  2291. /**
  2292. * @param npcId
  2293. * @param x
  2294. * @param y
  2295. * @param z
  2296. * @param heading
  2297. * @param randomOffset
  2298. * @param despawnDelay
  2299. * @param isSummonSpawn
  2300. * @param instanceId
  2301. * @return
  2302. */
  2303. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, long despawnDelay, boolean isSummonSpawn, int instanceId)
  2304. {
  2305. L2Npc result = null;
  2306. try
  2307. {
  2308. L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId);
  2309. if (template != null)
  2310. {
  2311. // Sometimes, even if the quest script specifies some xyz (for example npc.getX() etc) by the time the code
  2312. // reaches here, xyz have become 0! Also, a questdev might have purposely set xy to 0,0...however,
  2313. // the spawn code is coded such that if x=y=0, it looks into location for the spawn loc! This will NOT work
  2314. // with quest spawns! For both of the above cases, we need a fail-safe spawn. For this, we use the
  2315. // default spawn location, which is at the player's loc.
  2316. if ((x == 0) && (y == 0))
  2317. {
  2318. _log.log(Level.SEVERE, "Failed to adjust bad locks for quest spawn! Spawn aborted!");
  2319. return null;
  2320. }
  2321. if (randomOffset)
  2322. {
  2323. int offset;
  2324. offset = Rnd.get(2); // Get the direction of the offset
  2325. if (offset == 0)
  2326. {
  2327. // make offset negative
  2328. offset = -1;
  2329. }
  2330. offset *= Rnd.get(50, 100);
  2331. x += offset;
  2332. offset = Rnd.get(2); // Get the direction of the offset
  2333. if (offset == 0)
  2334. {
  2335. // make offset negative
  2336. offset = -1;
  2337. }
  2338. offset *= Rnd.get(50, 100);
  2339. y += offset;
  2340. }
  2341. L2Spawn spawn = new L2Spawn(template);
  2342. spawn.setInstanceId(instanceId);
  2343. spawn.setHeading(heading);
  2344. spawn.setLocx(x);
  2345. spawn.setLocy(y);
  2346. spawn.setLocz(z);
  2347. spawn.stopRespawn();
  2348. result = spawn.spawnOne(isSummonSpawn);
  2349. if (despawnDelay > 0)
  2350. {
  2351. result.scheduleDespawn(despawnDelay);
  2352. }
  2353. return result;
  2354. }
  2355. }
  2356. catch (Exception e1)
  2357. {
  2358. _log.warning("Could not spawn Npc " + npcId + " Error: " + e1.getMessage());
  2359. }
  2360. return null;
  2361. }
  2362. /**
  2363. * @param trapId
  2364. * @param x
  2365. * @param y
  2366. * @param z
  2367. * @param heading
  2368. * @param skill
  2369. * @param instanceId
  2370. * @return
  2371. */
  2372. public L2Trap addTrap(int trapId, int x, int y, int z, int heading, L2Skill skill, int instanceId)
  2373. {
  2374. L2NpcTemplate TrapTemplate = NpcTable.getInstance().getTemplate(trapId);
  2375. L2Trap trap = new L2TrapInstance(IdFactory.getInstance().getNextId(), TrapTemplate, instanceId, -1, skill);
  2376. trap.setCurrentHp(trap.getMaxHp());
  2377. trap.setCurrentMp(trap.getMaxMp());
  2378. trap.setIsInvul(true);
  2379. trap.setHeading(heading);
  2380. // L2World.getInstance().storeObject(trap);
  2381. trap.spawnMe(x, y, z);
  2382. return trap;
  2383. }
  2384. /**
  2385. * @param master
  2386. * @param minionId
  2387. * @return
  2388. */
  2389. public L2Npc addMinion(L2MonsterInstance master, int minionId)
  2390. {
  2391. return MinionList.spawnMinion(master, minionId);
  2392. }
  2393. /**
  2394. * @return the registered quest items Ids.
  2395. */
  2396. public int[] getRegisteredItemIds()
  2397. {
  2398. return questItemIds;
  2399. }
  2400. @Override
  2401. public String getScriptName()
  2402. {
  2403. return getName();
  2404. }
  2405. @Override
  2406. public void setActive(boolean status)
  2407. {
  2408. // TODO implement me
  2409. }
  2410. @Override
  2411. public boolean reload()
  2412. {
  2413. unload();
  2414. return super.reload();
  2415. }
  2416. @Override
  2417. public boolean unload()
  2418. {
  2419. return unload(true);
  2420. }
  2421. /**
  2422. * @param removeFromList
  2423. * @return
  2424. */
  2425. public boolean unload(boolean removeFromList)
  2426. {
  2427. saveGlobalData();
  2428. // cancel all pending timers before reloading.
  2429. // if timers ought to be restarted, the quest can take care of it
  2430. // with its code (example: save global data indicating what timer must
  2431. // be restarted).
  2432. for (List<QuestTimer> timers : _allEventTimers.values())
  2433. {
  2434. _readLock.lock();
  2435. try
  2436. {
  2437. for (QuestTimer timer : timers)
  2438. {
  2439. timer.cancel();
  2440. }
  2441. }
  2442. finally
  2443. {
  2444. _readLock.unlock();
  2445. }
  2446. timers.clear();
  2447. }
  2448. _allEventTimers.clear();
  2449. for (Integer npcId : _questInvolvedNpcs)
  2450. {
  2451. L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId.intValue());
  2452. if (template != null)
  2453. {
  2454. template.removeQuest(this);
  2455. }
  2456. }
  2457. _questInvolvedNpcs.clear();
  2458. if (removeFromList)
  2459. {
  2460. return QuestManager.getInstance().removeQuest(this);
  2461. }
  2462. return true;
  2463. }
  2464. @Override
  2465. public ScriptManager<?> getScriptManager()
  2466. {
  2467. return QuestManager.getInstance();
  2468. }
  2469. /**
  2470. * @param val
  2471. */
  2472. public void setOnEnterWorld(boolean val)
  2473. {
  2474. _onEnterWorld = val;
  2475. }
  2476. /**
  2477. * @return
  2478. */
  2479. public boolean getOnEnterWorld()
  2480. {
  2481. return _onEnterWorld;
  2482. }
  2483. /**
  2484. * If a quest is set as custom, it will display it's name in the NPC Quest List.<br>
  2485. * Retail quests are unhardcoded to display the name using a client string.
  2486. * @param val if {@code true} the quest script will be set as custom quest.
  2487. */
  2488. public void setIsCustom(boolean val)
  2489. {
  2490. _isCustom = val;
  2491. }
  2492. /**
  2493. * @return {@code true} if the quest script is a custom quest, {@code false} otherwise.
  2494. */
  2495. public boolean isCustomQuest()
  2496. {
  2497. return _isCustom;
  2498. }
  2499. /**
  2500. * @param val
  2501. */
  2502. public void setOlympiadUse(boolean val)
  2503. {
  2504. _isOlympiadUse = val;
  2505. }
  2506. /**
  2507. * @return {@code true} if the quest script is used for Olympiad quests, {@code false} otherwise.
  2508. */
  2509. public boolean isOlympiadUse()
  2510. {
  2511. return _isOlympiadUse;
  2512. }
  2513. /**
  2514. * @param player this parameter contains a reference to the player to check.
  2515. * @param itemId the item wanted to be count.
  2516. * @return the quantity of one sort of item hold by the player.
  2517. */
  2518. public long getQuestItemsCount(L2PcInstance player, int itemId)
  2519. {
  2520. long count = 0;
  2521. for (L2ItemInstance item : player.getInventory().getItems())
  2522. {
  2523. if ((item != null) && (item.getItemId() == itemId))
  2524. {
  2525. count += item.getCount();
  2526. }
  2527. }
  2528. return count;
  2529. }
  2530. /**
  2531. * @param player the player whose inventory to check for quest items
  2532. * @param itemId the ID of the item to check for
  2533. * @return {@code true} if the item exists in player's inventory, {@code false} otherwise
  2534. */
  2535. public boolean hasQuestItems(L2PcInstance player, int itemId)
  2536. {
  2537. return player.getInventory().getItemByItemId(itemId) != null;
  2538. }
  2539. /**
  2540. * @param player the player whose inventory to check for quest items
  2541. * @param itemIds a list or array of item IDs to check for
  2542. * @return {@code true} if all items exist in player's inventory, {@code false} otherwise
  2543. */
  2544. public boolean hasQuestItems(L2PcInstance player, int... itemIds)
  2545. {
  2546. final PcInventory inv = player.getInventory();
  2547. for (int itemId : itemIds)
  2548. {
  2549. if (inv.getItemByItemId(itemId) == null)
  2550. {
  2551. return false;
  2552. }
  2553. }
  2554. return true;
  2555. }
  2556. /**
  2557. * @param player the player whose item to check
  2558. * @param itemId the ID of the item whose enchantment to check
  2559. * @return the enchantment level of the item or 0 if no item was found
  2560. */
  2561. public int getEnchantLevel(L2PcInstance player, int itemId)
  2562. {
  2563. L2ItemInstance enchanteditem = player.getInventory().getItemByItemId(itemId);
  2564. if (enchanteditem == null)
  2565. {
  2566. return 0;
  2567. }
  2568. return enchanteditem.getEnchantLevel();
  2569. }
  2570. /**
  2571. * Give Adena to the player.
  2572. * @param player the player to whom to give the Adena
  2573. * @param count the amount of Adena to give
  2574. * @param applyRates if {@code true}, quest rates will be applied
  2575. */
  2576. public void giveAdena(L2PcInstance player, long count, boolean applyRates)
  2577. {
  2578. giveItems(player, PcInventory.ADENA_ID, count, applyRates ? 0 : 1);
  2579. }
  2580. /**
  2581. * Give reward to player using multipliers.
  2582. * @param player the player to whom to give the item
  2583. * @param itemId the ID of the item to give
  2584. * @param count the amount of items to give
  2585. */
  2586. public void rewardItems(L2PcInstance player, int itemId, long count)
  2587. {
  2588. if (count <= 0)
  2589. {
  2590. return;
  2591. }
  2592. L2ItemInstance _tmpItem = ItemTable.getInstance().createDummyItem(itemId);
  2593. if (_tmpItem == null)
  2594. {
  2595. return;
  2596. }
  2597. if (itemId == PcInventory.ADENA_ID)
  2598. {
  2599. count = (long) (count * Config.RATE_QUEST_REWARD_ADENA);
  2600. }
  2601. else if (Config.RATE_QUEST_REWARD_USE_MULTIPLIERS)
  2602. {
  2603. if (_tmpItem.isEtcItem())
  2604. {
  2605. switch (_tmpItem.getEtcItem().getItemType())
  2606. {
  2607. case POTION:
  2608. count = (long) (count * Config.RATE_QUEST_REWARD_POTION);
  2609. break;
  2610. case SCRL_ENCHANT_WP:
  2611. case SCRL_ENCHANT_AM:
  2612. case SCROLL:
  2613. count = (long) (count * Config.RATE_QUEST_REWARD_SCROLL);
  2614. break;
  2615. case RECIPE:
  2616. count = (long) (count * Config.RATE_QUEST_REWARD_RECIPE);
  2617. break;
  2618. case MATERIAL:
  2619. count = (long) (count * Config.RATE_QUEST_REWARD_MATERIAL);
  2620. break;
  2621. default:
  2622. count = (long) (count * Config.RATE_QUEST_REWARD);
  2623. }
  2624. }
  2625. }
  2626. else
  2627. {
  2628. count = (long) (count * Config.RATE_QUEST_REWARD);
  2629. }
  2630. // Add items to player's inventory
  2631. L2ItemInstance item = player.getInventory().addItem("Quest", itemId, count, player, player.getTarget());
  2632. if (item == null)
  2633. {
  2634. return;
  2635. }
  2636. sendItemGetMessage(player, item, count);
  2637. }
  2638. /**
  2639. * Send the system message and the status update packets to the player.
  2640. * @param player the player that has got the item
  2641. * @param item the item obtain by the player
  2642. * @param count the item count
  2643. */
  2644. private void sendItemGetMessage(L2PcInstance player, L2ItemInstance item, long count)
  2645. {
  2646. // If item for reward is gold, send message of gold reward to client
  2647. if (item.getItemId() == PcInventory.ADENA_ID)
  2648. {
  2649. SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S1_ADENA);
  2650. smsg.addItemNumber(count);
  2651. player.sendPacket(smsg);
  2652. }
  2653. // Otherwise, send message of object reward to client
  2654. else
  2655. {
  2656. if (count > 1)
  2657. {
  2658. SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S2_S1_S);
  2659. smsg.addItemName(item);
  2660. smsg.addItemNumber(count);
  2661. player.sendPacket(smsg);
  2662. }
  2663. else
  2664. {
  2665. SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
  2666. smsg.addItemName(item);
  2667. player.sendPacket(smsg);
  2668. }
  2669. }
  2670. // send packets
  2671. StatusUpdate su = new StatusUpdate(player);
  2672. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  2673. player.sendPacket(su);
  2674. }
  2675. /**
  2676. * Give item/reward to the player
  2677. * @param player
  2678. * @param itemId
  2679. * @param count
  2680. */
  2681. public void giveItems(L2PcInstance player, int itemId, long count)
  2682. {
  2683. giveItems(player, itemId, count, 0);
  2684. }
  2685. /**
  2686. * @param player
  2687. * @param itemId
  2688. * @param count
  2689. * @param enchantlevel
  2690. */
  2691. public void giveItems(L2PcInstance player, int itemId, long count, int enchantlevel)
  2692. {
  2693. if (count <= 0)
  2694. {
  2695. return;
  2696. }
  2697. // If item for reward is adena (ID=57), modify count with rate for quest reward if rates available
  2698. if ((itemId == PcInventory.ADENA_ID) && !(enchantlevel > 0))
  2699. {
  2700. count = (long) (count * Config.RATE_QUEST_REWARD_ADENA);
  2701. }
  2702. // Add items to player's inventory
  2703. L2ItemInstance item = player.getInventory().addItem("Quest", itemId, count, player, player.getTarget());
  2704. if (item == null)
  2705. {
  2706. return;
  2707. }
  2708. // set enchant level for item if that item is not adena
  2709. if ((enchantlevel > 0) && (itemId != PcInventory.ADENA_ID))
  2710. {
  2711. item.setEnchantLevel(enchantlevel);
  2712. }
  2713. sendItemGetMessage(player, item, count);
  2714. }
  2715. /**
  2716. * @param player
  2717. * @param itemId
  2718. * @param count
  2719. * @param attributeId
  2720. * @param attributeLevel
  2721. */
  2722. public void giveItems(L2PcInstance player, int itemId, long count, byte attributeId, int attributeLevel)
  2723. {
  2724. if (count <= 0)
  2725. {
  2726. return;
  2727. }
  2728. // Add items to player's inventory
  2729. L2ItemInstance item = player.getInventory().addItem("Quest", itemId, count, player, player.getTarget());
  2730. if (item == null)
  2731. {
  2732. return;
  2733. }
  2734. // set enchant level for item if that item is not adena
  2735. if ((attributeId >= 0) && (attributeLevel > 0))
  2736. {
  2737. item.setElementAttr(attributeId, attributeLevel);
  2738. if (item.isEquipped())
  2739. {
  2740. item.updateElementAttrBonus(player);
  2741. }
  2742. InventoryUpdate iu = new InventoryUpdate();
  2743. iu.addModifiedItem(item);
  2744. player.sendPacket(iu);
  2745. }
  2746. sendItemGetMessage(player, item, count);
  2747. }
  2748. /**
  2749. * Drop Quest item using Config.RATE_QUEST_DROP
  2750. * @param player
  2751. * @param itemId int Item Identifier of the item to be dropped
  2752. * @param count (minCount, maxCount) long Quantity of items to be dropped
  2753. * @param neededCount Quantity of items needed for quest
  2754. * @param dropChance int Base chance of drop, same as in droplist
  2755. * @param sound boolean indicating whether to play sound
  2756. * @return boolean indicating whether player has requested number of items
  2757. */
  2758. public boolean dropQuestItems(L2PcInstance player, int itemId, int count, long neededCount, int dropChance, boolean sound)
  2759. {
  2760. return dropQuestItems(player, itemId, count, count, neededCount, dropChance, sound);
  2761. }
  2762. /**
  2763. * @param player
  2764. * @param itemId
  2765. * @param minCount
  2766. * @param maxCount
  2767. * @param neededCount
  2768. * @param dropChance
  2769. * @param sound
  2770. * @return
  2771. */
  2772. public boolean dropQuestItems(L2PcInstance player, int itemId, int minCount, int maxCount, long neededCount, int dropChance, boolean sound)
  2773. {
  2774. dropChance *= Config.RATE_QUEST_DROP / ((player.getParty() != null) ? player.getParty().getMemberCount() : 1);
  2775. long currentCount = getQuestItemsCount(player, itemId);
  2776. if ((neededCount > 0) && (currentCount >= neededCount))
  2777. {
  2778. return true;
  2779. }
  2780. if (currentCount >= neededCount)
  2781. {
  2782. return true;
  2783. }
  2784. long itemCount = 0;
  2785. int random = Rnd.get(L2DropData.MAX_CHANCE);
  2786. while (random < dropChance)
  2787. {
  2788. // Get the item quantity dropped
  2789. if (minCount < maxCount)
  2790. {
  2791. itemCount += Rnd.get(minCount, maxCount);
  2792. }
  2793. else if (minCount == maxCount)
  2794. {
  2795. itemCount += minCount;
  2796. }
  2797. else
  2798. {
  2799. itemCount++;
  2800. }
  2801. // Prepare for next iteration if dropChance > L2DropData.MAX_CHANCE
  2802. dropChance -= L2DropData.MAX_CHANCE;
  2803. }
  2804. if (itemCount > 0)
  2805. {
  2806. // if over neededCount, just fill the gap
  2807. if ((neededCount > 0) && ((currentCount + itemCount) > neededCount))
  2808. {
  2809. itemCount = neededCount - currentCount;
  2810. }
  2811. // Inventory slot check
  2812. if (!player.getInventory().validateCapacityByItemId(itemId))
  2813. {
  2814. return false;
  2815. }
  2816. // Give the item to Player
  2817. player.addItem("Quest", itemId, itemCount, player.getTarget(), true);
  2818. if (sound)
  2819. {
  2820. playSound(player, ((currentCount + itemCount) < neededCount) ? "Itemsound.quest_itemget" : "Itemsound.quest_middle");
  2821. }
  2822. }
  2823. return ((neededCount > 0) && ((currentCount + itemCount) >= neededCount));
  2824. }
  2825. /**
  2826. * Remove items from player's inventory when talking to NPC in order to have rewards.<br>
  2827. * <u><i>Actions :</i></u>
  2828. * <ul>
  2829. * <li>Destroy quantity of items wanted</li>
  2830. * <li>Send new inventory list to player</li>
  2831. * </ul>
  2832. * @param player
  2833. * @param itemId : Identifier of the item
  2834. * @param count : Quantity of items to destroy
  2835. */
  2836. public void takeItems(L2PcInstance player, int itemId, long count)
  2837. {
  2838. // Get object item from player's inventory list
  2839. L2ItemInstance item = player.getInventory().getItemByItemId(itemId);
  2840. if (item == null)
  2841. {
  2842. return;
  2843. }
  2844. // Tests on count value in order not to have negative value
  2845. if ((count < 0) || (count > item.getCount()))
  2846. {
  2847. count = item.getCount();
  2848. }
  2849. // Destroy the quantity of items wanted
  2850. if (item.isEquipped())
  2851. {
  2852. L2ItemInstance[] unequiped = player.getInventory().unEquipItemInBodySlotAndRecord(item.getItem().getBodyPart());
  2853. InventoryUpdate iu = new InventoryUpdate();
  2854. for (L2ItemInstance itm : unequiped)
  2855. {
  2856. iu.addModifiedItem(itm);
  2857. }
  2858. player.sendPacket(iu);
  2859. player.broadcastUserInfo();
  2860. }
  2861. player.destroyItemByItemId("Quest", itemId, count, player, true);
  2862. }
  2863. /**
  2864. * Send a packet in order to play sound at client terminal
  2865. * @param player
  2866. * @param sound
  2867. */
  2868. public void playSound(L2PcInstance player, String sound)
  2869. {
  2870. player.sendPacket(new PlaySound(sound));
  2871. }
  2872. /**
  2873. * Add XP and SP as quest reward
  2874. * @param player
  2875. * @param exp
  2876. * @param sp
  2877. */
  2878. public void addExpAndSp(L2PcInstance player, int exp, int sp)
  2879. {
  2880. 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));
  2881. }
  2882. /**
  2883. * Gets a random integer number from 0 (inclusive) to {@code max} (exclusive).<br>
  2884. * Use this method instead importing {@link com.l2jserver.util.Rnd} utility.
  2885. * @param max this parameter represents the maximum value for randomization.
  2886. * @return a random integer number from 0 to {@code max} - 1.
  2887. */
  2888. public static int getRandom(int max)
  2889. {
  2890. return Rnd.get(max);
  2891. }
  2892. /**
  2893. * Gets a random integer number from {@code min} (inclusive) to {@code max} (inclusive).<br>
  2894. * Use this method instead importing {@link com.l2jserver.util.Rnd} utility.
  2895. * @param min this parameter represents the minimum value for randomization.
  2896. * @param max this parameter represents the maximum value for randomization.
  2897. * @return a random integer number from {@code min} to {@code max} .
  2898. */
  2899. public static int getRandom(int min, int max)
  2900. {
  2901. return Rnd.get(min, max);
  2902. }
  2903. /**
  2904. * @param player this parameter is a reference to the player.
  2905. * @param slot this parameter represents the location in the player's inventory.
  2906. * @return the item Id of the item present in the inventory slot {@code slot} if it's not null, 0 otherwise.
  2907. */
  2908. public int getItemEquipped(L2PcInstance player, int slot)
  2909. {
  2910. return player.getInventory().getPaperdollItemId(slot);
  2911. }
  2912. /**
  2913. * @return the number of ticks from the {@link com.l2jserver.gameserver.GameTimeController}.
  2914. */
  2915. public int getGameTicks()
  2916. {
  2917. return GameTimeController.getGameTicks();
  2918. }
  2919. }