Quest.java 95 KB

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