AbstractScript.java 98 KB

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