Quest.java 95 KB

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