AutoSpawnHandler.java 20 KB

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