AbstractScript.java 91 KB

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