AbstractScript.java 94 KB

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