AbstractScript.java 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.events;
  20. import java.lang.annotation.Annotation;
  21. import java.lang.reflect.Method;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.concurrent.ConcurrentHashMap;
  29. import java.util.function.Consumer;
  30. import java.util.function.Function;
  31. import java.util.logging.Level;
  32. import java.util.logging.Logger;
  33. import javolution.util.FastList;
  34. import com.l2jserver.Config;
  35. import com.l2jserver.gameserver.GameTimeController;
  36. import com.l2jserver.gameserver.datatables.DoorTable;
  37. import com.l2jserver.gameserver.datatables.ItemTable;
  38. import com.l2jserver.gameserver.datatables.NpcData;
  39. import com.l2jserver.gameserver.enums.QuestSound;
  40. import com.l2jserver.gameserver.idfactory.IdFactory;
  41. import com.l2jserver.gameserver.instancemanager.CastleManager;
  42. import com.l2jserver.gameserver.instancemanager.FortManager;
  43. import com.l2jserver.gameserver.instancemanager.InstanceManager;
  44. import com.l2jserver.gameserver.instancemanager.ZoneManager;
  45. import com.l2jserver.gameserver.model.L2Spawn;
  46. import com.l2jserver.gameserver.model.Location;
  47. import com.l2jserver.gameserver.model.actor.L2Attackable;
  48. import com.l2jserver.gameserver.model.actor.L2Character;
  49. import com.l2jserver.gameserver.model.actor.L2Npc;
  50. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  51. import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
  52. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  53. import com.l2jserver.gameserver.model.actor.instance.L2TrapInstance;
  54. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  55. import com.l2jserver.gameserver.model.entity.Castle;
  56. import com.l2jserver.gameserver.model.entity.Fort;
  57. import com.l2jserver.gameserver.model.entity.Instance;
  58. import com.l2jserver.gameserver.model.events.annotations.Npc;
  59. import com.l2jserver.gameserver.model.events.annotations.Npcs;
  60. import com.l2jserver.gameserver.model.events.annotations.RegisterEvent;
  61. import com.l2jserver.gameserver.model.events.impl.IBaseEvent;
  62. import com.l2jserver.gameserver.model.events.impl.character.OnCreatureKill;
  63. import com.l2jserver.gameserver.model.events.impl.character.OnCreatureZoneEnter;
  64. import com.l2jserver.gameserver.model.events.impl.character.OnCreatureZoneExit;
  65. import com.l2jserver.gameserver.model.events.impl.character.npc.OnNpcCanBeSeen;
  66. import com.l2jserver.gameserver.model.events.impl.character.npc.OnNpcCreatureSee;
  67. import com.l2jserver.gameserver.model.events.impl.character.npc.OnNpcEventReceived;
  68. import com.l2jserver.gameserver.model.events.impl.character.npc.OnNpcFirstTalk;
  69. import com.l2jserver.gameserver.model.events.impl.character.npc.OnNpcMoveFinished;
  70. import com.l2jserver.gameserver.model.events.impl.character.npc.OnNpcMoveNodeArrived;
  71. import com.l2jserver.gameserver.model.events.impl.character.npc.OnNpcMoveRouteFinished;
  72. import com.l2jserver.gameserver.model.events.impl.character.npc.OnNpcSkillFinished;
  73. import com.l2jserver.gameserver.model.events.impl.character.npc.OnNpcSkillSee;
  74. import com.l2jserver.gameserver.model.events.impl.character.npc.OnNpcSpawn;
  75. import com.l2jserver.gameserver.model.events.impl.character.npc.attackable.OnAttackableAggroRangeEnter;
  76. import com.l2jserver.gameserver.model.events.impl.character.npc.attackable.OnAttackableAttack;
  77. import com.l2jserver.gameserver.model.events.impl.character.npc.attackable.OnAttackableFactionCall;
  78. import com.l2jserver.gameserver.model.events.impl.character.npc.attackable.OnAttackableHate;
  79. import com.l2jserver.gameserver.model.events.impl.character.npc.attackable.OnAttackableKill;
  80. import com.l2jserver.gameserver.model.events.impl.character.player.OnPlayerLogin;
  81. import com.l2jserver.gameserver.model.events.impl.character.player.OnPlayerLogout;
  82. import com.l2jserver.gameserver.model.events.impl.character.player.OnPlayerProfessionChange;
  83. import com.l2jserver.gameserver.model.events.impl.character.player.OnPlayerSkillLearn;
  84. import com.l2jserver.gameserver.model.events.impl.character.player.OnPlayerSummonSpawn;
  85. import com.l2jserver.gameserver.model.events.impl.character.trap.OnTrapAction;
  86. import com.l2jserver.gameserver.model.events.impl.item.OnItemBypassEvent;
  87. import com.l2jserver.gameserver.model.events.impl.item.OnItemTalk;
  88. import com.l2jserver.gameserver.model.events.impl.olympiad.OnOlympiadMatchResult;
  89. import com.l2jserver.gameserver.model.events.impl.sieges.castle.OnCastleSiegeFinish;
  90. import com.l2jserver.gameserver.model.events.impl.sieges.castle.OnCastleSiegeOwnerChange;
  91. import com.l2jserver.gameserver.model.events.impl.sieges.castle.OnCastleSiegeStart;
  92. import com.l2jserver.gameserver.model.events.listeners.AbstractEventListener;
  93. import com.l2jserver.gameserver.model.events.listeners.AnnotationEventListener;
  94. import com.l2jserver.gameserver.model.events.listeners.ConsumerEventListener;
  95. import com.l2jserver.gameserver.model.events.listeners.DummyEventListener;
  96. import com.l2jserver.gameserver.model.events.listeners.FunctionEventListener;
  97. import com.l2jserver.gameserver.model.events.listeners.RunnableEventListener;
  98. import com.l2jserver.gameserver.model.events.returns.AbstractEventReturn;
  99. import com.l2jserver.gameserver.model.events.returns.TerminateReturn;
  100. import com.l2jserver.gameserver.model.holders.ItemHolder;
  101. import com.l2jserver.gameserver.model.interfaces.IPositionable;
  102. import com.l2jserver.gameserver.model.itemcontainer.Inventory;
  103. import com.l2jserver.gameserver.model.itemcontainer.PcInventory;
  104. import com.l2jserver.gameserver.model.items.L2Item;
  105. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  106. import com.l2jserver.gameserver.model.olympiad.Olympiad;
  107. import com.l2jserver.gameserver.model.skills.Skill;
  108. import com.l2jserver.gameserver.model.stats.Stats;
  109. import com.l2jserver.gameserver.model.zone.L2ZoneType;
  110. import com.l2jserver.gameserver.network.NpcStringId;
  111. import com.l2jserver.gameserver.network.SystemMessageId;
  112. import com.l2jserver.gameserver.network.serverpackets.ExShowScreenMessage;
  113. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  114. import com.l2jserver.gameserver.network.serverpackets.SpecialCamera;
  115. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  116. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  117. import com.l2jserver.gameserver.scripting.ManagedScript;
  118. import com.l2jserver.gameserver.util.MinionList;
  119. import com.l2jserver.util.Rnd;
  120. /**
  121. * @author UnAfraid
  122. */
  123. public abstract class AbstractScript extends ManagedScript
  124. {
  125. protected static final Logger _log = Logger.getLogger(AbstractScript.class.getName());
  126. private final Map<ListenerRegisterType, List<Integer>> _registeredIds = new ConcurrentHashMap<>();
  127. private final List<AbstractEventListener> _listeners = new FastList<AbstractEventListener>().shared();
  128. public AbstractScript()
  129. {
  130. initializeAnnotationListeners();
  131. }
  132. private void initializeAnnotationListeners()
  133. {
  134. final List<Integer> npcIds = new ArrayList<>();
  135. for (Method method : getClass().getMethods())
  136. {
  137. if (method.isAnnotationPresent(RegisterEvent.class))
  138. {
  139. final RegisterEvent listener = method.getAnnotation(RegisterEvent.class);
  140. final EventType eventType = listener.value();
  141. if (method.getParameterCount() != 1)
  142. {
  143. _log.log(Level.WARNING, getClass().getSimpleName() + ": Non properly defined annotation listener on method: " + method.getName() + " expected parameter count is 1 but found: " + method.getParameterCount());
  144. continue;
  145. }
  146. else if (!eventType.isEventClass(method.getParameterTypes()[0]))
  147. {
  148. _log.log(Level.WARNING, getClass().getSimpleName() + ": Non properly defined annotation listener on method: " + method.getName() + " expected parameter to be type of: " + eventType.getEventClass().getSimpleName() + " but found: " + method.getParameterTypes()[0].getSimpleName());
  149. continue;
  150. }
  151. else if (!eventType.isReturnClass(method.getReturnType()))
  152. {
  153. _log.log(Level.WARNING, getClass().getSimpleName() + ": Non properly defined annotation listener on method: " + method.getName() + " expected return type to be one of: " + Arrays.toString(eventType.getReturnClasses()) + " but found: " + method.getReturnType().getSimpleName());
  154. continue;
  155. }
  156. // Clear the list
  157. npcIds.clear();
  158. // Scan for possible Npc ID filters
  159. for (Annotation annotation : method.getAnnotations())
  160. {
  161. if (annotation instanceof Npc)
  162. {
  163. final Npc npc = (Npc) annotation;
  164. for (int id : npc.value())
  165. {
  166. npcIds.add(id);
  167. }
  168. }
  169. else if (annotation instanceof Npcs)
  170. {
  171. final Npcs npcs = (Npcs) annotation;
  172. for (Npc npc : npcs.value())
  173. {
  174. for (int id : npc.value())
  175. {
  176. npcIds.add(id);
  177. }
  178. }
  179. }
  180. }
  181. if (!npcIds.isEmpty())
  182. {
  183. if (!_registeredIds.containsKey(ListenerRegisterType.NPC))
  184. {
  185. _registeredIds.put(ListenerRegisterType.NPC, new FastList<Integer>().shared());
  186. }
  187. _registeredIds.get(ListenerRegisterType.NPC).addAll(npcIds);
  188. }
  189. registerAnnotation(method, eventType, ListenerRegisterType.NPC, npcIds);
  190. }
  191. }
  192. }
  193. /**
  194. * Unloads all listeners registered by this class.
  195. */
  196. @Override
  197. public boolean unload()
  198. {
  199. _listeners.forEach(AbstractEventListener::unregisterMe);
  200. _listeners.clear();
  201. return true;
  202. }
  203. // ---------------------------------------------------------------------------------------------------------------------------
  204. /**
  205. * Provides delayed (Depending on {@link com.l2jserver.gameserver.model.actor.L2Attackable#getOnKillDelay()}) callback operation when L2Attackable dies from a player.
  206. * @param callback
  207. * @param npcIds
  208. * @return
  209. */
  210. protected final List<AbstractEventListener> setAttackableKillId(Consumer<OnAttackableKill> callback, int... npcIds)
  211. {
  212. return registerConsumer(callback, EventType.ON_ATTACKABLE_KILL, ListenerRegisterType.NPC, npcIds);
  213. }
  214. /**
  215. * Provides delayed (Depending on {@link com.l2jserver.gameserver.model.actor.L2Attackable#getOnKillDelay()}) callback operation when L2Attackable dies from a player.
  216. * @param callback
  217. * @param npcIds
  218. * @return
  219. */
  220. protected final List<AbstractEventListener> setAttackableKillId(Consumer<OnAttackableKill> callback, Collection<Integer> npcIds)
  221. {
  222. return registerConsumer(callback, EventType.ON_ATTACKABLE_KILL, ListenerRegisterType.NPC, npcIds);
  223. }
  224. // ---------------------------------------------------------------------------------------------------------------------------
  225. /**
  226. * Provides instant callback operation when L2Attackable dies from a player with return type.
  227. * @param callback
  228. * @param npcIds
  229. * @return
  230. */
  231. protected final List<AbstractEventListener> addCreatureKillId(Function<OnCreatureKill, ? extends AbstractEventReturn> callback, int... npcIds)
  232. {
  233. return registerFunction(callback, EventType.ON_CREATURE_KILL, ListenerRegisterType.NPC, npcIds);
  234. }
  235. /**
  236. * Provides instant callback operation when L2Attackable dies from a player.
  237. * @param callback
  238. * @param npcIds
  239. * @return
  240. */
  241. protected final List<AbstractEventListener> setCreatureKillId(Consumer<OnCreatureKill> callback, int... npcIds)
  242. {
  243. return registerConsumer(callback, EventType.ON_CREATURE_KILL, ListenerRegisterType.NPC, npcIds);
  244. }
  245. /**
  246. * Provides instant callback operation when {@link L2Attackable} dies from a {@link L2PcInstance}.
  247. * @param callback
  248. * @param npcIds
  249. * @return
  250. */
  251. protected final List<AbstractEventListener> setCreatureKillId(Consumer<OnCreatureKill> callback, Collection<Integer> npcIds)
  252. {
  253. return registerConsumer(callback, EventType.ON_CREATURE_KILL, ListenerRegisterType.NPC, npcIds);
  254. }
  255. // ---------------------------------------------------------------------------------------------------------------------------
  256. /**
  257. * Provides instant callback operation when {@link L2PcInstance} talk to {@link L2Npc} for first time.
  258. * @param callback
  259. * @param npcIds
  260. * @return
  261. */
  262. protected final List<AbstractEventListener> setNpcFirstTalkId(Consumer<OnNpcFirstTalk> callback, int... npcIds)
  263. {
  264. return registerConsumer(callback, EventType.ON_NPC_FIRST_TALK, ListenerRegisterType.NPC, npcIds);
  265. }
  266. /**
  267. * Provides instant callback operation when {@link L2PcInstance} talk to {@link L2Npc} for first time.
  268. * @param callback
  269. * @param npcIds
  270. * @return
  271. */
  272. protected final List<AbstractEventListener> setNpcFirstTalkId(Consumer<OnNpcFirstTalk> callback, Collection<Integer> npcIds)
  273. {
  274. return registerConsumer(callback, EventType.ON_NPC_FIRST_TALK, ListenerRegisterType.NPC, npcIds);
  275. }
  276. // ---------------------------------------------------------------------------------------------------------------------------
  277. /**
  278. * Provides instant callback operation when {@link L2PcInstance} talk to {@link L2Npc}.
  279. * @param npcIds
  280. * @return
  281. */
  282. protected final List<AbstractEventListener> setNpcTalkId(Collection<Integer> npcIds)
  283. {
  284. return registerDummy(EventType.ON_NPC_TALK, ListenerRegisterType.NPC, npcIds);
  285. }
  286. /**
  287. * Provides instant callback operation when {@link L2PcInstance} talk to {@link L2Npc}.
  288. * @param npcIds
  289. * @return
  290. */
  291. protected final List<AbstractEventListener> setNpcTalkId(int... npcIds)
  292. {
  293. return registerDummy(EventType.ON_NPC_TALK, ListenerRegisterType.NPC, npcIds);
  294. }
  295. // ---------------------------------------------------------------------------------------------------------------------------
  296. /**
  297. * Provides instant callback operation when {@link L2PcInstance} talk to {@link L2Npc} and must receive quest state.
  298. * @param npcIds
  299. * @return
  300. */
  301. protected final List<AbstractEventListener> setNpcQuestStartId(int... npcIds)
  302. {
  303. return registerDummy(EventType.ON_NPC_QUEST_START, ListenerRegisterType.NPC, npcIds);
  304. }
  305. /**
  306. * Provides instant callback operation when {@link L2PcInstance} talk to {@link L2Npc} and must receive quest state.
  307. * @param npcIds
  308. * @return
  309. */
  310. protected final List<AbstractEventListener> setNpcQuestStartId(Collection<Integer> npcIds)
  311. {
  312. return registerDummy(EventType.ON_NPC_QUEST_START, ListenerRegisterType.NPC, npcIds);
  313. }
  314. // ---------------------------------------------------------------------------------------------------------------------------
  315. /**
  316. * Provides instant callback operation when L2Npc sees skill from a player.
  317. * @param callback
  318. * @param npcIds
  319. * @return
  320. */
  321. protected final List<AbstractEventListener> setNpcSkillSeeId(Consumer<OnNpcSkillSee> callback, int... npcIds)
  322. {
  323. return registerConsumer(callback, EventType.ON_NPC_SKILL_SEE, ListenerRegisterType.NPC, npcIds);
  324. }
  325. /**
  326. * Provides instant callback operation when L2Npc sees skill from a player.
  327. * @param callback
  328. * @param npcIds
  329. * @return
  330. */
  331. protected final List<AbstractEventListener> setNpcSkillSeeId(Consumer<OnNpcSkillSee> callback, Collection<Integer> npcIds)
  332. {
  333. return registerConsumer(callback, EventType.ON_NPC_SKILL_SEE, ListenerRegisterType.NPC, npcIds);
  334. }
  335. // ---------------------------------------------------------------------------------------------------------------------------
  336. /**
  337. * Provides instant callback operation when L2Npc casts skill on a player.
  338. * @param callback
  339. * @param npcIds
  340. * @return
  341. */
  342. protected final List<AbstractEventListener> setNpcSkillFinishedId(Consumer<OnNpcSkillFinished> callback, int... npcIds)
  343. {
  344. return registerConsumer(callback, EventType.ON_NPC_SKILL_FINISHED, ListenerRegisterType.NPC, npcIds);
  345. }
  346. /**
  347. * Provides instant callback operation when L2Npc casts skill on a player.
  348. * @param callback
  349. * @param npcIds
  350. * @return
  351. */
  352. protected final List<AbstractEventListener> setNpcSkillFinishedId(Consumer<OnNpcSkillFinished> callback, Collection<Integer> npcIds)
  353. {
  354. return registerConsumer(callback, EventType.ON_NPC_SKILL_FINISHED, ListenerRegisterType.NPC, npcIds);
  355. }
  356. // ---------------------------------------------------------------------------------------------------------------------------
  357. /**
  358. * Provides instant callback operation when L2Npc is spawned.
  359. * @param callback
  360. * @param npcIds
  361. * @return
  362. */
  363. protected final List<AbstractEventListener> setNpcSpawnId(Consumer<OnNpcSpawn> callback, int... npcIds)
  364. {
  365. return registerConsumer(callback, EventType.ON_NPC_SPAWN, ListenerRegisterType.NPC, npcIds);
  366. }
  367. /**
  368. * Provides instant callback operation when L2Npc is spawned.
  369. * @param callback
  370. * @param npcIds
  371. * @return
  372. */
  373. protected final List<AbstractEventListener> setNpcSpawnId(Consumer<OnNpcSpawn> callback, Collection<Integer> npcIds)
  374. {
  375. return registerConsumer(callback, EventType.ON_NPC_SPAWN, ListenerRegisterType.NPC, npcIds);
  376. }
  377. // ---------------------------------------------------------------------------------------------------------------------------
  378. /**
  379. * Provides instant callback operation when {@link L2Npc} receives event from another {@link L2Npc}
  380. * @param callback
  381. * @param npcIds
  382. * @return
  383. */
  384. protected final List<AbstractEventListener> setNpcEventReceivedId(Consumer<OnNpcEventReceived> callback, int... npcIds)
  385. {
  386. return registerConsumer(callback, EventType.ON_NPC_EVENT_RECEIVED, ListenerRegisterType.NPC, npcIds);
  387. }
  388. /**
  389. * Provides instant callback operation when {@link L2Npc} receives event from another {@link L2Npc}
  390. * @param callback
  391. * @param npcIds
  392. * @return
  393. */
  394. protected final List<AbstractEventListener> setNpcEventReceivedId(Consumer<OnNpcEventReceived> callback, Collection<Integer> npcIds)
  395. {
  396. return registerConsumer(callback, EventType.ON_NPC_EVENT_RECEIVED, ListenerRegisterType.NPC, npcIds);
  397. }
  398. // ---------------------------------------------------------------------------------------------------------------------------
  399. /**
  400. * Provides instant callback operation when {@link L2Npc} finishes to move.
  401. * @param callback
  402. * @param npcIds
  403. * @return
  404. */
  405. protected final List<AbstractEventListener> setNpcMoveFinishedId(Consumer<OnNpcMoveFinished> callback, int... npcIds)
  406. {
  407. return registerConsumer(callback, EventType.ON_NPC_MOVE_FINISHED, ListenerRegisterType.NPC, npcIds);
  408. }
  409. /**
  410. * Provides instant callback operation when {@link L2Npc} finishes to move.
  411. * @param callback
  412. * @param npcIds
  413. * @return
  414. */
  415. protected final List<AbstractEventListener> setNpcMoveFinishedId(Consumer<OnNpcMoveFinished> callback, Collection<Integer> npcIds)
  416. {
  417. return registerConsumer(callback, EventType.ON_NPC_MOVE_FINISHED, ListenerRegisterType.NPC, npcIds);
  418. }
  419. // ---------------------------------------------------------------------------------------------------------------------------
  420. /**
  421. * Provides instant callback operation when {@link L2Npc} arrive to node of its route
  422. * @param callback
  423. * @param npcIds
  424. * @return
  425. */
  426. protected final List<AbstractEventListener> setNpcMoveNodeArrivedId(Consumer<OnNpcMoveNodeArrived> callback, int... npcIds)
  427. {
  428. return registerConsumer(callback, EventType.ON_NPC_MOVE_NODE_ARRIVED, ListenerRegisterType.NPC, npcIds);
  429. }
  430. /**
  431. * Provides instant callback operation when {@link L2Npc} arrive to node of its route
  432. * @param callback
  433. * @param npcIds
  434. * @return
  435. */
  436. protected final List<AbstractEventListener> setNpcMoveNodeArrivedId(Consumer<OnNpcMoveNodeArrived> callback, Collection<Integer> npcIds)
  437. {
  438. return registerConsumer(callback, EventType.ON_NPC_MOVE_NODE_ARRIVED, ListenerRegisterType.NPC, npcIds);
  439. }
  440. // ---------------------------------------------------------------------------------------------------------------------------
  441. /**
  442. * Provides instant callback operation when {@link L2Npc} finishes to move on its route.
  443. * @param callback
  444. * @param npcIds
  445. * @return
  446. */
  447. protected final List<AbstractEventListener> setNpcMoveRouteFinishedId(Consumer<OnNpcMoveRouteFinished> callback, int... npcIds)
  448. {
  449. return registerConsumer(callback, EventType.ON_NPC_MOVE_ROUTE_FINISHED, ListenerRegisterType.NPC, npcIds);
  450. }
  451. /**
  452. * Provides instant callback operation when {@link L2Npc} finishes to move on its route.
  453. * @param callback
  454. * @param npcIds
  455. * @return
  456. */
  457. protected final List<AbstractEventListener> setNpcMoveRouteFinishedId(Consumer<OnNpcMoveRouteFinished> callback, Collection<Integer> npcIds)
  458. {
  459. return registerConsumer(callback, EventType.ON_NPC_MOVE_ROUTE_FINISHED, ListenerRegisterType.NPC, npcIds);
  460. }
  461. // ---------------------------------------------------------------------------------------------------------------------------
  462. /**
  463. * Provides instant callback operation when {@link L2Npc} is about to hate and start attacking a creature.
  464. * @param callback
  465. * @param npcIds
  466. * @return
  467. */
  468. protected final List<AbstractEventListener> setNpcHateId(Consumer<OnAttackableHate> callback, int... npcIds)
  469. {
  470. return registerConsumer(callback, EventType.ON_NPC_HATE, ListenerRegisterType.NPC, npcIds);
  471. }
  472. /**
  473. * Provides instant callback operation when {@link L2Npc} is about to hate and start attacking a creature.
  474. * @param callback
  475. * @param npcIds
  476. * @return
  477. */
  478. protected final List<AbstractEventListener> setNpcHateId(Consumer<OnAttackableHate> callback, Collection<Integer> npcIds)
  479. {
  480. return registerConsumer(callback, EventType.ON_NPC_HATE, ListenerRegisterType.NPC, npcIds);
  481. }
  482. /**
  483. * Provides instant callback operation when {@link L2Npc} is about to hate and start attacking a creature.
  484. * @param callback
  485. * @param npcIds
  486. * @return
  487. */
  488. protected final List<AbstractEventListener> addNpcHateId(Function<OnAttackableHate, TerminateReturn> callback, int... npcIds)
  489. {
  490. return registerFunction(callback, EventType.ON_NPC_HATE, ListenerRegisterType.NPC, npcIds);
  491. }
  492. /**
  493. * Provides instant callback operation when {@link L2Npc} is about to hate and start attacking a creature.
  494. * @param callback
  495. * @param npcIds
  496. * @return
  497. */
  498. protected final List<AbstractEventListener> addNpcHateId(Function<OnAttackableHate, TerminateReturn> callback, Collection<Integer> npcIds)
  499. {
  500. return registerFunction(callback, EventType.ON_NPC_HATE, ListenerRegisterType.NPC, npcIds);
  501. }
  502. // ---------------------------------------------------------------------------------------------------------------------------
  503. /**
  504. * Provides instant callback operation when {@link L2Npc} is about to hate and start attacking a creature.
  505. * @param callback
  506. * @param npcIds
  507. * @return
  508. */
  509. protected final List<AbstractEventListener> setNpcCanBeSeenId(Consumer<OnNpcCanBeSeen> callback, int... npcIds)
  510. {
  511. return registerConsumer(callback, EventType.ON_NPC_CAN_BE_SEEN, ListenerRegisterType.NPC, npcIds);
  512. }
  513. /**
  514. * Provides instant callback operation when {@link L2Npc} is about to hate and start attacking a creature.
  515. * @param callback
  516. * @param npcIds
  517. * @return
  518. */
  519. protected final List<AbstractEventListener> setNpcCanBeSeenId(Consumer<OnNpcCanBeSeen> callback, Collection<Integer> npcIds)
  520. {
  521. return registerConsumer(callback, EventType.ON_NPC_CAN_BE_SEEN, ListenerRegisterType.NPC, npcIds);
  522. }
  523. /**
  524. * Provides instant callback operation when {@link L2Npc} is about to hate and start attacking a creature.
  525. * @param callback
  526. * @param npcIds
  527. * @return
  528. */
  529. protected final List<AbstractEventListener> setNpcCanBeSeenId(Function<OnNpcCanBeSeen, TerminateReturn> callback, int... npcIds)
  530. {
  531. return registerFunction(callback, EventType.ON_NPC_CAN_BE_SEEN, ListenerRegisterType.NPC, npcIds);
  532. }
  533. /**
  534. * Provides instant callback operation when {@link L2Npc} is about to hate and start attacking a creature.
  535. * @param callback
  536. * @param npcIds
  537. * @return
  538. */
  539. protected final List<AbstractEventListener> setNpcCanBeSeenId(Function<OnNpcCanBeSeen, TerminateReturn> callback, Collection<Integer> npcIds)
  540. {
  541. return registerFunction(callback, EventType.ON_NPC_CAN_BE_SEEN, ListenerRegisterType.NPC, npcIds);
  542. }
  543. // ---------------------------------------------------------------------------------------------------------------------------
  544. /**
  545. * Provides instant callback operation when {@link L2Npc} sees another creature.
  546. * @param callback
  547. * @param npcIds
  548. * @return
  549. */
  550. protected final List<AbstractEventListener> setNpcCreatureSeeId(Consumer<OnNpcCreatureSee> callback, int... npcIds)
  551. {
  552. return registerConsumer(callback, EventType.ON_NPC_CREATURE_SEE, ListenerRegisterType.NPC, npcIds);
  553. }
  554. /**
  555. * Provides instant callback operation when {@link L2Npc} sees another creature.
  556. * @param callback
  557. * @param npcIds
  558. * @return
  559. */
  560. protected final List<AbstractEventListener> setNpcCreatureSeeId(Consumer<OnNpcCreatureSee> callback, Collection<Integer> npcIds)
  561. {
  562. return registerConsumer(callback, EventType.ON_NPC_CREATURE_SEE, ListenerRegisterType.NPC, npcIds);
  563. }
  564. // ---------------------------------------------------------------------------------------------------------------------------
  565. /**
  566. * Provides instant callback operation when L2Attackable is under attack to other clan mates.
  567. * @param callback
  568. * @param npcIds
  569. * @return
  570. */
  571. protected final List<AbstractEventListener> setAttackableFactionIdId(Consumer<OnAttackableFactionCall> callback, int... npcIds)
  572. {
  573. return registerConsumer(callback, EventType.ON_ATTACKABLE_FACTION_CALL, ListenerRegisterType.NPC, npcIds);
  574. }
  575. /**
  576. * Provides instant callback operation when L2Attackable is under attack to other clan mates.
  577. * @param callback
  578. * @param npcIds
  579. * @return
  580. */
  581. protected final List<AbstractEventListener> setAttackableFactionIdId(Consumer<OnAttackableFactionCall> callback, Collection<Integer> npcIds)
  582. {
  583. return registerConsumer(callback, EventType.ON_ATTACKABLE_FACTION_CALL, ListenerRegisterType.NPC, npcIds);
  584. }
  585. // ---------------------------------------------------------------------------------------------------------------------------
  586. /**
  587. * Provides instant callback operation when L2Attackable is attacked from a player.
  588. * @param callback
  589. * @param npcIds
  590. * @return
  591. */
  592. protected final List<AbstractEventListener> setAttackableAttackId(Consumer<OnAttackableAttack> callback, int... npcIds)
  593. {
  594. return registerConsumer(callback, EventType.ON_ATTACKABLE_ATTACK, ListenerRegisterType.NPC, npcIds);
  595. }
  596. /**
  597. * Provides instant callback operation when L2Attackable is attacked from a player.
  598. * @param callback
  599. * @param npcIds
  600. * @return
  601. */
  602. protected final List<AbstractEventListener> setAttackableAttackId(Consumer<OnAttackableAttack> callback, Collection<Integer> npcIds)
  603. {
  604. return registerConsumer(callback, EventType.ON_ATTACKABLE_ATTACK, ListenerRegisterType.NPC, npcIds);
  605. }
  606. // ---------------------------------------------------------------------------------------------------------------------------
  607. /**
  608. * Provides instant callback operation when {@link L2PcInstance} enters in {@link L2Attackable}'s aggressive range.
  609. * @param callback
  610. * @param npcIds
  611. * @return
  612. */
  613. protected final List<AbstractEventListener> setAttackableAggroRangeEnterId(Consumer<OnAttackableAggroRangeEnter> callback, int... npcIds)
  614. {
  615. return registerConsumer(callback, EventType.ON_ATTACKABLE_AGGRO_RANGE_ENTER, ListenerRegisterType.NPC, npcIds);
  616. }
  617. /**
  618. * Provides instant callback operation when {@link L2PcInstance} enters in {@link L2Attackable}'s aggressive range.
  619. * @param callback
  620. * @param npcIds
  621. * @return
  622. */
  623. protected final List<AbstractEventListener> setAttackableAggroRangeEnterId(Consumer<OnAttackableAggroRangeEnter> callback, Collection<Integer> npcIds)
  624. {
  625. return registerConsumer(callback, EventType.ON_ATTACKABLE_AGGRO_RANGE_ENTER, ListenerRegisterType.NPC, npcIds);
  626. }
  627. // ---------------------------------------------------------------------------------------------------------------------------
  628. /**
  629. * Provides instant callback operation when {@link L2PcInstance} learn's a {@link Skill}.
  630. * @param callback
  631. * @param npcIds
  632. * @return
  633. */
  634. protected final List<AbstractEventListener> setPlayerSkillLearnId(Consumer<OnPlayerSkillLearn> callback, int... npcIds)
  635. {
  636. return registerConsumer(callback, EventType.ON_PLAYER_SKILL_LEARN, ListenerRegisterType.NPC, npcIds);
  637. }
  638. /**
  639. * Provides instant callback operation when {@link L2PcInstance} learn's a {@link Skill}.
  640. * @param callback
  641. * @param npcIds
  642. * @return
  643. */
  644. protected final List<AbstractEventListener> setPlayerSkillLearnId(Consumer<OnPlayerSkillLearn> callback, Collection<Integer> npcIds)
  645. {
  646. return registerConsumer(callback, EventType.ON_PLAYER_SKILL_LEARN, ListenerRegisterType.NPC, npcIds);
  647. }
  648. // ---------------------------------------------------------------------------------------------------------------------------
  649. /**
  650. * Provides instant callback operation when {@link L2PcInstance} summons a servitor or a pet
  651. * @param callback
  652. * @param npcIds
  653. * @return
  654. */
  655. protected final List<AbstractEventListener> setPlayerSummonSpawnId(Consumer<OnPlayerSummonSpawn> callback, int... npcIds)
  656. {
  657. return registerConsumer(callback, EventType.ON_PLAYER_SUMMON_SPAWN, ListenerRegisterType.NPC, npcIds);
  658. }
  659. /**
  660. * Provides instant callback operation when {@link L2PcInstance} summons a servitor or a pet
  661. * @param callback
  662. * @param npcIds
  663. * @return
  664. */
  665. protected final List<AbstractEventListener> setPlayerSummonSpawnId(Consumer<OnPlayerSummonSpawn> callback, Collection<Integer> npcIds)
  666. {
  667. return registerConsumer(callback, EventType.ON_PLAYER_SUMMON_SPAWN, ListenerRegisterType.NPC, npcIds);
  668. }
  669. // ---------------------------------------------------------------------------------------------------------------------------
  670. /**
  671. * Provides instant callback operation when {@link L2PcInstance} summons a servitor or a pet
  672. * @param callback
  673. * @return
  674. */
  675. protected final List<AbstractEventListener> setPlayerLoginId(Consumer<OnPlayerLogin> callback)
  676. {
  677. return registerConsumer(callback, EventType.ON_PLAYER_LOGIN, ListenerRegisterType.GLOBAL);
  678. }
  679. // ---------------------------------------------------------------------------------------------------------------------------
  680. /**
  681. * Provides instant callback operation when {@link L2PcInstance} summons a servitor or a pet
  682. * @param callback
  683. * @return
  684. */
  685. protected final List<AbstractEventListener> setPlayerLogoutId(Consumer<OnPlayerLogout> callback)
  686. {
  687. return registerConsumer(callback, EventType.ON_PLAYER_LOGOUT, ListenerRegisterType.GLOBAL);
  688. }
  689. // ---------------------------------------------------------------------------------------------------------------------------
  690. /**
  691. * Provides instant callback operation when {@link com.l2jserver.gameserver.model.actor.L2Character} Enters on a {@link L2ZoneType}.
  692. * @param callback
  693. * @param npcIds
  694. * @return
  695. */
  696. protected final List<AbstractEventListener> setCreatureZoneEnterId(Consumer<OnCreatureZoneEnter> callback, int... npcIds)
  697. {
  698. return registerConsumer(callback, EventType.ON_CREATURE_ZONE_ENTER, ListenerRegisterType.ZONE, npcIds);
  699. }
  700. /**
  701. * Provides instant callback operation when {@link com.l2jserver.gameserver.model.actor.L2Character} Enters on a {@link L2ZoneType}.
  702. * @param callback
  703. * @param npcIds
  704. * @return
  705. */
  706. protected final List<AbstractEventListener> setCreatureZoneEnterId(Consumer<OnCreatureZoneEnter> callback, Collection<Integer> npcIds)
  707. {
  708. return registerConsumer(callback, EventType.ON_CREATURE_ZONE_ENTER, ListenerRegisterType.ZONE, npcIds);
  709. }
  710. // ---------------------------------------------------------------------------------------------------------------------------
  711. /**
  712. * Provides instant callback operation when {@link com.l2jserver.gameserver.model.actor.L2Character} Exits on a {@link L2ZoneType}.
  713. * @param callback
  714. * @param npcIds
  715. * @return
  716. */
  717. protected final List<AbstractEventListener> setCreatureZoneExitId(Consumer<OnCreatureZoneExit> callback, int... npcIds)
  718. {
  719. return registerConsumer(callback, EventType.ON_CREATURE_ZONE_EXIT, ListenerRegisterType.ZONE, npcIds);
  720. }
  721. /**
  722. * Provides instant callback operation when {@link com.l2jserver.gameserver.model.actor.L2Character} Exits on a {@link L2ZoneType}.
  723. * @param callback
  724. * @param npcIds
  725. * @return
  726. */
  727. protected final List<AbstractEventListener> setCreatureZoneExitId(Consumer<OnCreatureZoneExit> callback, Collection<Integer> npcIds)
  728. {
  729. return registerConsumer(callback, EventType.ON_CREATURE_ZONE_EXIT, ListenerRegisterType.ZONE, npcIds);
  730. }
  731. // ---------------------------------------------------------------------------------------------------------------------------
  732. /**
  733. * Provides instant callback operation when {@link com.l2jserver.gameserver.model.actor.instance.L2TrapInstance} acts.
  734. * @param callback
  735. * @param npcIds
  736. * @return
  737. */
  738. protected final List<AbstractEventListener> setTrapActionId(Consumer<OnTrapAction> callback, int... npcIds)
  739. {
  740. return registerConsumer(callback, EventType.ON_TRAP_ACTION, ListenerRegisterType.NPC, npcIds);
  741. }
  742. /**
  743. * Provides instant callback operation when {@link com.l2jserver.gameserver.model.actor.instance.L2TrapInstance} acts.
  744. * @param callback
  745. * @param npcIds
  746. * @return
  747. */
  748. protected final List<AbstractEventListener> setTrapActionId(Consumer<OnTrapAction> callback, Collection<Integer> npcIds)
  749. {
  750. return registerConsumer(callback, EventType.ON_TRAP_ACTION, ListenerRegisterType.NPC, npcIds);
  751. }
  752. // ---------------------------------------------------------------------------------------------------------------------------
  753. /**
  754. * Provides instant callback operation when {@link L2Item} receives an event from {@link L2PcInstance}.
  755. * @param callback
  756. * @param npcIds
  757. * @return
  758. */
  759. protected final List<AbstractEventListener> setItemBypassEvenId(Consumer<OnItemBypassEvent> callback, int... npcIds)
  760. {
  761. return registerConsumer(callback, EventType.ON_ITEM_BYPASS_EVENT, ListenerRegisterType.ITEM, npcIds);
  762. }
  763. /**
  764. * Provides instant callback operation when {@link L2Item} receives an event from {@link L2PcInstance}.
  765. * @param callback
  766. * @param npcIds
  767. * @return
  768. */
  769. protected final List<AbstractEventListener> setItemBypassEvenId(Consumer<OnItemBypassEvent> callback, Collection<Integer> npcIds)
  770. {
  771. return registerConsumer(callback, EventType.ON_ITEM_BYPASS_EVENT, ListenerRegisterType.ITEM, npcIds);
  772. }
  773. // ---------------------------------------------------------------------------------------------------------------------------
  774. /**
  775. * Provides instant callback operation when {@link L2PcInstance} talk to {@link L2Item}.
  776. * @param callback
  777. * @param npcIds
  778. * @return
  779. */
  780. protected final List<AbstractEventListener> setItemTalkId(Consumer<OnItemTalk> callback, int... npcIds)
  781. {
  782. return registerConsumer(callback, EventType.ON_ITEM_TALK, ListenerRegisterType.ITEM, npcIds);
  783. }
  784. /**
  785. * Provides instant callback operation when {@link L2PcInstance} talk to {@link L2Item}.
  786. * @param callback
  787. * @param npcIds
  788. * @return
  789. */
  790. protected final List<AbstractEventListener> setItemTalkId(Consumer<OnItemTalk> callback, Collection<Integer> npcIds)
  791. {
  792. return registerConsumer(callback, EventType.ON_ITEM_TALK, ListenerRegisterType.ITEM, npcIds);
  793. }
  794. // ---------------------------------------------------------------------------------------------------------------------------
  795. /**
  796. * Provides instant callback operation when Olympiad match finishes.
  797. * @param callback
  798. * @return
  799. */
  800. protected final List<AbstractEventListener> setOlympiadMatchResult(Consumer<OnOlympiadMatchResult> callback)
  801. {
  802. return registerConsumer(callback, EventType.ON_OLYMPIAD_MATCH_RESULT, ListenerRegisterType.OLYMPIAD);
  803. }
  804. // ---------------------------------------------------------------------------------------------------------------------------
  805. /**
  806. * Provides instant callback operation when castle siege begins
  807. * @param callback
  808. * @param castleIds
  809. * @return
  810. */
  811. protected final List<AbstractEventListener> setCastleSiegeStartId(Consumer<OnCastleSiegeStart> callback, int... castleIds)
  812. {
  813. return registerConsumer(callback, EventType.ON_CASTLE_SIEGE_START, ListenerRegisterType.CASTLE, castleIds);
  814. }
  815. /**
  816. * Provides instant callback operation when castle siege begins
  817. * @param callback
  818. * @param castleIds
  819. * @return
  820. */
  821. protected final List<AbstractEventListener> setCastleSiegeStartId(Consumer<OnCastleSiegeStart> callback, Collection<Integer> castleIds)
  822. {
  823. return registerConsumer(callback, EventType.ON_CASTLE_SIEGE_START, ListenerRegisterType.CASTLE, castleIds);
  824. }
  825. // ---------------------------------------------------------------------------------------------------------------------------
  826. /**
  827. * Provides instant callback operation when Castle owner has changed during a siege
  828. * @param callback
  829. * @param castleIds
  830. * @return
  831. */
  832. protected final List<AbstractEventListener> setCastleSiegeOwnerChangeId(Consumer<OnCastleSiegeOwnerChange> callback, int... castleIds)
  833. {
  834. return registerConsumer(callback, EventType.ON_CASTLE_SIEGE_OWNER_CHANGE, ListenerRegisterType.CASTLE, castleIds);
  835. }
  836. /**
  837. * Provides instant callback operation when Castle owner has changed during a siege
  838. * @param callback
  839. * @param castleIds
  840. * @return
  841. */
  842. protected final List<AbstractEventListener> setCastleSiegeOwnerChangeId(Consumer<OnCastleSiegeOwnerChange> callback, Collection<Integer> castleIds)
  843. {
  844. return registerConsumer(callback, EventType.ON_CASTLE_SIEGE_OWNER_CHANGE, ListenerRegisterType.CASTLE, castleIds);
  845. }
  846. // ---------------------------------------------------------------------------------------------------------------------------
  847. /**
  848. * Provides instant callback operation when castle siege ends
  849. * @param callback
  850. * @param castleIds
  851. * @return
  852. */
  853. protected final List<AbstractEventListener> setCastleSiegeFinishId(Consumer<OnCastleSiegeFinish> callback, int... castleIds)
  854. {
  855. return registerConsumer(callback, EventType.ON_CASTLE_SIEGE_FINISH, ListenerRegisterType.CASTLE, castleIds);
  856. }
  857. /**
  858. * Provides instant callback operation when castle siege ends
  859. * @param callback
  860. * @param castleIds
  861. * @return
  862. */
  863. protected final List<AbstractEventListener> setCastleSiegeFinishId(Consumer<OnCastleSiegeFinish> callback, Collection<Integer> castleIds)
  864. {
  865. return registerConsumer(callback, EventType.ON_CASTLE_SIEGE_FINISH, ListenerRegisterType.CASTLE, castleIds);
  866. }
  867. // ---------------------------------------------------------------------------------------------------------------------------
  868. /**
  869. * Provides instant callback operation when player's profession has change
  870. * @param callback
  871. * @return
  872. */
  873. protected final List<AbstractEventListener> setPlayerProfessionChangeId(Consumer<OnPlayerProfessionChange> callback)
  874. {
  875. return registerConsumer(callback, EventType.ON_PLAYER_PROFESSION_CHANGE, ListenerRegisterType.GLOBAL);
  876. }
  877. // --------------------------------------------------------------------------------------------------
  878. // --------------------------------Default listener register methods---------------------------------
  879. // --------------------------------------------------------------------------------------------------
  880. /**
  881. * Method that registers Function type of listeners (Listeners that need parameters but doesn't return objects)
  882. * @param callback
  883. * @param type
  884. * @param registerType
  885. * @param npcIds
  886. * @return
  887. */
  888. protected final List<AbstractEventListener> registerConsumer(Consumer<? extends IBaseEvent> callback, EventType type, ListenerRegisterType registerType, int... npcIds)
  889. {
  890. return registerListener((container) -> new ConsumerEventListener(container, type, callback, this), registerType, npcIds);
  891. }
  892. /**
  893. * Method that registers Function type of listeners (Listeners that need parameters but doesn't return objects)
  894. * @param callback
  895. * @param type
  896. * @param registerType
  897. * @param npcIds
  898. * @return
  899. */
  900. protected final List<AbstractEventListener> registerConsumer(Consumer<? extends IBaseEvent> callback, EventType type, ListenerRegisterType registerType, Collection<Integer> npcIds)
  901. {
  902. return registerListener((container) -> new ConsumerEventListener(container, type, callback, this), registerType, npcIds);
  903. }
  904. /**
  905. * Method that registers Function type of listeners (Listeners that need parameters and return objects)
  906. * @param callback
  907. * @param type
  908. * @param registerType
  909. * @param npcIds
  910. * @return
  911. */
  912. protected final List<AbstractEventListener> registerFunction(Function<? extends IBaseEvent, ? extends AbstractEventReturn> callback, EventType type, ListenerRegisterType registerType, int... npcIds)
  913. {
  914. return registerListener((container) -> new FunctionEventListener(container, type, callback, this), registerType, npcIds);
  915. }
  916. /**
  917. * Method that registers Function type of listeners (Listeners that need parameters and return objects)
  918. * @param callback
  919. * @param type
  920. * @param registerType
  921. * @param npcIds
  922. * @return
  923. */
  924. protected final List<AbstractEventListener> registerFunction(Function<? extends IBaseEvent, ? extends AbstractEventReturn> callback, EventType type, ListenerRegisterType registerType, Collection<Integer> npcIds)
  925. {
  926. return registerListener((container) -> new FunctionEventListener(container, type, callback, this), registerType, npcIds);
  927. }
  928. /**
  929. * Method that registers runnable type of listeners (Listeners that doesn't needs parameters or return objects)
  930. * @param callback
  931. * @param type
  932. * @param registerType
  933. * @param npcIds
  934. * @return
  935. */
  936. protected final List<AbstractEventListener> registerRunnable(Runnable callback, EventType type, ListenerRegisterType registerType, int... npcIds)
  937. {
  938. return registerListener((container) -> new RunnableEventListener(container, type, callback, this), registerType, npcIds);
  939. }
  940. /**
  941. * Method that registers runnable type of listeners (Listeners that doesn't needs parameters or return objects)
  942. * @param callback
  943. * @param type
  944. * @param registerType
  945. * @param npcIds
  946. * @return
  947. */
  948. protected final List<AbstractEventListener> registerRunnable(Runnable callback, EventType type, ListenerRegisterType registerType, Collection<Integer> npcIds)
  949. {
  950. return registerListener((container) -> new RunnableEventListener(container, type, callback, this), registerType, npcIds);
  951. }
  952. /**
  953. * Method that registers runnable type of listeners (Listeners that doesn't needs parameters or return objects)
  954. * @param callback
  955. * @param type
  956. * @param registerType
  957. * @param npcIds
  958. * @return
  959. */
  960. protected final List<AbstractEventListener> registerAnnotation(Method callback, EventType type, ListenerRegisterType registerType, int... npcIds)
  961. {
  962. return registerListener((container) -> new AnnotationEventListener(container, type, callback, this), registerType, npcIds);
  963. }
  964. /**
  965. * Method that registers runnable type of listeners (Listeners that doesn't needs parameters or return objects)
  966. * @param callback
  967. * @param type
  968. * @param registerType
  969. * @param npcIds
  970. * @return
  971. */
  972. protected final List<AbstractEventListener> registerAnnotation(Method callback, EventType type, ListenerRegisterType registerType, Collection<Integer> npcIds)
  973. {
  974. return registerListener((container) -> new AnnotationEventListener(container, type, callback, this), registerType, npcIds);
  975. }
  976. /**
  977. * Method that registers dummy type of listeners (Listeners doesn't gets notification but just used to check if their type present or not)
  978. * @param type
  979. * @param registerType
  980. * @param npcIds
  981. * @return
  982. */
  983. protected final List<AbstractEventListener> registerDummy(EventType type, ListenerRegisterType registerType, int... npcIds)
  984. {
  985. return registerListener((container) -> new DummyEventListener(container, type, this), registerType, npcIds);
  986. }
  987. /**
  988. * Method that registers dummy type of listeners (Listeners doesn't gets notification but just used to check if their type present or not)
  989. * @param type
  990. * @param registerType
  991. * @param npcIds
  992. * @return
  993. */
  994. protected final List<AbstractEventListener> registerDummy(EventType type, ListenerRegisterType registerType, Collection<Integer> npcIds)
  995. {
  996. return registerListener((container) -> new DummyEventListener(container, type, this), registerType, npcIds);
  997. }
  998. // --------------------------------------------------------------------------------------------------
  999. // --------------------------------------Register methods--------------------------------------------
  1000. // --------------------------------------------------------------------------------------------------
  1001. /**
  1002. * Generic listener register method
  1003. * @param action
  1004. * @param registerType
  1005. * @param ids
  1006. * @return
  1007. */
  1008. protected final List<AbstractEventListener> registerListener(Function<ListenersContainer, AbstractEventListener> action, ListenerRegisterType registerType, int... ids)
  1009. {
  1010. final List<AbstractEventListener> listeners = new ArrayList<>(ids.length > 0 ? ids.length : 1);
  1011. if (ids.length > 0)
  1012. {
  1013. for (int id : ids)
  1014. {
  1015. switch (registerType)
  1016. {
  1017. case NPC:
  1018. {
  1019. final L2NpcTemplate template = NpcData.getInstance().getTemplate(id);
  1020. if (template != null)
  1021. {
  1022. listeners.add(template.addListener(action.apply(template)));
  1023. }
  1024. break;
  1025. }
  1026. case ZONE:
  1027. {
  1028. final L2ZoneType template = ZoneManager.getInstance().getZoneById(id);
  1029. if (template != null)
  1030. {
  1031. listeners.add(template.addListener(action.apply(template)));
  1032. }
  1033. break;
  1034. }
  1035. case ITEM:
  1036. {
  1037. final L2Item template = ItemTable.getInstance().getTemplate(id);
  1038. if (template != null)
  1039. {
  1040. listeners.add(template.addListener(action.apply(template)));
  1041. }
  1042. break;
  1043. }
  1044. case CASTLE:
  1045. {
  1046. final Castle template = CastleManager.getInstance().getCastleById(id);
  1047. if (template != null)
  1048. {
  1049. listeners.add(template.addListener(action.apply(template)));
  1050. }
  1051. break;
  1052. }
  1053. case FORTRESS:
  1054. {
  1055. final Fort template = FortManager.getInstance().getFortById(id);
  1056. if (template != null)
  1057. {
  1058. listeners.add(template.addListener(action.apply(template)));
  1059. }
  1060. break;
  1061. }
  1062. default:
  1063. {
  1064. _log.log(Level.WARNING, getClass().getSimpleName() + ": Unhandled register type: " + registerType);
  1065. }
  1066. }
  1067. if (!_registeredIds.containsKey(registerType))
  1068. {
  1069. _registeredIds.put(registerType, new FastList<Integer>().shared());
  1070. }
  1071. _registeredIds.get(registerType).add(id);
  1072. }
  1073. }
  1074. else
  1075. {
  1076. switch (registerType)
  1077. {
  1078. case OLYMPIAD:
  1079. {
  1080. final Olympiad template = Olympiad.getInstance();
  1081. listeners.add(template.addListener(action.apply(template)));
  1082. break;
  1083. }
  1084. default: // Global Listener
  1085. {
  1086. final EventDispatcher template = EventDispatcher.getInstance();
  1087. listeners.add(template.addListener(action.apply(template)));
  1088. }
  1089. }
  1090. }
  1091. _listeners.addAll(listeners);
  1092. return listeners;
  1093. }
  1094. /**
  1095. * Generic listener register method
  1096. * @param action
  1097. * @param registerType
  1098. * @param ids
  1099. * @return
  1100. */
  1101. protected final List<AbstractEventListener> registerListener(Function<ListenersContainer, AbstractEventListener> action, ListenerRegisterType registerType, Collection<Integer> ids)
  1102. {
  1103. final List<AbstractEventListener> listeners = new ArrayList<>(!ids.isEmpty() ? ids.size() : 1);
  1104. if (!ids.isEmpty())
  1105. {
  1106. for (int id : ids)
  1107. {
  1108. switch (registerType)
  1109. {
  1110. case NPC:
  1111. {
  1112. final L2NpcTemplate template = NpcData.getInstance().getTemplate(id);
  1113. if (template != null)
  1114. {
  1115. listeners.add(template.addListener(action.apply(template)));
  1116. }
  1117. break;
  1118. }
  1119. case ZONE:
  1120. {
  1121. final L2ZoneType template = ZoneManager.getInstance().getZoneById(id);
  1122. if (template != null)
  1123. {
  1124. listeners.add(template.addListener(action.apply(template)));
  1125. }
  1126. break;
  1127. }
  1128. case ITEM:
  1129. {
  1130. final L2Item template = ItemTable.getInstance().getTemplate(id);
  1131. if (template != null)
  1132. {
  1133. listeners.add(template.addListener(action.apply(template)));
  1134. }
  1135. break;
  1136. }
  1137. case CASTLE:
  1138. {
  1139. final Castle template = CastleManager.getInstance().getCastleById(id);
  1140. if (template != null)
  1141. {
  1142. listeners.add(template.addListener(action.apply(template)));
  1143. }
  1144. break;
  1145. }
  1146. case FORTRESS:
  1147. {
  1148. final Fort template = FortManager.getInstance().getFortById(id);
  1149. if (template != null)
  1150. {
  1151. listeners.add(template.addListener(action.apply(template)));
  1152. }
  1153. break;
  1154. }
  1155. default:
  1156. {
  1157. _log.log(Level.WARNING, getClass().getSimpleName() + ": Unhandled register type: " + registerType);
  1158. }
  1159. }
  1160. }
  1161. if (!_registeredIds.containsKey(registerType))
  1162. {
  1163. _registeredIds.put(registerType, new FastList<Integer>().shared());
  1164. }
  1165. _registeredIds.get(registerType).addAll(ids);
  1166. }
  1167. else
  1168. {
  1169. switch (registerType)
  1170. {
  1171. case OLYMPIAD:
  1172. {
  1173. final Olympiad template = Olympiad.getInstance();
  1174. listeners.add(template.addListener(action.apply(template)));
  1175. break;
  1176. }
  1177. default: // Global Listener
  1178. {
  1179. final EventDispatcher template = EventDispatcher.getInstance();
  1180. listeners.add(template.addListener(action.apply(template)));
  1181. }
  1182. }
  1183. }
  1184. _listeners.addAll(listeners);
  1185. return listeners;
  1186. }
  1187. public List<Integer> getRegisteredIds(ListenerRegisterType type)
  1188. {
  1189. return _registeredIds.containsKey(type) ? _registeredIds.get(type) : Collections.emptyList();
  1190. }
  1191. public List<AbstractEventListener> getListeners()
  1192. {
  1193. return _listeners;
  1194. }
  1195. /**
  1196. * -------------------------------------------------------------------------------------------------------
  1197. */
  1198. /**
  1199. * Show an on screen message to the player.
  1200. * @param player the player to display the message to
  1201. * @param text the message to display
  1202. * @param time the duration of the message in milliseconds
  1203. */
  1204. public static void showOnScreenMsg(L2PcInstance player, String text, int time)
  1205. {
  1206. player.sendPacket(new ExShowScreenMessage(text, time));
  1207. }
  1208. /**
  1209. * Show an on screen message to the player.
  1210. * @param player the player to display the message to
  1211. * @param npcString the NPC string to display
  1212. * @param position the position of the message on the screen
  1213. * @param time the duration of the message in milliseconds
  1214. * @param params values of parameters to replace in the NPC String (like S1, C1 etc.)
  1215. */
  1216. public static void showOnScreenMsg(L2PcInstance player, NpcStringId npcString, int position, int time, String... params)
  1217. {
  1218. player.sendPacket(new ExShowScreenMessage(npcString, position, time, params));
  1219. }
  1220. /**
  1221. * Show an on screen message to the player.
  1222. * @param player the player to display the message to
  1223. * @param systemMsg the system message to display
  1224. * @param position the position of the message on the screen
  1225. * @param time the duration of the message in milliseconds
  1226. * @param params values of parameters to replace in the system message (like S1, C1 etc.)
  1227. */
  1228. public static void showOnScreenMsg(L2PcInstance player, SystemMessageId systemMsg, int position, int time, String... params)
  1229. {
  1230. player.sendPacket(new ExShowScreenMessage(systemMsg, position, time, params));
  1231. }
  1232. /**
  1233. * Add a temporary spawn of the specified NPC.
  1234. * @param npcId the ID of the NPC to spawn
  1235. * @param pos the object containing the spawn location coordinates
  1236. * @return the {@link L2Npc} object of the newly spawned NPC or {@code null} if the NPC doesn't exist
  1237. * @see #addSpawn(int, IPositionable, boolean, long, boolean, int)
  1238. * @see #addSpawn(int, int, int, int, int, boolean, long, boolean, int)
  1239. */
  1240. public static L2Npc addSpawn(int npcId, IPositionable pos)
  1241. {
  1242. return addSpawn(npcId, pos.getX(), pos.getY(), pos.getZ(), pos.getHeading(), false, 0, false, 0);
  1243. }
  1244. /**
  1245. * Add a temporary spawn of the specified NPC.
  1246. * @param npcId the ID of the NPC to spawn
  1247. * @param pos the object containing the spawn location coordinates
  1248. * @param isSummonSpawn if {@code true}, displays a summon animation on NPC spawn
  1249. * @return the {@link L2Npc} object of the newly spawned NPC or {@code null} if the NPC doesn't exist
  1250. * @see #addSpawn(int, IPositionable, boolean, long, boolean, int)
  1251. * @see #addSpawn(int, int, int, int, int, boolean, long, boolean, int)
  1252. */
  1253. public static L2Npc addSpawn(int npcId, IPositionable pos, boolean isSummonSpawn)
  1254. {
  1255. return addSpawn(npcId, pos.getX(), pos.getY(), pos.getZ(), pos.getHeading(), false, 0, isSummonSpawn, 0);
  1256. }
  1257. /**
  1258. * Add a temporary spawn of the specified NPC.
  1259. * @param npcId the ID of the NPC to spawn
  1260. * @param pos the object containing the spawn location coordinates
  1261. * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
  1262. * @param despawnDelay time in milliseconds till the NPC is despawned (0 - only despawned on server shutdown)
  1263. * @return the {@link L2Npc} object of the newly spawned NPC or {@code null} if the NPC doesn't exist
  1264. * @see #addSpawn(int, IPositionable, boolean, long, boolean, int)
  1265. * @see #addSpawn(int, int, int, int, int, boolean, long, boolean, int)
  1266. */
  1267. public static L2Npc addSpawn(int npcId, IPositionable pos, boolean randomOffset, long despawnDelay)
  1268. {
  1269. return addSpawn(npcId, pos.getX(), pos.getY(), pos.getZ(), pos.getHeading(), randomOffset, despawnDelay, false, 0);
  1270. }
  1271. /**
  1272. * Add a temporary spawn of the specified NPC.
  1273. * @param npcId the ID of the NPC to spawn
  1274. * @param pos the object containing the spawn location coordinates
  1275. * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
  1276. * @param despawnDelay time in milliseconds till the NPC is despawned (0 - only despawned on server shutdown)
  1277. * @param isSummonSpawn if {@code true}, displays a summon animation on NPC spawn
  1278. * @return the {@link L2Npc} object of the newly spawned NPC or {@code null} if the NPC doesn't exist
  1279. * @see #addSpawn(int, IPositionable, boolean, long, boolean, int)
  1280. * @see #addSpawn(int, int, int, int, int, boolean, long, boolean, int)
  1281. */
  1282. public static L2Npc addSpawn(int npcId, IPositionable pos, boolean randomOffset, long despawnDelay, boolean isSummonSpawn)
  1283. {
  1284. return addSpawn(npcId, pos.getX(), pos.getY(), pos.getZ(), pos.getHeading(), randomOffset, despawnDelay, isSummonSpawn, 0);
  1285. }
  1286. /**
  1287. * Add a temporary spawn of the specified NPC.
  1288. * @param npcId the ID of the NPC to spawn
  1289. * @param pos the object containing the spawn location coordinates
  1290. * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
  1291. * @param despawnDelay time in milliseconds till the NPC is despawned (0 - only despawned on server shutdown)
  1292. * @param isSummonSpawn if {@code true}, displays a summon animation on NPC spawn
  1293. * @param instanceId the ID of the instance to spawn the NPC in (0 - the open world)
  1294. * @return the {@link L2Npc} object of the newly spawned NPC or {@code null} if the NPC doesn't exist
  1295. * @see #addSpawn(int, IPositionable)
  1296. * @see #addSpawn(int, IPositionable, boolean)
  1297. * @see #addSpawn(int, IPositionable, boolean, long)
  1298. * @see #addSpawn(int, IPositionable, boolean, long, boolean)
  1299. * @see #addSpawn(int, int, int, int, int, boolean, long, boolean, int)
  1300. */
  1301. public static L2Npc addSpawn(int npcId, IPositionable pos, boolean randomOffset, long despawnDelay, boolean isSummonSpawn, int instanceId)
  1302. {
  1303. return addSpawn(npcId, pos.getX(), pos.getY(), pos.getZ(), pos.getHeading(), randomOffset, despawnDelay, isSummonSpawn, instanceId);
  1304. }
  1305. /**
  1306. * Add a temporary spawn of the specified NPC.
  1307. * @param npcId the ID of the NPC to spawn
  1308. * @param x the X coordinate of the spawn location
  1309. * @param y the Y coordinate of the spawn location
  1310. * @param z the Z coordinate (height) of the spawn location
  1311. * @param heading the heading of the NPC
  1312. * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
  1313. * @param despawnDelay time in milliseconds till the NPC is despawned (0 - only despawned on server shutdown)
  1314. * @return the {@link L2Npc} object of the newly spawned NPC or {@code null} if the NPC doesn't exist
  1315. * @see #addSpawn(int, IPositionable, boolean, long, boolean, int)
  1316. * @see #addSpawn(int, int, int, int, int, boolean, long, boolean, int)
  1317. */
  1318. public static L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, long despawnDelay)
  1319. {
  1320. return addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, false, 0);
  1321. }
  1322. /**
  1323. * Add a temporary spawn of the specified NPC.
  1324. * @param npcId the ID of the NPC to spawn
  1325. * @param x the X coordinate of the spawn location
  1326. * @param y the Y coordinate of the spawn location
  1327. * @param z the Z coordinate (height) of the spawn location
  1328. * @param heading the heading of the NPC
  1329. * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
  1330. * @param despawnDelay time in milliseconds till the NPC is despawned (0 - only despawned on server shutdown)
  1331. * @param isSummonSpawn if {@code true}, displays a summon animation on NPC spawn
  1332. * @return the {@link L2Npc} object of the newly spawned NPC or {@code null} if the NPC doesn't exist
  1333. * @see #addSpawn(int, IPositionable, boolean, long, boolean, int)
  1334. * @see #addSpawn(int, int, int, int, int, boolean, long, boolean, int)
  1335. */
  1336. public static L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, long despawnDelay, boolean isSummonSpawn)
  1337. {
  1338. return addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, isSummonSpawn, 0);
  1339. }
  1340. /**
  1341. * Add a temporary spawn of the specified NPC.
  1342. * @param npcId the ID of the NPC to spawn
  1343. * @param x the X coordinate of the spawn location
  1344. * @param y the Y coordinate of the spawn location
  1345. * @param z the Z coordinate (height) of the spawn location
  1346. * @param heading the heading of the NPC
  1347. * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
  1348. * @param despawnDelay time in milliseconds till the NPC is despawned (0 - only despawned on server shutdown)
  1349. * @param isSummonSpawn if {@code true}, displays a summon animation on NPC spawn
  1350. * @param instanceId the ID of the instance to spawn the NPC in (0 - the open world)
  1351. * @return the {@link L2Npc} object of the newly spawned NPC or {@code null} if the NPC doesn't exist
  1352. * @see #addSpawn(int, IPositionable, boolean, long, boolean, int)
  1353. * @see #addSpawn(int, int, int, int, int, boolean, long)
  1354. * @see #addSpawn(int, int, int, int, int, boolean, long, boolean)
  1355. */
  1356. public static L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, long despawnDelay, boolean isSummonSpawn, int instanceId)
  1357. {
  1358. try
  1359. {
  1360. L2NpcTemplate template = NpcData.getInstance().getTemplate(npcId);
  1361. if (template == null)
  1362. {
  1363. _log.log(Level.SEVERE, "addSpawn(): no NPC template found for NPC #" + npcId + "!");
  1364. }
  1365. else
  1366. {
  1367. if ((x == 0) && (y == 0))
  1368. {
  1369. _log.log(Level.SEVERE, "addSpawn(): invalid spawn coordinates for NPC #" + npcId + "!");
  1370. return null;
  1371. }
  1372. if (randomOffset)
  1373. {
  1374. int offset = Rnd.get(50, 100);
  1375. if (Rnd.nextBoolean())
  1376. {
  1377. offset *= -1;
  1378. }
  1379. x += offset;
  1380. offset = Rnd.get(50, 100);
  1381. if (Rnd.nextBoolean())
  1382. {
  1383. offset *= -1;
  1384. }
  1385. y += offset;
  1386. }
  1387. L2Spawn spawn = new L2Spawn(template);
  1388. spawn.setInstanceId(instanceId);
  1389. spawn.setHeading(heading);
  1390. spawn.setX(x);
  1391. spawn.setY(y);
  1392. spawn.setZ(z);
  1393. spawn.stopRespawn();
  1394. L2Npc result = spawn.spawnOne(isSummonSpawn);
  1395. if (despawnDelay > 0)
  1396. {
  1397. result.scheduleDespawn(despawnDelay);
  1398. }
  1399. return result;
  1400. }
  1401. }
  1402. catch (Exception e1)
  1403. {
  1404. _log.warning("Could not spawn NPC #" + npcId + "; error: " + e1.getMessage());
  1405. }
  1406. return null;
  1407. }
  1408. /**
  1409. * @param trapId
  1410. * @param x
  1411. * @param y
  1412. * @param z
  1413. * @param heading
  1414. * @param skill
  1415. * @param instanceId
  1416. * @return
  1417. */
  1418. public L2TrapInstance addTrap(int trapId, int x, int y, int z, int heading, Skill skill, int instanceId)
  1419. {
  1420. final L2NpcTemplate npcTemplate = NpcData.getInstance().getTemplate(trapId);
  1421. L2TrapInstance trap = new L2TrapInstance(IdFactory.getInstance().getNextId(), npcTemplate, instanceId, -1);
  1422. trap.setCurrentHp(trap.getMaxHp());
  1423. trap.setCurrentMp(trap.getMaxMp());
  1424. trap.setIsInvul(true);
  1425. trap.setHeading(heading);
  1426. trap.spawnMe(x, y, z);
  1427. return trap;
  1428. }
  1429. /**
  1430. * @param master
  1431. * @param minionId
  1432. * @return
  1433. */
  1434. public L2Npc addMinion(L2MonsterInstance master, int minionId)
  1435. {
  1436. return MinionList.spawnMinion(master, minionId);
  1437. }
  1438. /**
  1439. * Get the amount of an item in player's inventory.
  1440. * @param player the player whose inventory to check
  1441. * @param itemId the ID of the item whose amount to get
  1442. * @return the amount of the specified item in player's inventory
  1443. */
  1444. public static long getQuestItemsCount(L2PcInstance player, int itemId)
  1445. {
  1446. return player.getInventory().getInventoryItemCount(itemId, -1);
  1447. }
  1448. /**
  1449. * Get the total amount of all specified items in player's inventory.
  1450. * @param player the player whose inventory to check
  1451. * @param itemIds a list of IDs of items whose amount to get
  1452. * @return the summary amount of all listed items in player's inventory
  1453. */
  1454. public long getQuestItemsCount(L2PcInstance player, int... itemIds)
  1455. {
  1456. long count = 0;
  1457. for (L2ItemInstance item : player.getInventory().getItems())
  1458. {
  1459. if (item == null)
  1460. {
  1461. continue;
  1462. }
  1463. for (int itemId : itemIds)
  1464. {
  1465. if (item.getId() == itemId)
  1466. {
  1467. if ((count + item.getCount()) > Long.MAX_VALUE)
  1468. {
  1469. return Long.MAX_VALUE;
  1470. }
  1471. count += item.getCount();
  1472. }
  1473. }
  1474. }
  1475. return count;
  1476. }
  1477. /**
  1478. * Check if the player has the specified item in his inventory.
  1479. * @param player the player whose inventory to check for the specified item
  1480. * @param item the {@link ItemHolder} object containing the ID and count of the item to check
  1481. * @return {@code true} if the player has the required count of the item
  1482. */
  1483. protected static boolean hasItem(L2PcInstance player, ItemHolder item)
  1484. {
  1485. return hasItem(player, item, true);
  1486. }
  1487. /**
  1488. * Check if the player has the required count of the specified item in his inventory.
  1489. * @param player the player whose inventory to check for the specified item
  1490. * @param item the {@link ItemHolder} object containing the ID and count of the item to check
  1491. * @param checkCount if {@code true}, check if each item is at least of the count specified in the ItemHolder,<br>
  1492. * otherwise check only if the player has the item at all
  1493. * @return {@code true} if the player has the item
  1494. */
  1495. protected static boolean hasItem(L2PcInstance player, ItemHolder item, boolean checkCount)
  1496. {
  1497. if (item == null)
  1498. {
  1499. return false;
  1500. }
  1501. if (checkCount)
  1502. {
  1503. return (getQuestItemsCount(player, item.getId()) >= item.getCount());
  1504. }
  1505. return hasQuestItems(player, item.getId());
  1506. }
  1507. /**
  1508. * Check if the player has all the specified items in his inventory and, if necessary, if their count is also as required.
  1509. * @param player the player whose inventory to check for the specified item
  1510. * @param checkCount if {@code true}, check if each item is at least of the count specified in the ItemHolder,<br>
  1511. * otherwise check only if the player has the item at all
  1512. * @param itemList a list of {@link ItemHolder} objects containing the IDs of the items to check
  1513. * @return {@code true} if the player has all the items from the list
  1514. */
  1515. protected static boolean hasAllItems(L2PcInstance player, boolean checkCount, ItemHolder... itemList)
  1516. {
  1517. if ((itemList == null) || (itemList.length == 0))
  1518. {
  1519. return false;
  1520. }
  1521. for (ItemHolder item : itemList)
  1522. {
  1523. if (!hasItem(player, item, checkCount))
  1524. {
  1525. return false;
  1526. }
  1527. }
  1528. return true;
  1529. }
  1530. /**
  1531. * Check for an item in player's inventory.
  1532. * @param player the player whose inventory to check for quest items
  1533. * @param itemId the ID of the item to check for
  1534. * @return {@code true} if the item exists in player's inventory, {@code false} otherwise
  1535. */
  1536. public static boolean hasQuestItems(L2PcInstance player, int itemId)
  1537. {
  1538. return (player.getInventory().getItemByItemId(itemId) != null);
  1539. }
  1540. /**
  1541. * Check for multiple items in player's inventory.
  1542. * @param player the player whose inventory to check for quest items
  1543. * @param itemIds a list of item IDs to check for
  1544. * @return {@code true} if all items exist in player's inventory, {@code false} otherwise
  1545. */
  1546. public static boolean hasQuestItems(L2PcInstance player, int... itemIds)
  1547. {
  1548. if ((itemIds == null) || (itemIds.length == 0))
  1549. {
  1550. return false;
  1551. }
  1552. final PcInventory inv = player.getInventory();
  1553. for (int itemId : itemIds)
  1554. {
  1555. if (inv.getItemByItemId(itemId) == null)
  1556. {
  1557. return false;
  1558. }
  1559. }
  1560. return true;
  1561. }
  1562. /**
  1563. * Check for multiple items in player's inventory.
  1564. * @param player the player whose inventory to check for quest items
  1565. * @param itemIds a list of item IDs to check for
  1566. * @return {@code true} if at least one items exist in player's inventory, {@code false} otherwise
  1567. */
  1568. public boolean hasAtLeastOneQuestItem(L2PcInstance player, int... itemIds)
  1569. {
  1570. final PcInventory inv = player.getInventory();
  1571. for (int itemId : itemIds)
  1572. {
  1573. if (inv.getItemByItemId(itemId) != null)
  1574. {
  1575. return true;
  1576. }
  1577. }
  1578. return false;
  1579. }
  1580. /**
  1581. * Get the enchantment level of an item in player's inventory.
  1582. * @param player the player whose item to check
  1583. * @param itemId the ID of the item whose enchantment level to get
  1584. * @return the enchantment level of the item or 0 if the item was not found
  1585. */
  1586. public static int getEnchantLevel(L2PcInstance player, int itemId)
  1587. {
  1588. final L2ItemInstance enchantedItem = player.getInventory().getItemByItemId(itemId);
  1589. if (enchantedItem == null)
  1590. {
  1591. return 0;
  1592. }
  1593. return enchantedItem.getEnchantLevel();
  1594. }
  1595. /**
  1596. * Give Adena to the player.
  1597. * @param player the player to whom to give the Adena
  1598. * @param count the amount of Adena to give
  1599. * @param applyRates if {@code true} quest rates will be applied to the amount
  1600. */
  1601. public void giveAdena(L2PcInstance player, long count, boolean applyRates)
  1602. {
  1603. if (applyRates)
  1604. {
  1605. rewardItems(player, Inventory.ADENA_ID, count);
  1606. }
  1607. else
  1608. {
  1609. giveItems(player, Inventory.ADENA_ID, count);
  1610. }
  1611. }
  1612. /**
  1613. * Give a reward to player using multipliers.
  1614. * @param player the player to whom to give the item
  1615. * @param holder
  1616. */
  1617. public static void rewardItems(L2PcInstance player, ItemHolder holder)
  1618. {
  1619. rewardItems(player, holder.getId(), holder.getCount());
  1620. }
  1621. /**
  1622. * Give a reward to player using multipliers.
  1623. * @param player the player to whom to give the item
  1624. * @param itemId the ID of the item to give
  1625. * @param count the amount of items to give
  1626. */
  1627. public static void rewardItems(L2PcInstance player, int itemId, long count)
  1628. {
  1629. if (count <= 0)
  1630. {
  1631. return;
  1632. }
  1633. final L2ItemInstance _tmpItem = ItemTable.getInstance().createDummyItem(itemId);
  1634. if (_tmpItem == null)
  1635. {
  1636. return;
  1637. }
  1638. try
  1639. {
  1640. if (itemId == Inventory.ADENA_ID)
  1641. {
  1642. count *= Config.RATE_QUEST_REWARD_ADENA;
  1643. }
  1644. else if (Config.RATE_QUEST_REWARD_USE_MULTIPLIERS)
  1645. {
  1646. if (_tmpItem.isEtcItem())
  1647. {
  1648. switch (_tmpItem.getEtcItem().getItemType())
  1649. {
  1650. case POTION:
  1651. count *= Config.RATE_QUEST_REWARD_POTION;
  1652. break;
  1653. case SCRL_ENCHANT_WP:
  1654. case SCRL_ENCHANT_AM:
  1655. case SCROLL:
  1656. count *= Config.RATE_QUEST_REWARD_SCROLL;
  1657. break;
  1658. case RECIPE:
  1659. count *= Config.RATE_QUEST_REWARD_RECIPE;
  1660. break;
  1661. case MATERIAL:
  1662. count *= Config.RATE_QUEST_REWARD_MATERIAL;
  1663. break;
  1664. default:
  1665. count *= Config.RATE_QUEST_REWARD;
  1666. }
  1667. }
  1668. }
  1669. else
  1670. {
  1671. count *= Config.RATE_QUEST_REWARD;
  1672. }
  1673. }
  1674. catch (Exception e)
  1675. {
  1676. count = Long.MAX_VALUE;
  1677. }
  1678. // Add items to player's inventory
  1679. L2ItemInstance item = player.getInventory().addItem("Quest", itemId, count, player, player.getTarget());
  1680. if (item == null)
  1681. {
  1682. return;
  1683. }
  1684. sendItemGetMessage(player, item, count);
  1685. }
  1686. /**
  1687. * Send the system message and the status update packets to the player.
  1688. * @param player the player that has got the item
  1689. * @param item the item obtain by the player
  1690. * @param count the item count
  1691. */
  1692. private static void sendItemGetMessage(L2PcInstance player, L2ItemInstance item, long count)
  1693. {
  1694. // If item for reward is gold, send message of gold reward to client
  1695. if (item.getId() == Inventory.ADENA_ID)
  1696. {
  1697. SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S1_ADENA);
  1698. smsg.addLong(count);
  1699. player.sendPacket(smsg);
  1700. }
  1701. // Otherwise, send message of object reward to client
  1702. else
  1703. {
  1704. if (count > 1)
  1705. {
  1706. SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S2_S1_S);
  1707. smsg.addItemName(item);
  1708. smsg.addLong(count);
  1709. player.sendPacket(smsg);
  1710. }
  1711. else
  1712. {
  1713. SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
  1714. smsg.addItemName(item);
  1715. player.sendPacket(smsg);
  1716. }
  1717. }
  1718. // send packets
  1719. StatusUpdate su = new StatusUpdate(player);
  1720. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  1721. player.sendPacket(su);
  1722. }
  1723. /**
  1724. * Give item/reward to the player
  1725. * @param player
  1726. * @param itemId
  1727. * @param count
  1728. */
  1729. public static void giveItems(L2PcInstance player, int itemId, long count)
  1730. {
  1731. giveItems(player, itemId, count, 0);
  1732. }
  1733. /**
  1734. * Give item/reward to the player
  1735. * @param player
  1736. * @param holder
  1737. */
  1738. protected static void giveItems(L2PcInstance player, ItemHolder holder)
  1739. {
  1740. giveItems(player, holder.getId(), holder.getCount());
  1741. }
  1742. /**
  1743. * @param player
  1744. * @param itemId
  1745. * @param count
  1746. * @param enchantlevel
  1747. */
  1748. public static void giveItems(L2PcInstance player, int itemId, long count, int enchantlevel)
  1749. {
  1750. if (count <= 0)
  1751. {
  1752. return;
  1753. }
  1754. // Add items to player's inventory
  1755. final L2ItemInstance item = player.getInventory().addItem("Quest", itemId, count, player, player.getTarget());
  1756. if (item == null)
  1757. {
  1758. return;
  1759. }
  1760. // set enchant level for item if that item is not adena
  1761. if ((enchantlevel > 0) && (itemId != Inventory.ADENA_ID))
  1762. {
  1763. item.setEnchantLevel(enchantlevel);
  1764. }
  1765. sendItemGetMessage(player, item, count);
  1766. }
  1767. /**
  1768. * @param player
  1769. * @param itemId
  1770. * @param count
  1771. * @param attributeId
  1772. * @param attributeLevel
  1773. */
  1774. public static void giveItems(L2PcInstance player, int itemId, long count, byte attributeId, int attributeLevel)
  1775. {
  1776. if (count <= 0)
  1777. {
  1778. return;
  1779. }
  1780. // Add items to player's inventory
  1781. final L2ItemInstance item = player.getInventory().addItem("Quest", itemId, count, player, player.getTarget());
  1782. if (item == null)
  1783. {
  1784. return;
  1785. }
  1786. // set enchant level for item if that item is not adena
  1787. if ((attributeId >= 0) && (attributeLevel > 0))
  1788. {
  1789. item.setElementAttr(attributeId, attributeLevel);
  1790. if (item.isEquipped())
  1791. {
  1792. item.updateElementAttrBonus(player);
  1793. }
  1794. InventoryUpdate iu = new InventoryUpdate();
  1795. iu.addModifiedItem(item);
  1796. player.sendPacket(iu);
  1797. }
  1798. sendItemGetMessage(player, item, count);
  1799. }
  1800. /**
  1801. * Give the specified player a set amount of items if he is lucky enough.<br>
  1802. * Not recommended to use this for non-stacking items.
  1803. * @param player the player to give the item(s) to
  1804. * @param itemId the ID of the item to give
  1805. * @param amountToGive the amount of items to give
  1806. * @param limit the maximum amount of items the player can have. Won't give more if this limit is reached. 0 - no limit.
  1807. * @param dropChance the drop chance as a decimal digit from 0 to 1
  1808. * @param playSound if true, plays ItemSound.quest_itemget when items are given and ItemSound.quest_middle when the limit is reached
  1809. * @return {@code true} if limit > 0 and the limit was reached or if limit <= 0 and items were given; {@code false} in all other cases
  1810. */
  1811. public static boolean giveItemRandomly(L2PcInstance player, int itemId, long amountToGive, long limit, double dropChance, boolean playSound)
  1812. {
  1813. return giveItemRandomly(player, null, itemId, amountToGive, amountToGive, limit, dropChance, playSound);
  1814. }
  1815. /**
  1816. * Give the specified player a set amount of items if he is lucky enough.<br>
  1817. * Not recommended to use this for non-stacking items.
  1818. * @param player the player to give the item(s) to
  1819. * @param npc the NPC that "dropped" the item (can be null)
  1820. * @param itemId the ID of the item to give
  1821. * @param amountToGive the amount of items to give
  1822. * @param limit the maximum amount of items the player can have. Won't give more if this limit is reached. 0 - no limit.
  1823. * @param dropChance the drop chance as a decimal digit from 0 to 1
  1824. * @param playSound if true, plays ItemSound.quest_itemget when items are given and ItemSound.quest_middle when the limit is reached
  1825. * @return {@code true} if limit > 0 and the limit was reached or if limit <= 0 and items were given; {@code false} in all other cases
  1826. */
  1827. public static boolean giveItemRandomly(L2PcInstance player, L2Npc npc, int itemId, long amountToGive, long limit, double dropChance, boolean playSound)
  1828. {
  1829. return giveItemRandomly(player, npc, itemId, amountToGive, amountToGive, limit, dropChance, playSound);
  1830. }
  1831. /**
  1832. * Give the specified player a random amount of items if he is lucky enough.<br>
  1833. * Not recommended to use this for non-stacking items.
  1834. * @param player the player to give the item(s) to
  1835. * @param npc the NPC that "dropped" the item (can be null)
  1836. * @param itemId the ID of the item to give
  1837. * @param minAmount the minimum amount of items to give
  1838. * @param maxAmount the maximum amount of items to give (will give a random amount between min/maxAmount multiplied by quest rates)
  1839. * @param limit the maximum amount of items the player can have. Won't give more if this limit is reached. 0 - no limit.
  1840. * @param dropChance the drop chance as a decimal digit from 0 to 1
  1841. * @param playSound if true, plays ItemSound.quest_itemget when items are given and ItemSound.quest_middle when the limit is reached
  1842. * @return {@code true} if limit > 0 and the limit was reached or if limit <= 0 and items were given; {@code false} in all other cases
  1843. */
  1844. public static boolean giveItemRandomly(L2PcInstance player, L2Npc npc, int itemId, long minAmount, long maxAmount, long limit, double dropChance, boolean playSound)
  1845. {
  1846. final long currentCount = getQuestItemsCount(player, itemId);
  1847. if ((limit > 0) && (currentCount >= limit))
  1848. {
  1849. return true;
  1850. }
  1851. minAmount *= Config.RATE_QUEST_DROP;
  1852. maxAmount *= Config.RATE_QUEST_DROP;
  1853. dropChance *= Config.RATE_QUEST_DROP; // TODO separate configs for rate and amount
  1854. if ((npc != null) && Config.L2JMOD_CHAMPION_ENABLE && npc.isChampion())
  1855. {
  1856. dropChance *= Config.L2JMOD_CHAMPION_REWARDS;
  1857. if ((itemId == Inventory.ADENA_ID) || (itemId == Inventory.ANCIENT_ADENA_ID))
  1858. {
  1859. minAmount *= Config.L2JMOD_CHAMPION_ADENAS_REWARDS;
  1860. maxAmount *= Config.L2JMOD_CHAMPION_ADENAS_REWARDS;
  1861. }
  1862. else
  1863. {
  1864. minAmount *= Config.L2JMOD_CHAMPION_REWARDS;
  1865. maxAmount *= Config.L2JMOD_CHAMPION_REWARDS;
  1866. }
  1867. }
  1868. long amountToGive = ((minAmount == maxAmount) ? minAmount : Rnd.get(minAmount, maxAmount));
  1869. final double random = Rnd.nextDouble();
  1870. // Inventory slot check (almost useless for non-stacking items)
  1871. if ((dropChance >= random) && (amountToGive > 0) && player.getInventory().validateCapacityByItemId(itemId))
  1872. {
  1873. if ((limit > 0) && ((currentCount + amountToGive) > limit))
  1874. {
  1875. amountToGive = limit - currentCount;
  1876. }
  1877. // Give the item to player
  1878. L2ItemInstance item = player.addItem("Quest", itemId, amountToGive, npc, true);
  1879. if (item != null)
  1880. {
  1881. // limit reached (if there is no limit, this block doesn't execute)
  1882. if ((currentCount + amountToGive) == limit)
  1883. {
  1884. if (playSound)
  1885. {
  1886. playSound(player, QuestSound.ITEMSOUND_QUEST_MIDDLE);
  1887. }
  1888. return true;
  1889. }
  1890. if (playSound)
  1891. {
  1892. playSound(player, QuestSound.ITEMSOUND_QUEST_ITEMGET);
  1893. }
  1894. // if there is no limit, return true every time an item is given
  1895. if (limit <= 0)
  1896. {
  1897. return true;
  1898. }
  1899. }
  1900. }
  1901. return false;
  1902. }
  1903. /**
  1904. * Take an amount of a specified item from player's inventory.
  1905. * @param player the player whose item to take
  1906. * @param itemId the ID of the item to take
  1907. * @param amount the amount to take
  1908. * @return {@code true} if any items were taken, {@code false} otherwise
  1909. */
  1910. public static boolean takeItems(L2PcInstance player, int itemId, long amount)
  1911. {
  1912. // Get object item from player's inventory list
  1913. final L2ItemInstance item = player.getInventory().getItemByItemId(itemId);
  1914. if (item == null)
  1915. {
  1916. return false;
  1917. }
  1918. // Tests on count value in order not to have negative value
  1919. if ((amount < 0) || (amount > item.getCount()))
  1920. {
  1921. amount = item.getCount();
  1922. }
  1923. // Destroy the quantity of items wanted
  1924. if (item.isEquipped())
  1925. {
  1926. final L2ItemInstance[] unequiped = player.getInventory().unEquipItemInBodySlotAndRecord(item.getItem().getBodyPart());
  1927. InventoryUpdate iu = new InventoryUpdate();
  1928. for (L2ItemInstance itm : unequiped)
  1929. {
  1930. iu.addModifiedItem(itm);
  1931. }
  1932. player.sendPacket(iu);
  1933. player.broadcastUserInfo();
  1934. }
  1935. return player.destroyItemByItemId("Quest", itemId, amount, player, true);
  1936. }
  1937. /**
  1938. * Take a set amount of a specified item from player's inventory.
  1939. * @param player the player whose item to take
  1940. * @param holder the {@link ItemHolder} object containing the ID and count of the item to take
  1941. * @return {@code true} if the item was taken, {@code false} otherwise
  1942. */
  1943. protected static boolean takeItem(L2PcInstance player, ItemHolder holder)
  1944. {
  1945. if (holder == null)
  1946. {
  1947. return false;
  1948. }
  1949. return takeItems(player, holder.getId(), holder.getCount());
  1950. }
  1951. /**
  1952. * Take a set amount of all specified items from player's inventory.
  1953. * @param player the player whose items to take
  1954. * @param itemList the list of {@link ItemHolder} objects containing the IDs and counts of the items to take
  1955. * @return {@code true} if all items were taken, {@code false} otherwise
  1956. */
  1957. protected static boolean takeAllItems(L2PcInstance player, ItemHolder... itemList)
  1958. {
  1959. if ((itemList == null) || (itemList.length == 0))
  1960. {
  1961. return false;
  1962. }
  1963. // first check if the player has all items to avoid taking half the items from the list
  1964. if (!hasAllItems(player, true, itemList))
  1965. {
  1966. return false;
  1967. }
  1968. for (ItemHolder item : itemList)
  1969. {
  1970. // this should never be false, but just in case
  1971. if (!takeItem(player, item))
  1972. {
  1973. return false;
  1974. }
  1975. }
  1976. return true;
  1977. }
  1978. /**
  1979. * Take an amount of all specified items from player's inventory.
  1980. * @param player the player whose items to take
  1981. * @param amount the amount to take of each item
  1982. * @param itemIds a list or an array of IDs of the items to take
  1983. * @return {@code true} if all items were taken, {@code false} otherwise
  1984. */
  1985. public static boolean takeItems(L2PcInstance player, int amount, int... itemIds)
  1986. {
  1987. boolean check = true;
  1988. if (itemIds != null)
  1989. {
  1990. for (int item : itemIds)
  1991. {
  1992. check &= takeItems(player, item, amount);
  1993. }
  1994. }
  1995. return check;
  1996. }
  1997. /**
  1998. * Send a packet in order to play a sound to the player.
  1999. * @param player the player whom to send the packet
  2000. * @param sound the name of the sound to play
  2001. */
  2002. public static void playSound(L2PcInstance player, String sound)
  2003. {
  2004. player.sendPacket(QuestSound.getSound(sound));
  2005. }
  2006. /**
  2007. * Send a packet in order to play a sound to the player.
  2008. * @param player the player whom to send the packet
  2009. * @param sound the {@link QuestSound} object of the sound to play
  2010. */
  2011. public static void playSound(L2PcInstance player, QuestSound sound)
  2012. {
  2013. player.sendPacket(sound.getPacket());
  2014. }
  2015. /**
  2016. * Add EXP and SP as quest reward.
  2017. * @param player the player whom to reward with the EXP/SP
  2018. * @param exp the amount of EXP to give to the player
  2019. * @param sp the amount of SP to give to the player
  2020. */
  2021. public static void addExpAndSp(L2PcInstance player, long exp, int sp)
  2022. {
  2023. player.addExpAndSp((long) 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));
  2024. }
  2025. /**
  2026. * Get a random integer from 0 (inclusive) to {@code max} (exclusive).<br>
  2027. * Use this method instead of importing {@link com.l2jserver.util.Rnd} utility.
  2028. * @param max the maximum value for randomization
  2029. * @return a random integer number from 0 to {@code max - 1}
  2030. */
  2031. public static int getRandom(int max)
  2032. {
  2033. return Rnd.get(max);
  2034. }
  2035. /**
  2036. * Get a random integer from {@code min} (inclusive) to {@code max} (inclusive).<br>
  2037. * Use this method instead of importing {@link com.l2jserver.util.Rnd} utility.
  2038. * @param min the minimum value for randomization
  2039. * @param max the maximum value for randomization
  2040. * @return a random integer number from {@code min} to {@code max}
  2041. */
  2042. public static int getRandom(int min, int max)
  2043. {
  2044. return Rnd.get(min, max);
  2045. }
  2046. /**
  2047. * Get a random boolean.<br>
  2048. * Use this method instead of importing {@link com.l2jserver.util.Rnd} utility.
  2049. * @return {@code true} or {@code false} randomly
  2050. */
  2051. public static boolean getRandomBoolean()
  2052. {
  2053. return Rnd.nextBoolean();
  2054. }
  2055. /**
  2056. * Get the ID of the item equipped in the specified inventory slot of the player.
  2057. * @param player the player whose inventory to check
  2058. * @param slot the location in the player's inventory to check
  2059. * @return the ID of the item equipped in the specified inventory slot or 0 if the slot is empty or item is {@code null}.
  2060. */
  2061. public static int getItemEquipped(L2PcInstance player, int slot)
  2062. {
  2063. return player.getInventory().getPaperdollItemId(slot);
  2064. }
  2065. /**
  2066. * @return the number of ticks from the {@link com.l2jserver.gameserver.GameTimeController}.
  2067. */
  2068. public static int getGameTicks()
  2069. {
  2070. return GameTimeController.getInstance().getGameTicks();
  2071. }
  2072. /**
  2073. * Execute a procedure for each player depending on the parameters.
  2074. * @param player the player on which the procedure will be executed
  2075. * @param npc the related NPC
  2076. * @param isSummon {@code true} if the event that called this method was originated by the player's summon, {@code false} otherwise
  2077. * @param includeParty if {@code true}, #actionForEachPlayer(L2PcInstance, L2Npc, boolean) will be called with the player's party members
  2078. * @param includeCommandChannel if {@code true}, {@link #actionForEachPlayer(L2PcInstance, L2Npc, boolean)} will be called with the player's command channel members
  2079. * @see #actionForEachPlayer(L2PcInstance, L2Npc, boolean)
  2080. */
  2081. public final void executeForEachPlayer(L2PcInstance player, final L2Npc npc, final boolean isSummon, boolean includeParty, boolean includeCommandChannel)
  2082. {
  2083. if ((includeParty || includeCommandChannel) && player.isInParty())
  2084. {
  2085. if (includeCommandChannel && player.getParty().isInCommandChannel())
  2086. {
  2087. player.getParty().getCommandChannel().forEachMember(member ->
  2088. {
  2089. actionForEachPlayer(member, npc, isSummon);
  2090. return true;
  2091. });
  2092. }
  2093. else if (includeParty)
  2094. {
  2095. player.getParty().forEachMember(member ->
  2096. {
  2097. actionForEachPlayer(member, npc, isSummon);
  2098. return true;
  2099. });
  2100. }
  2101. }
  2102. else
  2103. {
  2104. actionForEachPlayer(player, npc, isSummon);
  2105. }
  2106. }
  2107. /**
  2108. * Overridable method called from {@link #executeForEachPlayer(L2PcInstance, L2Npc, boolean, boolean, boolean)}
  2109. * @param player the player on which the action will be run
  2110. * @param npc the NPC related to this action
  2111. * @param isSummon {@code true} if the event that called this method was originated by the player's summon
  2112. */
  2113. public void actionForEachPlayer(L2PcInstance player, L2Npc npc, boolean isSummon)
  2114. {
  2115. // To be overridden in quest scripts.
  2116. }
  2117. /**
  2118. * Open a door if it is present on the instance and its not open.
  2119. * @param doorId the ID of the door to open
  2120. * @param instanceId the ID of the instance the door is in (0 if the door is not not inside an instance)
  2121. */
  2122. public void openDoor(int doorId, int instanceId)
  2123. {
  2124. final L2DoorInstance door = getDoor(doorId, instanceId);
  2125. if (door == null)
  2126. {
  2127. _log.log(Level.WARNING, getClass().getSimpleName() + ": called openDoor(" + doorId + ", " + instanceId + "); but door wasnt found!", new NullPointerException());
  2128. }
  2129. else if (!door.getOpen())
  2130. {
  2131. door.openMe();
  2132. }
  2133. }
  2134. /**
  2135. * Close a door if it is present in a specified the instance and its open.
  2136. * @param doorId the ID of the door to close
  2137. * @param instanceId the ID of the instance the door is in (0 if the door is not not inside an instance)
  2138. */
  2139. public void closeDoor(int doorId, int instanceId)
  2140. {
  2141. final L2DoorInstance door = getDoor(doorId, instanceId);
  2142. if (door == null)
  2143. {
  2144. _log.log(Level.WARNING, getClass().getSimpleName() + ": called closeDoor(" + doorId + ", " + instanceId + "); but door wasnt found!", new NullPointerException());
  2145. }
  2146. else if (door.getOpen())
  2147. {
  2148. door.closeMe();
  2149. }
  2150. }
  2151. /**
  2152. * Retrieve a door from an instance or the real world.
  2153. * @param doorId the ID of the door to get
  2154. * @param instanceId the ID of the instance the door is in (0 if the door is not not inside an instance)
  2155. * @return the found door or {@code null} if no door with that ID and instance ID was found
  2156. */
  2157. public L2DoorInstance getDoor(int doorId, int instanceId)
  2158. {
  2159. L2DoorInstance door = null;
  2160. if (instanceId <= 0)
  2161. {
  2162. door = DoorTable.getInstance().getDoor(doorId);
  2163. }
  2164. else
  2165. {
  2166. final Instance inst = InstanceManager.getInstance().getInstance(instanceId);
  2167. if (inst != null)
  2168. {
  2169. door = inst.getDoor(doorId);
  2170. }
  2171. }
  2172. return door;
  2173. }
  2174. /**
  2175. * Teleport a player into/out of an instance.
  2176. * @param player the player to teleport
  2177. * @param loc the {@link Location} object containing the destination coordinates
  2178. * @param instanceId the ID of the instance to teleport the player to (0 to teleport out of an instance)
  2179. */
  2180. public void teleportPlayer(L2PcInstance player, Location loc, int instanceId)
  2181. {
  2182. teleportPlayer(player, loc, instanceId, true);
  2183. }
  2184. /**
  2185. * Teleport a player into/out of an instance.
  2186. * @param player the player to teleport
  2187. * @param loc the {@link Location} object containing the destination coordinates
  2188. * @param instanceId the ID of the instance to teleport the player to (0 to teleport out of an instance)
  2189. * @param allowRandomOffset if {@code true}, will randomize the teleport coordinates by +/-Config.MAX_OFFSET_ON_TELEPORT
  2190. */
  2191. public void teleportPlayer(L2PcInstance player, Location loc, int instanceId, boolean allowRandomOffset)
  2192. {
  2193. loc.setInstanceId(instanceId);
  2194. player.teleToLocation(loc, allowRandomOffset);
  2195. }
  2196. /**
  2197. * Sends the special camera packet to the player.
  2198. * @param player the player
  2199. * @param creature the watched creature
  2200. * @param force
  2201. * @param angle1
  2202. * @param angle2
  2203. * @param time
  2204. * @param range
  2205. * @param duration
  2206. * @param relYaw
  2207. * @param relPitch
  2208. * @param isWide
  2209. * @param relAngle
  2210. */
  2211. public static final void specialCamera(L2PcInstance player, L2Character creature, int force, int angle1, int angle2, int time, int range, int duration, int relYaw, int relPitch, int isWide, int relAngle)
  2212. {
  2213. player.sendPacket(new SpecialCamera(creature, force, angle1, angle2, time, range, duration, relYaw, relPitch, isWide, relAngle));
  2214. }
  2215. /**
  2216. * Sends the special camera packet to the player.
  2217. * @param player
  2218. * @param creature
  2219. * @param force
  2220. * @param angle1
  2221. * @param angle2
  2222. * @param time
  2223. * @param duration
  2224. * @param relYaw
  2225. * @param relPitch
  2226. * @param isWide
  2227. * @param relAngle
  2228. */
  2229. public static final void specialCameraEx(L2PcInstance player, L2Character creature, int force, int angle1, int angle2, int time, int duration, int relYaw, int relPitch, int isWide, int relAngle)
  2230. {
  2231. player.sendPacket(new SpecialCamera(creature, player, force, angle1, angle2, time, duration, relYaw, relPitch, isWide, relAngle));
  2232. }
  2233. /**
  2234. * Sends the special camera packet to the player.
  2235. * @param player
  2236. * @param creature
  2237. * @param force
  2238. * @param angle1
  2239. * @param angle2
  2240. * @param time
  2241. * @param range
  2242. * @param duration
  2243. * @param relYaw
  2244. * @param relPitch
  2245. * @param isWide
  2246. * @param relAngle
  2247. * @param unk
  2248. */
  2249. public static final void specialCamera3(L2PcInstance player, L2Character creature, int force, int angle1, int angle2, int time, int range, int duration, int relYaw, int relPitch, int isWide, int relAngle, int unk)
  2250. {
  2251. player.sendPacket(new SpecialCamera(creature, force, angle1, angle2, time, range, duration, relYaw, relPitch, isWide, relAngle, unk));
  2252. }
  2253. }