AutoSpawnHandler.java 20 KB

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