AbstractScript.java 93 KB

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