AutoSpawnHandler.java 22 KB

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