AutoSpawnHandler.java 20 KB

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