AutoSpawnHandler.java 21 KB

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