AutoSpawnHandler.java 21 KB

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