AutoSpawnHandler.java 20 KB

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