AbstractScript.java 94 KB

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