AutoSpawnHandler.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.Statement;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.concurrent.ScheduledFuture;
  27. import java.util.concurrent.TimeUnit;
  28. import java.util.logging.Level;
  29. import java.util.logging.Logger;
  30. import javolution.util.FastList;
  31. import javolution.util.FastMap;
  32. import com.l2jserver.L2DatabaseFactory;
  33. import com.l2jserver.gameserver.Announcements;
  34. import com.l2jserver.gameserver.ThreadPoolManager;
  35. import com.l2jserver.gameserver.datatables.NpcTable;
  36. import com.l2jserver.gameserver.datatables.SpawnTable;
  37. import com.l2jserver.gameserver.idfactory.IdFactory;
  38. import com.l2jserver.gameserver.instancemanager.MapRegionManager;
  39. import com.l2jserver.gameserver.model.actor.L2Npc;
  40. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  41. import com.l2jserver.gameserver.model.interfaces.IIdentifiable;
  42. import com.l2jserver.util.Rnd;
  43. /**
  44. * Auto Spawn handler.<br>
  45. * Allows spawning of a NPC object based on a timer.<br>
  46. * (From the official idea used for the Merchant and Blacksmith of Mammon)<br>
  47. * General Usage: - Call registerSpawn() with the parameters listed below.<br>
  48. * int npcId int[][] spawnPoints or specify NULL to add points later.<br>
  49. * int initialDelay (If < 0 = default value) int respawnDelay (If < 0 = default value)<br>
  50. * int despawnDelay (If < 0 = default value or if = 0, function disabled)<br>
  51. * spawnPoints is a standard two-dimensional int array containing X,Y and Z coordinates.<br>
  52. * The default respawn/despawn delays are currently every hour (as for Mammon on official servers).<br>
  53. * The resulting AutoSpawnInstance object represents the newly added spawn index.<br>
  54. * The internal methods of this object can be used to adjust random spawning, for instance a call to setRandomSpawn(1, true); would set the spawn at index 1 to be randomly rather than sequentially-based.<br>
  55. * Also they can be used to specify the number of NPC instances to spawn using setSpawnCount(), and broadcast a message to all users using setBroadcast().<br>
  56. * Random Spawning = OFF by default Broadcasting = OFF by default
  57. * @author Tempy
  58. */
  59. public class AutoSpawnHandler
  60. {
  61. protected static final Logger _log = Logger.getLogger(AutoSpawnHandler.class.getName());
  62. private static final int DEFAULT_INITIAL_SPAWN = 30000; // 30 seconds after registration
  63. private static final int DEFAULT_RESPAWN = 3600000; // 1 hour in millisecs
  64. private static final int DEFAULT_DESPAWN = 3600000; // 1 hour in millisecs
  65. protected Map<Integer, AutoSpawnInstance> _registeredSpawns;
  66. protected Map<Integer, ScheduledFuture<?>> _runningSpawns;
  67. protected boolean _activeState = true;
  68. protected AutoSpawnHandler()
  69. {
  70. _registeredSpawns = new FastMap<>();
  71. _runningSpawns = new FastMap<>();
  72. restoreSpawnData();
  73. }
  74. public static AutoSpawnHandler getInstance()
  75. {
  76. return SingletonHolder._instance;
  77. }
  78. public final int size()
  79. {
  80. return _registeredSpawns.size();
  81. }
  82. public void reload()
  83. {
  84. // stop all timers
  85. for (ScheduledFuture<?> sf : _runningSpawns.values())
  86. {
  87. if (sf != null)
  88. {
  89. sf.cancel(true);
  90. }
  91. }
  92. // unregister all registered spawns
  93. for (AutoSpawnInstance asi : _registeredSpawns.values())
  94. {
  95. if (asi != null)
  96. {
  97. this.removeSpawn(asi);
  98. }
  99. }
  100. // create clean list
  101. _registeredSpawns = new FastMap<>();
  102. _runningSpawns = new FastMap<>();
  103. // load
  104. restoreSpawnData();
  105. }
  106. private void restoreSpawnData()
  107. {
  108. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  109. Statement s = con.createStatement();
  110. ResultSet rs = s.executeQuery("SELECT * FROM random_spawn ORDER BY groupId ASC");
  111. PreparedStatement ps = con.prepareStatement("SELECT * FROM random_spawn_loc WHERE groupId=?"))
  112. {
  113. // Restore spawn group data, then the location data.
  114. while (rs.next())
  115. {
  116. // Register random spawn group, set various options on the
  117. // created spawn instance.
  118. AutoSpawnInstance spawnInst = registerSpawn(rs.getInt("npcId"), rs.getInt("initialDelay"), rs.getInt("respawnDelay"), rs.getInt("despawnDelay"));
  119. spawnInst.setSpawnCount(rs.getInt("count"));
  120. spawnInst.setBroadcast(rs.getBoolean("broadcastSpawn"));
  121. spawnInst.setRandomSpawn(rs.getBoolean("randomSpawn"));
  122. // Restore the spawn locations for this spawn group/instance.
  123. ps.setInt(1, rs.getInt("groupId"));
  124. try (ResultSet rs2 = ps.executeQuery())
  125. {
  126. ps.clearParameters();
  127. while (rs2.next())
  128. {
  129. // Add each location to the spawn group/instance.
  130. spawnInst.addSpawnLocation(rs2.getInt("x"), rs2.getInt("y"), rs2.getInt("z"), rs2.getInt("heading"));
  131. }
  132. }
  133. }
  134. }
  135. catch (Exception e)
  136. {
  137. _log.log(Level.WARNING, "AutoSpawnHandler: Could not restore spawn data: " + e.getMessage(), e);
  138. }
  139. }
  140. /**
  141. * Registers a spawn with the given parameters with the spawner, and marks it as active.<br>
  142. * Returns a AutoSpawnInstance containing info about the spawn.
  143. * @param npcId
  144. * @param spawnPoints
  145. * @param initialDelay (If < 0 = default value)
  146. * @param respawnDelay (If < 0 = default value)
  147. * @param despawnDelay (If < 0 = default value or if = 0, function disabled)
  148. * @return AutoSpawnInstance spawnInst
  149. */
  150. public AutoSpawnInstance registerSpawn(int npcId, int[][] spawnPoints, int initialDelay, int respawnDelay, int despawnDelay)
  151. {
  152. if (initialDelay < 0)
  153. {
  154. initialDelay = DEFAULT_INITIAL_SPAWN;
  155. }
  156. if (respawnDelay < 0)
  157. {
  158. respawnDelay = DEFAULT_RESPAWN;
  159. }
  160. if (despawnDelay < 0)
  161. {
  162. despawnDelay = DEFAULT_DESPAWN;
  163. }
  164. AutoSpawnInstance newSpawn = new AutoSpawnInstance(npcId, initialDelay, respawnDelay, despawnDelay);
  165. if (spawnPoints != null)
  166. {
  167. for (int[] spawnPoint : spawnPoints)
  168. {
  169. newSpawn.addSpawnLocation(spawnPoint);
  170. }
  171. }
  172. int newId = IdFactory.getInstance().getNextId();
  173. newSpawn._objectId = newId;
  174. _registeredSpawns.put(newId, newSpawn);
  175. setSpawnActive(newSpawn, true);
  176. return newSpawn;
  177. }
  178. /**
  179. * Registers a spawn with the given parameters with the spawner, and marks it as active.<br>
  180. * Returns a AutoSpawnInstance containing info about the spawn.<br>
  181. * <B>Warning:</B> Spawn locations must be specified separately using addSpawnLocation().
  182. * @param npcId
  183. * @param initialDelay (If < 0 = default value)
  184. * @param respawnDelay (If < 0 = default value)
  185. * @param despawnDelay (If < 0 = default value or if = 0, function disabled)
  186. * @return AutoSpawnInstance spawnInst
  187. */
  188. public AutoSpawnInstance registerSpawn(int npcId, int initialDelay, int respawnDelay, int despawnDelay)
  189. {
  190. return registerSpawn(npcId, null, initialDelay, respawnDelay, despawnDelay);
  191. }
  192. /**
  193. * Remove a registered spawn from the list, specified by the given spawn instance.
  194. * @param spawnInst
  195. * @return boolean removedSuccessfully
  196. */
  197. public boolean removeSpawn(AutoSpawnInstance spawnInst)
  198. {
  199. if (!isSpawnRegistered(spawnInst))
  200. {
  201. return false;
  202. }
  203. try
  204. {
  205. // Try to remove from the list of registered spawns if it exists.
  206. _registeredSpawns.remove(spawnInst.getId());
  207. // Cancel the currently associated running scheduled task.
  208. ScheduledFuture<?> respawnTask = _runningSpawns.remove(spawnInst._objectId);
  209. respawnTask.cancel(false);
  210. }
  211. catch (Exception e)
  212. {
  213. _log.log(Level.WARNING, "AutoSpawnHandler: Could not auto spawn for NPC ID " + spawnInst._npcId + " (Object ID = " + spawnInst._objectId + "): " + e.getMessage(), e);
  214. return false;
  215. }
  216. return true;
  217. }
  218. /**
  219. * Remove a registered spawn from the list, specified by the given spawn object ID.
  220. * @param objectId
  221. */
  222. public void removeSpawn(int objectId)
  223. {
  224. removeSpawn(_registeredSpawns.get(objectId));
  225. }
  226. /**
  227. * Sets the active state of the specified spawn.
  228. * @param spawnInst
  229. * @param isActive
  230. */
  231. public void setSpawnActive(AutoSpawnInstance spawnInst, boolean isActive)
  232. {
  233. if (spawnInst == null)
  234. {
  235. return;
  236. }
  237. int objectId = spawnInst._objectId;
  238. if (isSpawnRegistered(objectId))
  239. {
  240. ScheduledFuture<?> spawnTask = null;
  241. if (isActive)
  242. {
  243. AutoSpawner rs = new AutoSpawner(objectId);
  244. if (spawnInst._desDelay > 0)
  245. {
  246. spawnTask = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(rs, spawnInst._initDelay, spawnInst._resDelay);
  247. }
  248. else
  249. {
  250. spawnTask = ThreadPoolManager.getInstance().scheduleEffect(rs, spawnInst._initDelay);
  251. }
  252. _runningSpawns.put(objectId, spawnTask);
  253. }
  254. else
  255. {
  256. AutoDespawner rd = new AutoDespawner(objectId);
  257. spawnTask = _runningSpawns.remove(objectId);
  258. if (spawnTask != null)
  259. {
  260. spawnTask.cancel(false);
  261. }
  262. ThreadPoolManager.getInstance().scheduleEffect(rd, 0);
  263. }
  264. spawnInst.setSpawnActive(isActive);
  265. }
  266. }
  267. /**
  268. * Sets the active state of all auto spawn instances to that specified, and cancels the scheduled spawn task if necessary.
  269. * @param isActive
  270. */
  271. public void setAllActive(boolean isActive)
  272. {
  273. if (_activeState == isActive)
  274. {
  275. return;
  276. }
  277. for (AutoSpawnInstance spawnInst : _registeredSpawns.values())
  278. {
  279. setSpawnActive(spawnInst, isActive);
  280. }
  281. _activeState = isActive;
  282. }
  283. /**
  284. * Returns the number of milliseconds until the next occurrence of the given spawn.
  285. * @param spawnInst
  286. * @return
  287. */
  288. public final long getTimeToNextSpawn(AutoSpawnInstance spawnInst)
  289. {
  290. int objectId = spawnInst.getObjectId();
  291. if (!isSpawnRegistered(objectId))
  292. {
  293. return -1;
  294. }
  295. return _runningSpawns.get(objectId).getDelay(TimeUnit.MILLISECONDS);
  296. }
  297. /**
  298. * Attempts to return the AutoSpawnInstance associated with the given NPC or Object ID type.<br>
  299. * Note: If isObjectId == false, returns first instance for the specified NPC ID.
  300. * @param id
  301. * @param isObjectId
  302. * @return AutoSpawnInstance spawnInst
  303. */
  304. public final AutoSpawnInstance getAutoSpawnInstance(int id, boolean isObjectId)
  305. {
  306. if (isObjectId)
  307. {
  308. if (isSpawnRegistered(id))
  309. {
  310. return _registeredSpawns.get(id);
  311. }
  312. }
  313. else
  314. {
  315. for (AutoSpawnInstance spawnInst : _registeredSpawns.values())
  316. {
  317. if (spawnInst.getId() == id)
  318. {
  319. return spawnInst;
  320. }
  321. }
  322. }
  323. return null;
  324. }
  325. public Map<Integer, AutoSpawnInstance> getAutoSpawnInstances(int npcId)
  326. {
  327. Map<Integer, AutoSpawnInstance> spawnInstList = new FastMap<>();
  328. for (AutoSpawnInstance spawnInst : _registeredSpawns.values())
  329. {
  330. if (spawnInst.getId() == npcId)
  331. {
  332. spawnInstList.put(spawnInst.getObjectId(), spawnInst);
  333. }
  334. }
  335. return spawnInstList;
  336. }
  337. /**
  338. * Tests if the specified object ID is assigned to an auto spawn.
  339. * @param objectId
  340. * @return isAssigned
  341. */
  342. public final boolean isSpawnRegistered(int objectId)
  343. {
  344. return _registeredSpawns.containsKey(objectId);
  345. }
  346. /**
  347. * Tests if the specified spawn instance is assigned to an auto spawn.
  348. * @param spawnInst
  349. * @return boolean isAssigned
  350. */
  351. public final boolean isSpawnRegistered(AutoSpawnInstance spawnInst)
  352. {
  353. return _registeredSpawns.containsValue(spawnInst);
  354. }
  355. /**
  356. * AutoSpawner class<br>
  357. * This handles the main spawn task for an auto spawn instance, and initializes a despawner if required.
  358. * @author Tempy
  359. */
  360. private class AutoSpawner implements Runnable
  361. {
  362. private final int _objectId;
  363. protected AutoSpawner(int objectId)
  364. {
  365. _objectId = objectId;
  366. }
  367. @Override
  368. public void run()
  369. {
  370. try
  371. {
  372. // Retrieve the required spawn instance for this spawn task.
  373. AutoSpawnInstance spawnInst = _registeredSpawns.get(_objectId);
  374. // If the spawn is not scheduled to be active, cancel the spawn
  375. // task.
  376. if (!spawnInst.isSpawnActive())
  377. {
  378. return;
  379. }
  380. Location[] locationList = spawnInst.getLocationList();
  381. // If there are no set co-ordinates, cancel the spawn task.
  382. if (locationList.length == 0)
  383. {
  384. _log.info("AutoSpawnHandler: No location co-ords specified for spawn instance (Object ID = " + _objectId + ").");
  385. return;
  386. }
  387. int locationCount = locationList.length;
  388. int locationIndex = Rnd.nextInt(locationCount);
  389. // If random spawning is disabled, the spawn at the next set of co-ordinates after the last.
  390. // If the index is greater than the number of possible spawns, reset the counter to zero.
  391. if (!spawnInst.isRandomSpawn())
  392. {
  393. locationIndex = spawnInst._lastLocIndex + 1;
  394. if (locationIndex == locationCount)
  395. {
  396. locationIndex = 0;
  397. }
  398. spawnInst._lastLocIndex = locationIndex;
  399. }
  400. // Set the X, Y and Z co-ordinates, where this spawn will take place.
  401. final int x = locationList[locationIndex].getX();
  402. final int y = locationList[locationIndex].getY();
  403. final int z = locationList[locationIndex].getZ();
  404. final int heading = locationList[locationIndex].getHeading();
  405. // Fetch the template for this NPC ID and create a new spawn.
  406. L2NpcTemplate npcTemp = NpcTable.getInstance().getTemplate(spawnInst.getId());
  407. if (npcTemp == null)
  408. {
  409. _log.warning("Couldnt find NPC id" + spawnInst.getId() + " Try to update your DP");
  410. return;
  411. }
  412. L2Spawn newSpawn = new L2Spawn(npcTemp);
  413. newSpawn.setX(x);
  414. newSpawn.setY(y);
  415. newSpawn.setZ(z);
  416. if (heading != -1)
  417. {
  418. newSpawn.setHeading(heading);
  419. }
  420. newSpawn.setAmount(spawnInst.getSpawnCount());
  421. if (spawnInst._desDelay == 0)
  422. {
  423. newSpawn.setRespawnDelay(spawnInst._resDelay);
  424. }
  425. // Add the new spawn information to the spawn table, but do not store it.
  426. SpawnTable.getInstance().addNewSpawn(newSpawn, false);
  427. L2Npc npcInst = null;
  428. if (spawnInst._spawnCount == 1)
  429. {
  430. npcInst = newSpawn.doSpawn();
  431. npcInst.setXYZ(npcInst.getX(), npcInst.getY(), npcInst.getZ());
  432. spawnInst.addNpcInstance(npcInst);
  433. }
  434. else
  435. {
  436. for (int i = 0; i < spawnInst._spawnCount; i++)
  437. {
  438. npcInst = newSpawn.doSpawn();
  439. // To prevent spawning of more than one NPC in the exact same spot, move it slightly by a small random offset.
  440. npcInst.setXYZ(npcInst.getX() + Rnd.nextInt(50), npcInst.getY() + Rnd.nextInt(50), npcInst.getZ());
  441. // Add the NPC instance to the list of managed instances.
  442. spawnInst.addNpcInstance(npcInst);
  443. }
  444. }
  445. String nearestTown = MapRegionManager.getInstance().getClosestTownName(npcInst);
  446. // Announce to all players that the spawn has taken place, with the nearest town location.
  447. if (spawnInst.isBroadcasting() && (npcInst != null))
  448. {
  449. Announcements.getInstance().announceToAll("The " + npcInst.getName() + " has spawned near " + nearestTown + "!");
  450. }
  451. // If there is no despawn time, do not create a despawn task.
  452. if (spawnInst.getDespawnDelay() > 0)
  453. {
  454. AutoDespawner rd = new AutoDespawner(_objectId);
  455. ThreadPoolManager.getInstance().scheduleAi(rd, spawnInst.getDespawnDelay() - 1000);
  456. }
  457. }
  458. catch (Exception e)
  459. {
  460. _log.log(Level.WARNING, "AutoSpawnHandler: An error occurred while initializing spawn instance (Object ID = " + _objectId + "): " + e.getMessage(), e);
  461. }
  462. }
  463. }
  464. /**
  465. * AutoDespawner Class<br>
  466. * Simply used as a secondary class for despawning an auto spawn instance.
  467. * @author Tempy
  468. */
  469. private class AutoDespawner implements Runnable
  470. {
  471. private final int _objectId;
  472. protected AutoDespawner(int objectId)
  473. {
  474. _objectId = objectId;
  475. }
  476. @Override
  477. public void run()
  478. {
  479. try
  480. {
  481. AutoSpawnInstance spawnInst = _registeredSpawns.get(_objectId);
  482. if (spawnInst == null)
  483. {
  484. _log.info("AutoSpawnHandler: No spawn registered for object ID = " + _objectId + ".");
  485. return;
  486. }
  487. for (L2Npc npcInst : spawnInst.getNPCInstanceList())
  488. {
  489. if (npcInst == null)
  490. {
  491. continue;
  492. }
  493. npcInst.deleteMe();
  494. SpawnTable.getInstance().deleteSpawn(npcInst.getSpawn(), false);
  495. spawnInst.removeNpcInstance(npcInst);
  496. }
  497. }
  498. catch (Exception e)
  499. {
  500. _log.log(Level.WARNING, "AutoSpawnHandler: An error occurred while despawning spawn (Object ID = " + _objectId + "): " + e.getMessage(), e);
  501. }
  502. }
  503. }
  504. /**
  505. * AutoSpawnInstance Class<br>
  506. * Stores information about a registered auto spawn.
  507. * @author Tempy
  508. */
  509. public static class AutoSpawnInstance implements IIdentifiable
  510. {
  511. protected int _objectId;
  512. protected int _spawnIndex;
  513. protected int _npcId;
  514. protected int _initDelay;
  515. protected int _resDelay;
  516. protected int _desDelay;
  517. protected int _spawnCount = 1;
  518. protected int _lastLocIndex = -1;
  519. private final List<L2Npc> _npcList = new FastList<>();
  520. private final List<Location> _locList = new FastList<>();
  521. private boolean _spawnActive;
  522. private boolean _randomSpawn = false;
  523. private boolean _broadcastAnnouncement = false;
  524. protected AutoSpawnInstance(int npcId, int initDelay, int respawnDelay, int despawnDelay)
  525. {
  526. _npcId = npcId;
  527. _initDelay = initDelay;
  528. _resDelay = respawnDelay;
  529. _desDelay = despawnDelay;
  530. }
  531. protected void setSpawnActive(boolean activeValue)
  532. {
  533. _spawnActive = activeValue;
  534. }
  535. protected boolean addNpcInstance(L2Npc npcInst)
  536. {
  537. return _npcList.add(npcInst);
  538. }
  539. protected boolean removeNpcInstance(L2Npc npcInst)
  540. {
  541. return _npcList.remove(npcInst);
  542. }
  543. public int getObjectId()
  544. {
  545. return _objectId;
  546. }
  547. public int getInitialDelay()
  548. {
  549. return _initDelay;
  550. }
  551. public int getRespawnDelay()
  552. {
  553. return _resDelay;
  554. }
  555. public int getDespawnDelay()
  556. {
  557. return _desDelay;
  558. }
  559. /**
  560. * Gets the NPC ID.
  561. * @return the NPC ID
  562. */
  563. @Override
  564. public int getId()
  565. {
  566. return _npcId;
  567. }
  568. public int getSpawnCount()
  569. {
  570. return _spawnCount;
  571. }
  572. public Location[] getLocationList()
  573. {
  574. return _locList.toArray(new Location[_locList.size()]);
  575. }
  576. public L2Npc[] getNPCInstanceList()
  577. {
  578. L2Npc[] ret;
  579. synchronized (_npcList)
  580. {
  581. ret = new L2Npc[_npcList.size()];
  582. _npcList.toArray(ret);
  583. }
  584. return ret;
  585. }
  586. public L2Spawn[] getSpawns()
  587. {
  588. List<L2Spawn> npcSpawns = new FastList<>();
  589. for (L2Npc npcInst : _npcList)
  590. {
  591. npcSpawns.add(npcInst.getSpawn());
  592. }
  593. return npcSpawns.toArray(new L2Spawn[npcSpawns.size()]);
  594. }
  595. public void setSpawnCount(int spawnCount)
  596. {
  597. _spawnCount = spawnCount;
  598. }
  599. public void setRandomSpawn(boolean randValue)
  600. {
  601. _randomSpawn = randValue;
  602. }
  603. public void setBroadcast(boolean broadcastValue)
  604. {
  605. _broadcastAnnouncement = broadcastValue;
  606. }
  607. public boolean isSpawnActive()
  608. {
  609. return _spawnActive;
  610. }
  611. public boolean isRandomSpawn()
  612. {
  613. return _randomSpawn;
  614. }
  615. public boolean isBroadcasting()
  616. {
  617. return _broadcastAnnouncement;
  618. }
  619. public boolean addSpawnLocation(int x, int y, int z, int heading)
  620. {
  621. return _locList.add(new Location(x, y, z, heading));
  622. }
  623. public boolean addSpawnLocation(int[] spawnLoc)
  624. {
  625. if (spawnLoc.length != 3)
  626. {
  627. return false;
  628. }
  629. return addSpawnLocation(spawnLoc[0], spawnLoc[1], spawnLoc[2], -1);
  630. }
  631. public Location removeSpawnLocation(int locIndex)
  632. {
  633. try
  634. {
  635. return _locList.remove(locIndex);
  636. }
  637. catch (IndexOutOfBoundsException e)
  638. {
  639. return null;
  640. }
  641. }
  642. }
  643. private static class SingletonHolder
  644. {
  645. protected static final AutoSpawnHandler _instance = new AutoSpawnHandler();
  646. }
  647. }