Quest.java 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095
  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. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1243. PreparedStatement invalidQuestData = con.prepareStatement("DELETE FROM character_quests WHERE charId = ? AND name = ?");
  1244. PreparedStatement invalidQuestDataVar = con.prepareStatement("DELETE FROM character_quests WHERE charId = ? AND name = ? AND var = ?");
  1245. PreparedStatement ps1 = con.prepareStatement("SELECT name, value FROM character_quests WHERE charId = ? AND var = ?"))
  1246. {
  1247. // Get list of quests owned by the player from database
  1248. ps1.setInt(1, player.getObjectId());
  1249. ps1.setString(2, "<state>");
  1250. try (ResultSet rs = ps1.executeQuery())
  1251. {
  1252. while (rs.next())
  1253. {
  1254. // Get ID of the quest and ID of its state
  1255. String questId = rs.getString("name");
  1256. String statename = rs.getString("value");
  1257. // Search quest associated with the ID
  1258. Quest q = QuestManager.getInstance().getQuest(questId);
  1259. if (q == null)
  1260. {
  1261. _log.finer("Unknown quest " + questId + " for player " + player.getName());
  1262. if (Config.AUTODELETE_INVALID_QUEST_DATA)
  1263. {
  1264. invalidQuestData.setInt(1, player.getObjectId());
  1265. invalidQuestData.setString(2, questId);
  1266. invalidQuestData.executeUpdate();
  1267. }
  1268. continue;
  1269. }
  1270. // Create a new QuestState for the player that will be added to the player's list of quests
  1271. new QuestState(q, player, State.getStateId(statename));
  1272. }
  1273. }
  1274. // Get list of quests owned by the player from the DB in order to add variables used in the quest.
  1275. try (PreparedStatement ps2 = con.prepareStatement("SELECT name, var, value FROM character_quests WHERE charId = ? AND var <> ?"))
  1276. {
  1277. ps2.setInt(1, player.getObjectId());
  1278. ps2.setString(2, "<state>");
  1279. try (ResultSet rs = ps2.executeQuery())
  1280. {
  1281. while (rs.next())
  1282. {
  1283. String questId = rs.getString("name");
  1284. String var = rs.getString("var");
  1285. String value = rs.getString("value");
  1286. // Get the QuestState saved in the loop before
  1287. QuestState qs = player.getQuestState(questId);
  1288. if (qs == null)
  1289. {
  1290. _log.finer("Lost variable " + var + " in quest " + questId + " for player " + player.getName());
  1291. if (Config.AUTODELETE_INVALID_QUEST_DATA)
  1292. {
  1293. invalidQuestDataVar.setInt(1, player.getObjectId());
  1294. invalidQuestDataVar.setString(2, questId);
  1295. invalidQuestDataVar.setString(3, var);
  1296. invalidQuestDataVar.executeUpdate();
  1297. }
  1298. continue;
  1299. }
  1300. // Add parameter to the quest
  1301. qs.setInternal(var, value);
  1302. }
  1303. }
  1304. }
  1305. }
  1306. catch (Exception e)
  1307. {
  1308. _log.log(Level.WARNING, "could not insert char quest:", e);
  1309. }
  1310. // events
  1311. for (String name : _allEventsS.keySet())
  1312. {
  1313. player.processQuestEvent(name, "enter");
  1314. }
  1315. }
  1316. /**
  1317. * Insert (or update) in the database variables that need to stay persistent for this quest after a reboot.<br>
  1318. * This function is for storage of values that do not related to a specific player but are global for all characters.<br>
  1319. * 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.
  1320. * @param var the name of the variable to save
  1321. * @param value the value of the variable
  1322. */
  1323. public final void saveGlobalQuestVar(String var, String value)
  1324. {
  1325. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1326. PreparedStatement statement = con.prepareStatement("REPLACE INTO quest_global_data (quest_name,var,value) VALUES (?,?,?)"))
  1327. {
  1328. statement.setString(1, getName());
  1329. statement.setString(2, var);
  1330. statement.setString(3, value);
  1331. statement.executeUpdate();
  1332. }
  1333. catch (Exception e)
  1334. {
  1335. _log.log(Level.WARNING, "could not insert global quest variable:", e);
  1336. }
  1337. }
  1338. /**
  1339. * Read from the database a previously saved variable for this quest.<br>
  1340. * Due to performance considerations, this function should best be used only when the quest is first loaded.<br>
  1341. * Subclasses of this class can define structures into which these loaded values can be saved.<br>
  1342. * However, on-demand usage of this function throughout the script is not prohibited, only not recommended.<br>
  1343. * Values read from this function were entered by calls to "saveGlobalQuestVar".
  1344. * @param var the name of the variable to load
  1345. * @return the current value of the specified variable, or an empty string if the variable does not exist
  1346. */
  1347. public final String loadGlobalQuestVar(String var)
  1348. {
  1349. String result = "";
  1350. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1351. PreparedStatement statement = con.prepareStatement("SELECT value FROM quest_global_data WHERE quest_name = ? AND var = ?"))
  1352. {
  1353. statement.setString(1, getName());
  1354. statement.setString(2, var);
  1355. try (ResultSet rs = statement.executeQuery())
  1356. {
  1357. if (rs.first())
  1358. {
  1359. result = rs.getString(1);
  1360. }
  1361. }
  1362. }
  1363. catch (Exception e)
  1364. {
  1365. _log.log(Level.WARNING, "could not load global quest variable:", e);
  1366. }
  1367. return result;
  1368. }
  1369. /**
  1370. * Permanently delete from the database a global quest variable that was previously saved for this quest.
  1371. * @param var the name of the variable to delete
  1372. */
  1373. public final void deleteGlobalQuestVar(String var)
  1374. {
  1375. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1376. PreparedStatement statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ? AND var = ?"))
  1377. {
  1378. statement.setString(1, getName());
  1379. statement.setString(2, var);
  1380. statement.executeUpdate();
  1381. }
  1382. catch (Exception e)
  1383. {
  1384. _log.log(Level.WARNING, "could not delete global quest variable:", e);
  1385. }
  1386. }
  1387. /**
  1388. * Permanently delete from the database all global quest variables that were previously saved for this quest.
  1389. */
  1390. public final void deleteAllGlobalQuestVars()
  1391. {
  1392. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1393. PreparedStatement statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ?"))
  1394. {
  1395. statement.setString(1, getName());
  1396. statement.executeUpdate();
  1397. }
  1398. catch (Exception e)
  1399. {
  1400. _log.log(Level.WARNING, "could not delete global quest variables:", e);
  1401. }
  1402. }
  1403. /**
  1404. * Insert in the database the quest for the player.
  1405. * @param qs the {@link QuestState} object whose variable to insert
  1406. * @param var the name of the variable
  1407. * @param value the value of the variable
  1408. */
  1409. public static void createQuestVarInDb(QuestState qs, String var, String value)
  1410. {
  1411. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1412. PreparedStatement statement = con.prepareStatement("INSERT INTO character_quests (charId,name,var,value) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE value=?"))
  1413. {
  1414. statement.setInt(1, qs.getPlayer().getObjectId());
  1415. statement.setString(2, qs.getQuestName());
  1416. statement.setString(3, var);
  1417. statement.setString(4, value);
  1418. statement.setString(5, value);
  1419. statement.executeUpdate();
  1420. }
  1421. catch (Exception e)
  1422. {
  1423. _log.log(Level.WARNING, "could not insert char quest:", e);
  1424. }
  1425. }
  1426. /**
  1427. * Update the value of the variable "var" for the specified quest in database
  1428. * @param qs the {@link QuestState} object whose variable to update
  1429. * @param var the name of the variable
  1430. * @param value the value of the variable
  1431. */
  1432. public static void updateQuestVarInDb(QuestState qs, String var, String value)
  1433. {
  1434. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1435. PreparedStatement statement = con.prepareStatement("UPDATE character_quests SET value=? WHERE charId=? AND name=? AND var = ?"))
  1436. {
  1437. statement.setString(1, value);
  1438. statement.setInt(2, qs.getPlayer().getObjectId());
  1439. statement.setString(3, qs.getQuestName());
  1440. statement.setString(4, var);
  1441. statement.executeUpdate();
  1442. }
  1443. catch (Exception e)
  1444. {
  1445. _log.log(Level.WARNING, "could not update char quest:", e);
  1446. }
  1447. }
  1448. /**
  1449. * Delete a variable of player's quest from the database.
  1450. * @param qs the {@link QuestState} object whose variable to delete
  1451. * @param var the name of the variable to delete
  1452. */
  1453. public static void deleteQuestVarInDb(QuestState qs, String var)
  1454. {
  1455. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1456. PreparedStatement statement = con.prepareStatement("DELETE FROM character_quests WHERE charId=? AND name=? AND var=?"))
  1457. {
  1458. statement.setInt(1, qs.getPlayer().getObjectId());
  1459. statement.setString(2, qs.getQuestName());
  1460. statement.setString(3, var);
  1461. statement.executeUpdate();
  1462. }
  1463. catch (Exception e)
  1464. {
  1465. _log.log(Level.WARNING, "could not delete char quest:", e);
  1466. }
  1467. }
  1468. /**
  1469. * Delete from the database all variables and states of the specified quest state.
  1470. * @param qs the {@link QuestState} object whose variables to delete
  1471. * @param repeatable if {@code false}, the state variable will be preserved, otherwise it will be deleted as well
  1472. */
  1473. public static void deleteQuestInDb(QuestState qs, boolean repeatable)
  1474. {
  1475. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1476. PreparedStatement ps = con.prepareStatement(repeatable ? QUEST_DELETE_FROM_CHAR_QUERY : QUEST_DELETE_FROM_CHAR_QUERY_NON_REPEATABLE_QUERY))
  1477. {
  1478. ps.setInt(1, qs.getPlayer().getObjectId());
  1479. ps.setString(2, qs.getQuestName());
  1480. if (!repeatable)
  1481. {
  1482. ps.setString(3, "<state>");
  1483. }
  1484. ps.executeUpdate();
  1485. }
  1486. catch (Exception e)
  1487. {
  1488. _log.log(Level.WARNING, "could not delete char quest:", e);
  1489. }
  1490. }
  1491. /**
  1492. * Create a database record for the specified quest state.
  1493. * @param qs the {@link QuestState} object whose data to write in the database
  1494. */
  1495. public static void createQuestInDb(QuestState qs)
  1496. {
  1497. createQuestVarInDb(qs, "<state>", State.getStateName(qs.getState()));
  1498. }
  1499. /**
  1500. * Update informations regarding quest in database.<br>
  1501. * Actions:<br>
  1502. * <ul>
  1503. * <li>Get ID state of the quest recorded in object qs</li>
  1504. * <li>Test if quest is completed. If true, add a star (*) before the ID state</li>
  1505. * <li>Save in database the ID state (with or without the star) for the variable called "&lt;state&gt;" of the quest</li>
  1506. * </ul>
  1507. * @param qs : QuestState
  1508. */
  1509. public static void updateQuestInDb(QuestState qs)
  1510. {
  1511. String val = State.getStateName(qs.getState());
  1512. updateQuestVarInDb(qs, "<state>", val);
  1513. }
  1514. /**
  1515. * @param player the player whose language settings to use in finding the html of the right language
  1516. * @return the default html for when no quest is available: "You are either not on a quest that involves this NPC.."
  1517. */
  1518. public static String getNoQuestMsg(L2PcInstance player)
  1519. {
  1520. final String result = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/noquest.htm");
  1521. if ((result != null) && (result.length() > 0))
  1522. {
  1523. return result;
  1524. }
  1525. return DEFAULT_NO_QUEST_MSG;
  1526. }
  1527. /**
  1528. * @param player the player whose language settings to use in finding the html of the right language
  1529. * @return the default html for when no quest is already completed: "This quest has already been completed."
  1530. */
  1531. public static String getAlreadyCompletedMsg(L2PcInstance player)
  1532. {
  1533. final String result = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/alreadycompleted.htm");
  1534. if ((result != null) && (result.length() > 0))
  1535. {
  1536. return result;
  1537. }
  1538. return DEFAULT_ALREADY_COMPLETED_MSG;
  1539. }
  1540. /**
  1541. * Add this quest to the list of quests that the passed mob will respond to for the specified Event type.
  1542. * @param npcId : id of the NPC to register
  1543. * @param eventType : type of event being registered
  1544. * @return L2NpcTemplate : Npc Template corresponding to the npcId, or null if the id is invalid
  1545. */
  1546. public L2NpcTemplate addEventId(int npcId, QuestEventType eventType)
  1547. {
  1548. try
  1549. {
  1550. L2NpcTemplate t = NpcTable.getInstance().getTemplate(npcId);
  1551. if (t != null)
  1552. {
  1553. t.addQuestEvent(eventType, this);
  1554. }
  1555. if (!_questInvolvedNpcs.contains(Integer.valueOf(npcId)))
  1556. {
  1557. _questInvolvedNpcs.add(npcId);
  1558. }
  1559. return t;
  1560. }
  1561. catch (Exception e)
  1562. {
  1563. _log.log(Level.WARNING, "Exception on addEventId(): " + e.getMessage(), e);
  1564. return null;
  1565. }
  1566. }
  1567. /**
  1568. * Add the quest to the NPC's startQuest
  1569. * @param npcIds
  1570. * @return L2NpcTemplate : Start NPC
  1571. */
  1572. public L2NpcTemplate[] addStartNpc(int... npcIds)
  1573. {
  1574. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1575. int i = 0;
  1576. for (int npcId : npcIds)
  1577. {
  1578. value[i++] = addEventId(npcId, QuestEventType.QUEST_START);
  1579. }
  1580. return value;
  1581. }
  1582. public L2NpcTemplate addStartNpc(int npcId)
  1583. {
  1584. return addEventId(npcId, QuestEventType.QUEST_START);
  1585. }
  1586. /**
  1587. * Add the quest to the NPC's first-talk (default action dialog)
  1588. * @param npcIds
  1589. * @return L2NpcTemplate : Start NPC
  1590. */
  1591. public L2NpcTemplate[] addFirstTalkId(int... npcIds)
  1592. {
  1593. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1594. int i = 0;
  1595. for (int npcId : npcIds)
  1596. {
  1597. value[i++] = addEventId(npcId, QuestEventType.ON_FIRST_TALK);
  1598. }
  1599. return value;
  1600. }
  1601. /**
  1602. * @param npcId
  1603. * @return
  1604. */
  1605. public L2NpcTemplate addFirstTalkId(int npcId)
  1606. {
  1607. return addEventId(npcId, QuestEventType.ON_FIRST_TALK);
  1608. }
  1609. /**
  1610. * Add the NPC to the AcquireSkill dialog
  1611. * @param npcIds
  1612. * @return L2NpcTemplate : NPC
  1613. */
  1614. public L2NpcTemplate[] addAcquireSkillId(int... npcIds)
  1615. {
  1616. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1617. int i = 0;
  1618. for (int npcId : npcIds)
  1619. {
  1620. value[i++] = addEventId(npcId, QuestEventType.ON_SKILL_LEARN);
  1621. }
  1622. return value;
  1623. }
  1624. /**
  1625. * @param npcId
  1626. * @return
  1627. */
  1628. public L2NpcTemplate addAcquireSkillId(int npcId)
  1629. {
  1630. return addEventId(npcId, QuestEventType.ON_SKILL_LEARN);
  1631. }
  1632. /**
  1633. * Add this quest to the list of quests that the passed mob will respond to for Attack Events.
  1634. * @param npcIds
  1635. * @return int : attackId
  1636. */
  1637. public L2NpcTemplate[] addAttackId(int... npcIds)
  1638. {
  1639. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1640. int i = 0;
  1641. for (int npcId : npcIds)
  1642. {
  1643. value[i++] = addEventId(npcId, QuestEventType.ON_ATTACK);
  1644. }
  1645. return value;
  1646. }
  1647. /**
  1648. * @param npcId
  1649. * @return
  1650. */
  1651. public L2NpcTemplate addAttackId(int npcId)
  1652. {
  1653. return addEventId(npcId, QuestEventType.ON_ATTACK);
  1654. }
  1655. /**
  1656. * Add this quest to the list of quests that the passed mob will respond to for Kill Events.
  1657. * @param killIds
  1658. * @return int : killId
  1659. */
  1660. public L2NpcTemplate[] addKillId(int... killIds)
  1661. {
  1662. L2NpcTemplate[] value = new L2NpcTemplate[killIds.length];
  1663. int i = 0;
  1664. for (int killId : killIds)
  1665. {
  1666. value[i++] = addEventId(killId, QuestEventType.ON_KILL);
  1667. }
  1668. return value;
  1669. }
  1670. /**
  1671. * @param npcId
  1672. * @return
  1673. */
  1674. public L2NpcTemplate addKillId(int npcId)
  1675. {
  1676. return addEventId(npcId, QuestEventType.ON_KILL);
  1677. }
  1678. /**
  1679. * Add this quest to the list of quests that the passed npc will respond to for Talk Events.
  1680. * @param talkIds : ID of the NPC
  1681. * @return int : ID of the NPC
  1682. */
  1683. public L2NpcTemplate[] addTalkId(int... talkIds)
  1684. {
  1685. L2NpcTemplate[] value = new L2NpcTemplate[talkIds.length];
  1686. int i = 0;
  1687. for (int talkId : talkIds)
  1688. {
  1689. value[i++] = addEventId(talkId, QuestEventType.ON_TALK);
  1690. }
  1691. return value;
  1692. }
  1693. /**
  1694. * @param npcId
  1695. * @return
  1696. */
  1697. public L2NpcTemplate addTalkId(int npcId)
  1698. {
  1699. return addEventId(npcId, QuestEventType.ON_TALK);
  1700. }
  1701. /**
  1702. * Add this quest to the list of quests that the passed npc will respond to for Spawn Events.
  1703. * @param npcIds : ID of the NPC
  1704. * @return int : ID of the NPC
  1705. */
  1706. public L2NpcTemplate[] addSpawnId(int... npcIds)
  1707. {
  1708. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1709. int i = 0;
  1710. for (int npcId : npcIds)
  1711. {
  1712. value[i++] = addEventId(npcId, QuestEventType.ON_SPAWN);
  1713. }
  1714. return value;
  1715. }
  1716. /**
  1717. * @param npcId
  1718. * @return
  1719. */
  1720. public L2NpcTemplate addSpawnId(int npcId)
  1721. {
  1722. return addEventId(npcId, QuestEventType.ON_SPAWN);
  1723. }
  1724. /**
  1725. * Add this quest to the list of quests that the passed npc will respond to for Skill-See Events.
  1726. * @param npcIds : ID of the NPC
  1727. * @return int : ID of the NPC
  1728. */
  1729. public L2NpcTemplate[] addSkillSeeId(int... npcIds)
  1730. {
  1731. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1732. int i = 0;
  1733. for (int npcId : npcIds)
  1734. {
  1735. value[i++] = addEventId(npcId, QuestEventType.ON_SKILL_SEE);
  1736. }
  1737. return value;
  1738. }
  1739. /**
  1740. * @param npcId
  1741. * @return
  1742. */
  1743. public L2NpcTemplate addSkillSeeId(int npcId)
  1744. {
  1745. return addEventId(npcId, QuestEventType.ON_SKILL_SEE);
  1746. }
  1747. /**
  1748. * @param npcIds
  1749. * @return
  1750. */
  1751. public L2NpcTemplate[] addSpellFinishedId(int... npcIds)
  1752. {
  1753. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1754. int i = 0;
  1755. for (int npcId : npcIds)
  1756. {
  1757. value[i++] = addEventId(npcId, QuestEventType.ON_SPELL_FINISHED);
  1758. }
  1759. return value;
  1760. }
  1761. /**
  1762. * @param npcId
  1763. * @return
  1764. */
  1765. public L2NpcTemplate addSpellFinishedId(int npcId)
  1766. {
  1767. return addEventId(npcId, QuestEventType.ON_SPELL_FINISHED);
  1768. }
  1769. /**
  1770. * @param npcIds
  1771. * @return
  1772. */
  1773. public L2NpcTemplate[] addTrapActionId(int... npcIds)
  1774. {
  1775. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1776. int i = 0;
  1777. for (int npcId : npcIds)
  1778. {
  1779. value[i++] = addEventId(npcId, QuestEventType.ON_TRAP_ACTION);
  1780. }
  1781. return value;
  1782. }
  1783. /**
  1784. * @param npcId
  1785. * @return
  1786. */
  1787. public L2NpcTemplate addTrapActionId(int npcId)
  1788. {
  1789. return addEventId(npcId, QuestEventType.ON_TRAP_ACTION);
  1790. }
  1791. /**
  1792. * Add this quest to the list of quests that the passed npc will respond to for Faction Call Events.
  1793. * @param npcIds : ID of the NPC
  1794. * @return int : ID of the NPC
  1795. */
  1796. public L2NpcTemplate[] addFactionCallId(int... npcIds)
  1797. {
  1798. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1799. int i = 0;
  1800. for (int npcId : npcIds)
  1801. {
  1802. value[i++] = addEventId(npcId, QuestEventType.ON_FACTION_CALL);
  1803. }
  1804. return value;
  1805. }
  1806. /**
  1807. * @param npcId
  1808. * @return
  1809. */
  1810. public L2NpcTemplate addFactionCallId(int npcId)
  1811. {
  1812. return addEventId(npcId, QuestEventType.ON_FACTION_CALL);
  1813. }
  1814. /**
  1815. * Add this quest to the list of quests that the passed npc will respond to for Character See Events.
  1816. * @param npcIds : ID of the NPC
  1817. * @return int : ID of the NPC
  1818. */
  1819. public L2NpcTemplate[] addAggroRangeEnterId(int... npcIds)
  1820. {
  1821. L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
  1822. int i = 0;
  1823. for (int npcId : npcIds)
  1824. {
  1825. value[i++] = addEventId(npcId, QuestEventType.ON_AGGRO_RANGE_ENTER);
  1826. }
  1827. return value;
  1828. }
  1829. /**
  1830. * @param npcId
  1831. * @return
  1832. */
  1833. public L2NpcTemplate addAggroRangeEnterId(int npcId)
  1834. {
  1835. return addEventId(npcId, QuestEventType.ON_AGGRO_RANGE_ENTER);
  1836. }
  1837. /**
  1838. * @param zoneIds
  1839. * @return
  1840. */
  1841. public L2ZoneType[] addEnterZoneId(int... zoneIds)
  1842. {
  1843. L2ZoneType[] value = new L2ZoneType[zoneIds.length];
  1844. int i = 0;
  1845. for (int zoneId : zoneIds)
  1846. {
  1847. try
  1848. {
  1849. L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
  1850. if (zone != null)
  1851. {
  1852. zone.addQuestEvent(QuestEventType.ON_ENTER_ZONE, this);
  1853. }
  1854. value[i++] = zone;
  1855. }
  1856. catch (Exception e)
  1857. {
  1858. _log.log(Level.WARNING, "Exception on addEnterZoneId(): " + e.getMessage(), e);
  1859. continue;
  1860. }
  1861. }
  1862. return value;
  1863. }
  1864. /**
  1865. * @param zoneId
  1866. * @return
  1867. */
  1868. public L2ZoneType addEnterZoneId(int zoneId)
  1869. {
  1870. try
  1871. {
  1872. L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
  1873. if (zone != null)
  1874. {
  1875. zone.addQuestEvent(QuestEventType.ON_ENTER_ZONE, this);
  1876. }
  1877. return zone;
  1878. }
  1879. catch (Exception e)
  1880. {
  1881. _log.log(Level.WARNING, "Exception on addEnterZoneId(): " + e.getMessage(), e);
  1882. return null;
  1883. }
  1884. }
  1885. /**
  1886. * @param zoneIds
  1887. * @return
  1888. */
  1889. public L2ZoneType[] addExitZoneId(int... zoneIds)
  1890. {
  1891. L2ZoneType[] value = new L2ZoneType[zoneIds.length];
  1892. int i = 0;
  1893. for (int zoneId : zoneIds)
  1894. {
  1895. try
  1896. {
  1897. L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
  1898. if (zone != null)
  1899. {
  1900. zone.addQuestEvent(QuestEventType.ON_EXIT_ZONE, this);
  1901. }
  1902. value[i++] = zone;
  1903. }
  1904. catch (Exception e)
  1905. {
  1906. _log.log(Level.WARNING, "Exception on addEnterZoneId(): " + e.getMessage(), e);
  1907. continue;
  1908. }
  1909. }
  1910. return value;
  1911. }
  1912. /**
  1913. * @param zoneId
  1914. * @return
  1915. */
  1916. public L2ZoneType addExitZoneId(int zoneId)
  1917. {
  1918. try
  1919. {
  1920. L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
  1921. if (zone != null)
  1922. {
  1923. zone.addQuestEvent(QuestEventType.ON_EXIT_ZONE, this);
  1924. }
  1925. return zone;
  1926. }
  1927. catch (Exception e)
  1928. {
  1929. _log.log(Level.WARNING, "Exception on addExitZoneId(): " + e.getMessage(), e);
  1930. return null;
  1931. }
  1932. }
  1933. /**
  1934. * Use this method to get a random party member from a player's party.<br>
  1935. * Useful when distributing rewards after killing an NPC.
  1936. * @param player this parameter represents the player whom the party will taken.
  1937. * @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
  1938. */
  1939. public L2PcInstance getRandomPartyMember(L2PcInstance player)
  1940. {
  1941. if (player == null)
  1942. {
  1943. return null;
  1944. }
  1945. final L2Party party = player.getParty();
  1946. if ((party == null) || (party.getMembers().isEmpty()))
  1947. {
  1948. return player;
  1949. }
  1950. return party.getMembers().get(Rnd.get(party.getMembers().size()));
  1951. }
  1952. /**
  1953. * Auxiliary function for party quests.<br>
  1954. * Note: This function is only here because of how commonly it may be used by quest developers.<br>
  1955. * For any variations on this function, the quest script can always handle things on its own.
  1956. * @param player the instance of a player whose party is to be searched
  1957. * @param value the value of the "cond" variable that must be matched
  1958. * @return L2PcInstance: L2PcInstance for a random party member that matches the specified condition, or {@code null} if no match was found
  1959. */
  1960. public L2PcInstance getRandomPartyMember(L2PcInstance player, String value)
  1961. {
  1962. return getRandomPartyMember(player, "cond", value);
  1963. }
  1964. /**
  1965. * Auxiliary function for party quests.<br>
  1966. * Note: This function is only here because of how commonly it may be used by quest developers.<br>
  1967. * For any variations on this function, the quest script can always handle things on its own.
  1968. * @param player the instance of a player whose party is to be searched
  1969. * @param var the quest variable to look for in party members. If {@code null}, it simply unconditionally returns a random party member
  1970. * @param value the value of the specified quest variable the random party member must have
  1971. * @return a random party member that matches the specified conditions or {@code null} if no match was found.<br>
  1972. * If the {@code var} parameter is {@code null}, a random party member is selected without any conditions.<br>
  1973. * 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
  1974. */
  1975. public L2PcInstance getRandomPartyMember(L2PcInstance player, String var, String value)
  1976. {
  1977. // if no valid player instance is passed, there is nothing to check...
  1978. if (player == null)
  1979. {
  1980. return null;
  1981. }
  1982. // for null var condition, return any random party member.
  1983. if (var == null)
  1984. {
  1985. return getRandomPartyMember(player);
  1986. }
  1987. // normal cases...if the player is not in a party, check the player's state
  1988. QuestState temp = null;
  1989. L2Party party = player.getParty();
  1990. // if this player is not in a party, just check if this player instance matches the conditions itself
  1991. if ((party == null) || (party.getMembers().isEmpty()))
  1992. {
  1993. temp = player.getQuestState(getName());
  1994. if ((temp != null) && (temp.get(var) != null) && (temp.get(var)).equalsIgnoreCase(value))
  1995. {
  1996. return player; // match
  1997. }
  1998. return null; // no match
  1999. }
  2000. // if the player is in a party, gather a list of all matching party members (possibly
  2001. // including this player)
  2002. List<L2PcInstance> candidates = new ArrayList<>();
  2003. // get the target for enforcing distance limitations.
  2004. L2Object target = player.getTarget();
  2005. if (target == null)
  2006. {
  2007. target = player;
  2008. }
  2009. for (L2PcInstance partyMember : party.getMembers())
  2010. {
  2011. if (partyMember == null)
  2012. {
  2013. continue;
  2014. }
  2015. temp = partyMember.getQuestState(getName());
  2016. if ((temp != null) && (temp.get(var) != null) && (temp.get(var)).equalsIgnoreCase(value) && partyMember.isInsideRadius(target, 1500, true, false))
  2017. {
  2018. candidates.add(partyMember);
  2019. }
  2020. }
  2021. // if there was no match, return null...
  2022. if (candidates.isEmpty())
  2023. {
  2024. return null;
  2025. }
  2026. // TODO where's the range check?
  2027. // if a match was found from the party, return one of them at random.
  2028. return candidates.get(Rnd.get(candidates.size()));
  2029. }
  2030. /**
  2031. * Auxiliary function for party quests.<br>
  2032. * Note: This function is only here because of how commonly it may be used by quest developers.<br>
  2033. * For any variations on this function, the quest script can always handle things on its own.
  2034. * @param player the player whose random party member is to be selected
  2035. * @param state the quest state required of the random party member
  2036. * @return {@code null} if nothing was selected or a random party member that has the specified quest state
  2037. */
  2038. public L2PcInstance getRandomPartyMemberState(L2PcInstance player, byte state)
  2039. {
  2040. // if no valid player instance is passed, there is nothing to check...
  2041. if (player == null)
  2042. {
  2043. return null;
  2044. }
  2045. // normal cases...if the player is not in a partym check the player's state
  2046. QuestState temp = null;
  2047. L2Party party = player.getParty();
  2048. // if this player is not in a party, just check if this player instance matches the conditions itself
  2049. if ((party == null) || (party.getMembers().isEmpty()))
  2050. {
  2051. temp = player.getQuestState(getName());
  2052. if ((temp != null) && (temp.getState() == state))
  2053. {
  2054. return player; // match
  2055. }
  2056. return null; // no match
  2057. }
  2058. // if the player is in a party, gather a list of all matching party members (possibly
  2059. // including this player)
  2060. List<L2PcInstance> candidates = new ArrayList<>();
  2061. // get the target for enforcing distance limitations.
  2062. L2Object target = player.getTarget();
  2063. if (target == null)
  2064. {
  2065. target = player;
  2066. }
  2067. for (L2PcInstance partyMember : party.getMembers())
  2068. {
  2069. if (partyMember == null)
  2070. {
  2071. continue;
  2072. }
  2073. temp = partyMember.getQuestState(getName());
  2074. if ((temp != null) && (temp.getState() == state) && partyMember.isInsideRadius(target, 1500, true, false))
  2075. {
  2076. candidates.add(partyMember);
  2077. }
  2078. }
  2079. // if there was no match, return null...
  2080. if (candidates.isEmpty())
  2081. {
  2082. return null;
  2083. }
  2084. // if a match was found from the party, return one of them at random.
  2085. return candidates.get(Rnd.get(candidates.size()));
  2086. }
  2087. /**
  2088. * Show HTML file to client
  2089. * @param player
  2090. * @param fileName
  2091. * @return String : message sent to client
  2092. */
  2093. public String showHtmlFile(L2PcInstance player, String fileName)
  2094. {
  2095. boolean questwindow = true;
  2096. if (fileName.endsWith(".html"))
  2097. {
  2098. questwindow = false;
  2099. }
  2100. int questId = getQuestIntId();
  2101. // Create handler to file linked to the quest
  2102. String content = getHtm(player.getHtmlPrefix(), fileName);
  2103. if (player.getTarget() != null)
  2104. {
  2105. content = content.replaceAll("%objectId%", String.valueOf(player.getTarget().getObjectId()));
  2106. }
  2107. // Send message to client if message not empty
  2108. if (content != null)
  2109. {
  2110. if (questwindow && (questId > 0) && (questId < 20000) && (questId != 999))
  2111. {
  2112. NpcQuestHtmlMessage npcReply = new NpcQuestHtmlMessage(5, questId);
  2113. npcReply.setHtml(content);
  2114. npcReply.replace("%playername%", player.getName());
  2115. player.sendPacket(npcReply);
  2116. }
  2117. else
  2118. {
  2119. NpcHtmlMessage npcReply = new NpcHtmlMessage(5);
  2120. npcReply.setHtml(content);
  2121. npcReply.replace("%playername%", player.getName());
  2122. player.sendPacket(npcReply);
  2123. }
  2124. player.sendPacket(ActionFailed.STATIC_PACKET);
  2125. }
  2126. return content;
  2127. }
  2128. /**
  2129. * Return HTML file contents
  2130. * @param prefix player's language prefix.
  2131. * @param fileName the html file to be get.
  2132. * @return
  2133. */
  2134. public String getHtm(String prefix, String fileName)
  2135. {
  2136. String content = HtmCache.getInstance().getHtm(prefix, "data/scripts/" + getDescr().toLowerCase() + "/" + getName() + "/" + fileName);
  2137. if (content == null)
  2138. {
  2139. content = HtmCache.getInstance().getHtm(prefix, "data/scripts/quests/Q" + getName() + "/" + fileName);
  2140. if (content == null)
  2141. {
  2142. content = HtmCache.getInstance().getHtmForce(prefix, "data/scripts/quests/" + getName() + "/" + fileName);
  2143. }
  2144. }
  2145. return content;
  2146. }
  2147. /**
  2148. * Add a temporary (quest) spawn
  2149. * @param npcId
  2150. * @param cha
  2151. * @return instance of newly spawned npc
  2152. */
  2153. public L2Npc addSpawn(int npcId, L2Character cha)
  2154. {
  2155. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), false, 0, false);
  2156. }
  2157. /**
  2158. * Add a temporary (quest) spawn
  2159. * @param npcId
  2160. * @param cha
  2161. * @param isSummonSpawn
  2162. * @return instance of newly spawned npc with summon animation
  2163. */
  2164. public L2Npc addSpawn(int npcId, L2Character cha, boolean isSummonSpawn)
  2165. {
  2166. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), false, 0, isSummonSpawn);
  2167. }
  2168. /**
  2169. * @param npcId
  2170. * @param x
  2171. * @param y
  2172. * @param z
  2173. * @param heading
  2174. * @param randomOffSet
  2175. * @param despawnDelay
  2176. * @return
  2177. */
  2178. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffSet, long despawnDelay)
  2179. {
  2180. return addSpawn(npcId, x, y, z, heading, randomOffSet, despawnDelay, false);
  2181. }
  2182. /**
  2183. * @param npcId
  2184. * @param loc
  2185. * @param randomOffSet
  2186. * @param despawnDelay
  2187. * @return
  2188. */
  2189. public L2Npc addSpawn(int npcId, Location loc, boolean randomOffSet, long despawnDelay)
  2190. {
  2191. return addSpawn(npcId, loc.getX(), loc.getY(), loc.getZ(), loc.getHeading(), randomOffSet, despawnDelay, false);
  2192. }
  2193. /**
  2194. * @param npcId
  2195. * @param x
  2196. * @param y
  2197. * @param z
  2198. * @param heading
  2199. * @param randomOffset
  2200. * @param despawnDelay
  2201. * @param isSummonSpawn
  2202. * @return
  2203. */
  2204. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, long despawnDelay, boolean isSummonSpawn)
  2205. {
  2206. return addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, isSummonSpawn, 0);
  2207. }
  2208. /**
  2209. * @param npcId
  2210. * @param loc
  2211. * @param randomOffset
  2212. * @param despawnDelay
  2213. * @param isSummonSpawn
  2214. * @return
  2215. */
  2216. public L2Npc addSpawn(int npcId, Location loc, boolean randomOffset, long despawnDelay, boolean isSummonSpawn)
  2217. {
  2218. return addSpawn(npcId, loc.getX(), loc.getY(), loc.getZ(), loc.getHeading(), randomOffset, despawnDelay, isSummonSpawn, 0);
  2219. }
  2220. /**
  2221. * @param npcId
  2222. * @param x
  2223. * @param y
  2224. * @param z
  2225. * @param heading
  2226. * @param randomOffset
  2227. * @param despawnDelay
  2228. * @param isSummonSpawn
  2229. * @param instanceId
  2230. * @return
  2231. */
  2232. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, long despawnDelay, boolean isSummonSpawn, int instanceId)
  2233. {
  2234. L2Npc result = null;
  2235. try
  2236. {
  2237. L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId);
  2238. if (template != null)
  2239. {
  2240. // Sometimes, even if the quest script specifies some xyz (for example npc.getX() etc) by the time the code
  2241. // reaches here, xyz have become 0! Also, a questdev might have purposely set xy to 0,0...however,
  2242. // the spawn code is coded such that if x=y=0, it looks into location for the spawn loc! This will NOT work
  2243. // with quest spawns! For both of the above cases, we need a fail-safe spawn. For this, we use the
  2244. // default spawn location, which is at the player's loc.
  2245. if ((x == 0) && (y == 0))
  2246. {
  2247. _log.log(Level.SEVERE, "Failed to adjust bad locks for quest spawn! Spawn aborted!");
  2248. return null;
  2249. }
  2250. if (randomOffset)
  2251. {
  2252. int offset;
  2253. offset = Rnd.get(2); // Get the direction of the offset
  2254. if (offset == 0)
  2255. {
  2256. // make offset negative
  2257. offset = -1;
  2258. }
  2259. offset *= Rnd.get(50, 100);
  2260. x += offset;
  2261. offset = Rnd.get(2); // Get the direction of the offset
  2262. if (offset == 0)
  2263. {
  2264. // make offset negative
  2265. offset = -1;
  2266. }
  2267. offset *= Rnd.get(50, 100);
  2268. y += offset;
  2269. }
  2270. L2Spawn spawn = new L2Spawn(template);
  2271. spawn.setInstanceId(instanceId);
  2272. spawn.setHeading(heading);
  2273. spawn.setLocx(x);
  2274. spawn.setLocy(y);
  2275. spawn.setLocz(z);
  2276. spawn.stopRespawn();
  2277. result = spawn.spawnOne(isSummonSpawn);
  2278. if (despawnDelay > 0)
  2279. {
  2280. result.scheduleDespawn(despawnDelay);
  2281. }
  2282. return result;
  2283. }
  2284. }
  2285. catch (Exception e1)
  2286. {
  2287. _log.warning("Could not spawn Npc " + npcId + " Error: " + e1.getMessage());
  2288. }
  2289. return null;
  2290. }
  2291. /**
  2292. * @param trapId
  2293. * @param x
  2294. * @param y
  2295. * @param z
  2296. * @param heading
  2297. * @param skill
  2298. * @param instanceId
  2299. * @return
  2300. */
  2301. public L2Trap addTrap(int trapId, int x, int y, int z, int heading, L2Skill skill, int instanceId)
  2302. {
  2303. L2NpcTemplate TrapTemplate = NpcTable.getInstance().getTemplate(trapId);
  2304. L2Trap trap = new L2TrapInstance(IdFactory.getInstance().getNextId(), TrapTemplate, instanceId, -1, skill);
  2305. trap.setCurrentHp(trap.getMaxHp());
  2306. trap.setCurrentMp(trap.getMaxMp());
  2307. trap.setIsInvul(true);
  2308. trap.setHeading(heading);
  2309. // L2World.getInstance().storeObject(trap);
  2310. trap.spawnMe(x, y, z);
  2311. return trap;
  2312. }
  2313. /**
  2314. * @param master
  2315. * @param minionId
  2316. * @return
  2317. */
  2318. public L2Npc addMinion(L2MonsterInstance master, int minionId)
  2319. {
  2320. return MinionList.spawnMinion(master, minionId);
  2321. }
  2322. /**
  2323. * @return the registered quest items Ids.
  2324. */
  2325. public int[] getRegisteredItemIds()
  2326. {
  2327. return questItemIds;
  2328. }
  2329. @Override
  2330. public String getScriptName()
  2331. {
  2332. return getName();
  2333. }
  2334. @Override
  2335. public void setActive(boolean status)
  2336. {
  2337. // TODO implement me
  2338. }
  2339. @Override
  2340. public boolean reload()
  2341. {
  2342. unload();
  2343. return super.reload();
  2344. }
  2345. @Override
  2346. public boolean unload()
  2347. {
  2348. return unload(true);
  2349. }
  2350. /**
  2351. * @param removeFromList
  2352. * @return
  2353. */
  2354. public boolean unload(boolean removeFromList)
  2355. {
  2356. saveGlobalData();
  2357. // cancel all pending timers before reloading.
  2358. // if timers ought to be restarted, the quest can take care of it
  2359. // with its code (example: save global data indicating what timer must
  2360. // be restarted).
  2361. for (List<QuestTimer> timers : _allEventTimers.values())
  2362. {
  2363. _readLock.lock();
  2364. try
  2365. {
  2366. for (QuestTimer timer : timers)
  2367. {
  2368. timer.cancel();
  2369. }
  2370. }
  2371. finally
  2372. {
  2373. _readLock.unlock();
  2374. }
  2375. timers.clear();
  2376. }
  2377. _allEventTimers.clear();
  2378. for (Integer npcId : _questInvolvedNpcs)
  2379. {
  2380. L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId.intValue());
  2381. if (template != null)
  2382. {
  2383. template.removeQuest(this);
  2384. }
  2385. }
  2386. _questInvolvedNpcs.clear();
  2387. if (removeFromList)
  2388. {
  2389. return QuestManager.getInstance().removeQuest(this);
  2390. }
  2391. return true;
  2392. }
  2393. @Override
  2394. public ScriptManager<?> getScriptManager()
  2395. {
  2396. return QuestManager.getInstance();
  2397. }
  2398. /**
  2399. * @param val
  2400. */
  2401. public void setOnEnterWorld(boolean val)
  2402. {
  2403. _onEnterWorld = val;
  2404. }
  2405. /**
  2406. * @return
  2407. */
  2408. public boolean getOnEnterWorld()
  2409. {
  2410. return _onEnterWorld;
  2411. }
  2412. /**
  2413. * If a quest is set as custom, it will display it's name in the NPC Quest List.<br>
  2414. * Retail quests are unhardcoded to display the name using a client string.
  2415. * @param val if {@code true} the quest script will be set as custom quest.
  2416. */
  2417. public void setIsCustom(boolean val)
  2418. {
  2419. _isCustom = val;
  2420. }
  2421. /**
  2422. * @return {@code true} if the quest script is a custom quest, {@code false} otherwise.
  2423. */
  2424. public boolean isCustomQuest()
  2425. {
  2426. return _isCustom;
  2427. }
  2428. /**
  2429. * @param val
  2430. */
  2431. public void setOlympiadUse(boolean val)
  2432. {
  2433. _isOlympiadUse = val;
  2434. }
  2435. /**
  2436. * @return {@code true} if the quest script is used for Olympiad quests, {@code false} otherwise.
  2437. */
  2438. public boolean isOlympiadUse()
  2439. {
  2440. return _isOlympiadUse;
  2441. }
  2442. /**
  2443. * @param player this parameter contains a reference to the player to check.
  2444. * @param itemId the item wanted to be count.
  2445. * @return the quantity of one sort of item hold by the player.
  2446. */
  2447. public long getQuestItemsCount(L2PcInstance player, int itemId)
  2448. {
  2449. long count = 0;
  2450. for (L2ItemInstance item : player.getInventory().getItems())
  2451. {
  2452. if ((item != null) && (item.getItemId() == itemId))
  2453. {
  2454. count += item.getCount();
  2455. }
  2456. }
  2457. return count;
  2458. }
  2459. /**
  2460. * @param player the player whose inventory to check for quest items
  2461. * @param itemId the ID of the item to check for
  2462. * @return {@code true} if the item exists in player's inventory, {@code false} otherwise
  2463. */
  2464. public boolean hasQuestItems(L2PcInstance player, int itemId)
  2465. {
  2466. return player.getInventory().getItemByItemId(itemId) != null;
  2467. }
  2468. /**
  2469. * @param player the player whose inventory to check for quest items
  2470. * @param itemIds a list or array of item IDs to check for
  2471. * @return {@code true} if all items exist in player's inventory, {@code false} otherwise
  2472. */
  2473. public boolean hasQuestItems(L2PcInstance player, int... itemIds)
  2474. {
  2475. final PcInventory inv = player.getInventory();
  2476. for (int itemId : itemIds)
  2477. {
  2478. if (inv.getItemByItemId(itemId) == null)
  2479. {
  2480. return false;
  2481. }
  2482. }
  2483. return true;
  2484. }
  2485. /**
  2486. * @param player the player whose item to check
  2487. * @param itemId the ID of the item whose enchantment to check
  2488. * @return the enchantment level of the item or 0 if no item was found
  2489. */
  2490. public int getEnchantLevel(L2PcInstance player, int itemId)
  2491. {
  2492. L2ItemInstance enchanteditem = player.getInventory().getItemByItemId(itemId);
  2493. if (enchanteditem == null)
  2494. {
  2495. return 0;
  2496. }
  2497. return enchanteditem.getEnchantLevel();
  2498. }
  2499. /**
  2500. * Give Adena to the player.
  2501. * @param player the player to whom to give the Adena
  2502. * @param count the amount of Adena to give
  2503. * @param applyRates if {@code true}, quest rates will be applied
  2504. */
  2505. public void giveAdena(L2PcInstance player, long count, boolean applyRates)
  2506. {
  2507. giveItems(player, PcInventory.ADENA_ID, count, applyRates ? 0 : 1);
  2508. }
  2509. /**
  2510. * Give reward to player using multipliers.
  2511. * @param player the player to whom to give the item
  2512. * @param itemId the ID of the item to give
  2513. * @param count the amount of items to give
  2514. */
  2515. public void rewardItems(L2PcInstance player, int itemId, long count)
  2516. {
  2517. if (count <= 0)
  2518. {
  2519. return;
  2520. }
  2521. L2ItemInstance _tmpItem = ItemTable.getInstance().createDummyItem(itemId);
  2522. if (_tmpItem == null)
  2523. {
  2524. return;
  2525. }
  2526. if (itemId == PcInventory.ADENA_ID)
  2527. {
  2528. count = (long) (count * Config.RATE_QUEST_REWARD_ADENA);
  2529. }
  2530. else if (Config.RATE_QUEST_REWARD_USE_MULTIPLIERS)
  2531. {
  2532. if (_tmpItem.isEtcItem())
  2533. {
  2534. switch (_tmpItem.getEtcItem().getItemType())
  2535. {
  2536. case POTION:
  2537. count = (long) (count * Config.RATE_QUEST_REWARD_POTION);
  2538. break;
  2539. case SCRL_ENCHANT_WP:
  2540. case SCRL_ENCHANT_AM:
  2541. case SCROLL:
  2542. count = (long) (count * Config.RATE_QUEST_REWARD_SCROLL);
  2543. break;
  2544. case RECIPE:
  2545. count = (long) (count * Config.RATE_QUEST_REWARD_RECIPE);
  2546. break;
  2547. case MATERIAL:
  2548. count = (long) (count * Config.RATE_QUEST_REWARD_MATERIAL);
  2549. break;
  2550. default:
  2551. count = (long) (count * Config.RATE_QUEST_REWARD);
  2552. }
  2553. }
  2554. }
  2555. else
  2556. {
  2557. count = (long) (count * Config.RATE_QUEST_REWARD);
  2558. }
  2559. // Add items to player's inventory
  2560. L2ItemInstance item = player.getInventory().addItem("Quest", itemId, count, player, player.getTarget());
  2561. if (item == null)
  2562. {
  2563. return;
  2564. }
  2565. sendItemGetMessage(player, item, count);
  2566. }
  2567. /**
  2568. * Send the system message and the status update packets to the player.
  2569. * @param player the player that has got the item
  2570. * @param item the item obtain by the player
  2571. * @param count the item count
  2572. */
  2573. private void sendItemGetMessage(L2PcInstance player, L2ItemInstance item, long count)
  2574. {
  2575. // If item for reward is gold, send message of gold reward to client
  2576. if (item.getItemId() == PcInventory.ADENA_ID)
  2577. {
  2578. SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S1_ADENA);
  2579. smsg.addItemNumber(count);
  2580. player.sendPacket(smsg);
  2581. }
  2582. // Otherwise, send message of object reward to client
  2583. else
  2584. {
  2585. if (count > 1)
  2586. {
  2587. SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S2_S1_S);
  2588. smsg.addItemName(item);
  2589. smsg.addItemNumber(count);
  2590. player.sendPacket(smsg);
  2591. }
  2592. else
  2593. {
  2594. SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
  2595. smsg.addItemName(item);
  2596. player.sendPacket(smsg);
  2597. }
  2598. }
  2599. // send packets
  2600. StatusUpdate su = new StatusUpdate(player);
  2601. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  2602. player.sendPacket(su);
  2603. }
  2604. /**
  2605. * Give item/reward to the player
  2606. * @param player
  2607. * @param itemId
  2608. * @param count
  2609. */
  2610. public void giveItems(L2PcInstance player, int itemId, long count)
  2611. {
  2612. giveItems(player, itemId, count, 0);
  2613. }
  2614. /**
  2615. * @param player
  2616. * @param itemId
  2617. * @param count
  2618. * @param enchantlevel
  2619. */
  2620. public void giveItems(L2PcInstance player, int itemId, long count, int enchantlevel)
  2621. {
  2622. if (count <= 0)
  2623. {
  2624. return;
  2625. }
  2626. // If item for reward is adena (ID=57), modify count with rate for quest reward if rates available
  2627. if ((itemId == PcInventory.ADENA_ID) && !(enchantlevel > 0))
  2628. {
  2629. count = (long) (count * Config.RATE_QUEST_REWARD_ADENA);
  2630. }
  2631. // Add items to player's inventory
  2632. L2ItemInstance item = player.getInventory().addItem("Quest", itemId, count, player, player.getTarget());
  2633. if (item == null)
  2634. {
  2635. return;
  2636. }
  2637. // set enchant level for item if that item is not adena
  2638. if ((enchantlevel > 0) && (itemId != PcInventory.ADENA_ID))
  2639. {
  2640. item.setEnchantLevel(enchantlevel);
  2641. }
  2642. sendItemGetMessage(player, item, count);
  2643. }
  2644. /**
  2645. * @param player
  2646. * @param itemId
  2647. * @param count
  2648. * @param attributeId
  2649. * @param attributeLevel
  2650. */
  2651. public void giveItems(L2PcInstance player, int itemId, long count, byte attributeId, int attributeLevel)
  2652. {
  2653. if (count <= 0)
  2654. {
  2655. return;
  2656. }
  2657. // Add items to player's inventory
  2658. L2ItemInstance item = player.getInventory().addItem("Quest", itemId, count, player, player.getTarget());
  2659. if (item == null)
  2660. {
  2661. return;
  2662. }
  2663. // set enchant level for item if that item is not adena
  2664. if ((attributeId >= 0) && (attributeLevel > 0))
  2665. {
  2666. item.setElementAttr(attributeId, attributeLevel);
  2667. if (item.isEquipped())
  2668. {
  2669. item.updateElementAttrBonus(player);
  2670. }
  2671. InventoryUpdate iu = new InventoryUpdate();
  2672. iu.addModifiedItem(item);
  2673. player.sendPacket(iu);
  2674. }
  2675. sendItemGetMessage(player, item, count);
  2676. }
  2677. /**
  2678. * Drop Quest item using Config.RATE_QUEST_DROP
  2679. * @param player
  2680. * @param itemId int Item Identifier of the item to be dropped
  2681. * @param count (minCount, maxCount) long Quantity of items to be dropped
  2682. * @param neededCount Quantity of items needed for quest
  2683. * @param dropChance int Base chance of drop, same as in droplist
  2684. * @param sound boolean indicating whether to play sound
  2685. * @return boolean indicating whether player has requested number of items
  2686. */
  2687. public boolean dropQuestItems(L2PcInstance player, int itemId, int count, long neededCount, int dropChance, boolean sound)
  2688. {
  2689. return dropQuestItems(player, itemId, count, count, neededCount, dropChance, sound);
  2690. }
  2691. /**
  2692. * @param player
  2693. * @param itemId
  2694. * @param minCount
  2695. * @param maxCount
  2696. * @param neededCount
  2697. * @param dropChance
  2698. * @param sound
  2699. * @return
  2700. */
  2701. public boolean dropQuestItems(L2PcInstance player, int itemId, int minCount, int maxCount, long neededCount, int dropChance, boolean sound)
  2702. {
  2703. dropChance *= Config.RATE_QUEST_DROP / ((player.getParty() != null) ? player.getParty().getMemberCount() : 1);
  2704. long currentCount = getQuestItemsCount(player, itemId);
  2705. if ((neededCount > 0) && (currentCount >= neededCount))
  2706. {
  2707. return true;
  2708. }
  2709. if (currentCount >= neededCount)
  2710. {
  2711. return true;
  2712. }
  2713. long itemCount = 0;
  2714. int random = Rnd.get(L2DropData.MAX_CHANCE);
  2715. while (random < dropChance)
  2716. {
  2717. // Get the item quantity dropped
  2718. if (minCount < maxCount)
  2719. {
  2720. itemCount += Rnd.get(minCount, maxCount);
  2721. }
  2722. else if (minCount == maxCount)
  2723. {
  2724. itemCount += minCount;
  2725. }
  2726. else
  2727. {
  2728. itemCount++;
  2729. }
  2730. // Prepare for next iteration if dropChance > L2DropData.MAX_CHANCE
  2731. dropChance -= L2DropData.MAX_CHANCE;
  2732. }
  2733. if (itemCount > 0)
  2734. {
  2735. // if over neededCount, just fill the gap
  2736. if ((neededCount > 0) && ((currentCount + itemCount) > neededCount))
  2737. {
  2738. itemCount = neededCount - currentCount;
  2739. }
  2740. // Inventory slot check
  2741. if (!player.getInventory().validateCapacityByItemId(itemId))
  2742. {
  2743. return false;
  2744. }
  2745. // Give the item to Player
  2746. player.addItem("Quest", itemId, itemCount, player.getTarget(), true);
  2747. if (sound)
  2748. {
  2749. playSound(player, ((currentCount + itemCount) < neededCount) ? "Itemsound.quest_itemget" : "Itemsound.quest_middle");
  2750. }
  2751. }
  2752. return ((neededCount > 0) && ((currentCount + itemCount) >= neededCount));
  2753. }
  2754. /**
  2755. * Remove items from player's inventory when talking to NPC in order to have rewards.<br>
  2756. * <u><i>Actions :</i></u>
  2757. * <ul>
  2758. * <li>Destroy quantity of items wanted</li>
  2759. * <li>Send new inventory list to player</li>
  2760. * </ul>
  2761. * @param player
  2762. * @param itemId : Identifier of the item
  2763. * @param count : Quantity of items to destroy
  2764. */
  2765. public void takeItems(L2PcInstance player, int itemId, long count)
  2766. {
  2767. // Get object item from player's inventory list
  2768. L2ItemInstance item = player.getInventory().getItemByItemId(itemId);
  2769. if (item == null)
  2770. {
  2771. return;
  2772. }
  2773. // Tests on count value in order not to have negative value
  2774. if ((count < 0) || (count > item.getCount()))
  2775. {
  2776. count = item.getCount();
  2777. }
  2778. // Destroy the quantity of items wanted
  2779. if (item.isEquipped())
  2780. {
  2781. L2ItemInstance[] unequiped = player.getInventory().unEquipItemInBodySlotAndRecord(item.getItem().getBodyPart());
  2782. InventoryUpdate iu = new InventoryUpdate();
  2783. for (L2ItemInstance itm : unequiped)
  2784. {
  2785. iu.addModifiedItem(itm);
  2786. }
  2787. player.sendPacket(iu);
  2788. player.broadcastUserInfo();
  2789. }
  2790. player.destroyItemByItemId("Quest", itemId, count, player, true);
  2791. }
  2792. /**
  2793. * Send a packet in order to play sound at client terminal
  2794. * @param player
  2795. * @param sound
  2796. */
  2797. public void playSound(L2PcInstance player, String sound)
  2798. {
  2799. player.sendPacket(new PlaySound(sound));
  2800. }
  2801. /**
  2802. * Add XP and SP as quest reward
  2803. * @param player
  2804. * @param exp
  2805. * @param sp
  2806. */
  2807. public void addExpAndSp(L2PcInstance player, int exp, int sp)
  2808. {
  2809. 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));
  2810. }
  2811. /**
  2812. * Gets a random integer number from 0 (inclusive) to {@code max} (exclusive).<br>
  2813. * Use this method instead importing {@link com.l2jserver.util.Rnd} utility.
  2814. * @param max this parameter represents the maximum value for randomization.
  2815. * @return a random integer number from 0 to {@code max} - 1.
  2816. */
  2817. public static int getRandom(int max)
  2818. {
  2819. return Rnd.get(max);
  2820. }
  2821. /**
  2822. * Gets a random integer number from {@code min} (inclusive) to {@code max} (inclusive).<br>
  2823. * Use this method instead importing {@link com.l2jserver.util.Rnd} utility.
  2824. * @param min this parameter represents the minimum value for randomization.
  2825. * @param max this parameter represents the maximum value for randomization.
  2826. * @return a random integer number from {@code min} to {@code max} .
  2827. */
  2828. public static int getRandom(int min, int max)
  2829. {
  2830. return Rnd.get(min, max);
  2831. }
  2832. /**
  2833. * @param player this parameter is a reference to the player.
  2834. * @param slot this parameter represents the location in the player's inventory.
  2835. * @return the item Id of the item present in the inventory slot {@code slot} if it's not null, 0 otherwise.
  2836. */
  2837. public int getItemEquipped(L2PcInstance player, int slot)
  2838. {
  2839. return player.getInventory().getPaperdollItemId(slot);
  2840. }
  2841. /**
  2842. * @return the number of ticks from the {@link com.l2jserver.gameserver.GameTimeController}.
  2843. */
  2844. public int getGameTicks()
  2845. {
  2846. return GameTimeController.getGameTicks();
  2847. }
  2848. }