AutoSpawnHandler.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.Statement;
  24. import java.util.ArrayList;
  25. import java.util.LinkedList;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Queue;
  29. import java.util.concurrent.ConcurrentHashMap;
  30. import java.util.concurrent.ConcurrentLinkedQueue;
  31. import java.util.concurrent.CopyOnWriteArrayList;
  32. import java.util.concurrent.ScheduledFuture;
  33. import java.util.concurrent.TimeUnit;
  34. import java.util.logging.Level;
  35. import java.util.logging.Logger;
  36. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  37. import com.l2jserver.gameserver.ThreadPoolManager;
  38. import com.l2jserver.gameserver.datatables.SpawnTable;
  39. import com.l2jserver.gameserver.idfactory.IdFactory;
  40. import com.l2jserver.gameserver.instancemanager.MapRegionManager;
  41. import com.l2jserver.gameserver.model.actor.L2Npc;
  42. import com.l2jserver.gameserver.model.interfaces.IIdentifiable;
  43. import com.l2jserver.gameserver.util.Broadcast;
  44. import com.l2jserver.util.Rnd;
  45. /**
  46. * Auto Spawn handler.<br>
  47. * Allows spawning of a NPC object based on a timer.<br>
  48. * (From the official idea used for the Merchant and Blacksmith of Mammon)<br>
  49. * General Usage: - Call registerSpawn() with the parameters listed below.<br>
  50. * int npcId int[][] spawnPoints or specify NULL to add points later.<br>
  51. * int initialDelay (If < 0 = default value) int respawnDelay (If < 0 = default value)<br>
  52. * int despawnDelay (If < 0 = default value or if = 0, function disabled)<br>
  53. * spawnPoints is a standard two-dimensional int array containing X,Y and Z coordinates.<br>
  54. * The default respawn/despawn delays are currently every hour (as for Mammon on official servers).<br>
  55. * The resulting AutoSpawnInstance object represents the newly added spawn index.<br>
  56. * The internal methods of this object can be used to adjust random spawning, for instance a call to setRandomSpawn(1, true); would set the spawn at index 1 to be randomly rather than sequentially-based.<br>
  57. * Also they can be used to specify the number of NPC instances to spawn using setSpawnCount(), and broadcast a message to all users using setBroadcast().<br>
  58. * Random Spawning = OFF by default Broadcasting = OFF by default
  59. * @author Tempy
  60. */
  61. public class AutoSpawnHandler
  62. {
  63. protected static final Logger _log = Logger.getLogger(AutoSpawnHandler.class.getName());
  64. private static final int DEFAULT_INITIAL_SPAWN = 30000; // 30 seconds after registration
  65. private static final int DEFAULT_RESPAWN = 3600000; // 1 hour in millisecs
  66. private static final int DEFAULT_DESPAWN = 3600000; // 1 hour in millisecs
  67. protected Map<Integer, AutoSpawnInstance> _registeredSpawns = new ConcurrentHashMap<>();
  68. protected Map<Integer, ScheduledFuture<?>> _runningSpawns = new ConcurrentHashMap<>();
  69. protected boolean _activeState = true;
  70. protected AutoSpawnHandler()
  71. {
  72. restoreSpawnData();
  73. }
  74. public static AutoSpawnHandler getInstance()
  75. {
  76. return SingletonHolder._instance;
  77. }
  78. public final int size()
  79. {
  80. return _registeredSpawns.size();
  81. }
  82. public void reload()
  83. {
  84. // stop all timers
  85. for (ScheduledFuture<?> sf : _runningSpawns.values())
  86. {
  87. if (sf != null)
  88. {
  89. sf.cancel(true);
  90. }
  91. }
  92. // unregister all registered spawns
  93. for (AutoSpawnInstance asi : _registeredSpawns.values())
  94. {
  95. if (asi != null)
  96. {
  97. this.removeSpawn(asi);
  98. }
  99. }
  100. // create clean list
  101. _registeredSpawns.clear();
  102. _runningSpawns.clear();
  103. // load
  104. restoreSpawnData();
  105. }
  106. private void restoreSpawnData()
  107. {
  108. try (Connection con = ConnectionFactory.getInstance().getConnection();
  109. Statement s = con.createStatement();
  110. ResultSet rs = s.executeQuery("SELECT * FROM random_spawn ORDER BY groupId ASC");
  111. PreparedStatement ps = con.prepareStatement("SELECT * FROM random_spawn_loc WHERE groupId=?"))
  112. {
  113. // Restore spawn group data, then the location data.
  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. ps.setInt(1, rs.getInt("groupId"));
  124. try (ResultSet rs2 = ps.executeQuery())
  125. {
  126. ps.clearParameters();
  127. while (rs2.next())
  128. {
  129. // Add each location to the spawn group/instance.
  130. spawnInst.addSpawnLocation(rs2.getInt("x"), rs2.getInt("y"), rs2.getInt("z"), rs2.getInt("heading"));
  131. }
  132. }
  133. }
  134. }
  135. catch (Exception e)
  136. {
  137. _log.log(Level.WARNING, "AutoSpawnHandler: Could not restore spawn data: " + e.getMessage(), e);
  138. }
  139. }
  140. /**
  141. * Registers a spawn with the given parameters with the spawner, and marks it as active.<br>
  142. * Returns a AutoSpawnInstance containing info about the spawn.
  143. * @param npcId
  144. * @param spawnPoints
  145. * @param initialDelay (If < 0 = default value)
  146. * @param respawnDelay (If < 0 = default value)
  147. * @param despawnDelay (If < 0 = default value or if = 0, function disabled)
  148. * @return AutoSpawnInstance spawnInst
  149. */
  150. public AutoSpawnInstance registerSpawn(int npcId, int[][] spawnPoints, int initialDelay, int respawnDelay, int despawnDelay)
  151. {
  152. if (initialDelay < 0)
  153. {
  154. initialDelay = DEFAULT_INITIAL_SPAWN;
  155. }
  156. if (respawnDelay < 0)
  157. {
  158. respawnDelay = DEFAULT_RESPAWN;
  159. }
  160. if (despawnDelay < 0)
  161. {
  162. despawnDelay = DEFAULT_DESPAWN;
  163. }
  164. AutoSpawnInstance newSpawn = new AutoSpawnInstance(npcId, initialDelay, respawnDelay, despawnDelay);
  165. if (spawnPoints != null)
  166. {
  167. for (int[] spawnPoint : spawnPoints)
  168. {
  169. newSpawn.addSpawnLocation(spawnPoint);
  170. }
  171. }
  172. int newId = IdFactory.getInstance().getNextId();
  173. newSpawn._objectId = newId;
  174. _registeredSpawns.put(newId, newSpawn);
  175. setSpawnActive(newSpawn, true);
  176. return newSpawn;
  177. }
  178. /**
  179. * Registers a spawn with the given parameters with the spawner, and marks it as active.<br>
  180. * Returns a AutoSpawnInstance containing info about the spawn.<br>
  181. * <B>Warning:</B> Spawn locations must be specified separately using addSpawnLocation().
  182. * @param npcId
  183. * @param initialDelay (If < 0 = default value)
  184. * @param respawnDelay (If < 0 = default value)
  185. * @param despawnDelay (If < 0 = default value or if = 0, function disabled)
  186. * @return AutoSpawnInstance spawnInst
  187. */
  188. public AutoSpawnInstance registerSpawn(int npcId, int initialDelay, int respawnDelay, int despawnDelay)
  189. {
  190. return registerSpawn(npcId, null, initialDelay, respawnDelay, despawnDelay);
  191. }
  192. /**
  193. * Remove a registered spawn from the list, specified by the given spawn instance.
  194. * @param spawnInst
  195. * @return boolean removedSuccessfully
  196. */
  197. public boolean removeSpawn(AutoSpawnInstance spawnInst)
  198. {
  199. if (!isSpawnRegistered(spawnInst))
  200. {
  201. return false;
  202. }
  203. try
  204. {
  205. // Try to remove from the list of registered spawns if it exists.
  206. _registeredSpawns.remove(spawnInst.getId());
  207. // Cancel the currently associated running scheduled task.
  208. ScheduledFuture<?> respawnTask = _runningSpawns.remove(spawnInst._objectId);
  209. respawnTask.cancel(false);
  210. }
  211. catch (Exception e)
  212. {
  213. _log.log(Level.WARNING, "AutoSpawnHandler: Could not auto spawn for NPC ID " + spawnInst._npcId + " (Object ID = " + spawnInst._objectId + "): " + e.getMessage(), e);
  214. return false;
  215. }
  216. return true;
  217. }
  218. /**
  219. * Remove a registered spawn from the list, specified by the given spawn object ID.
  220. * @param objectId
  221. */
  222. public void removeSpawn(int objectId)
  223. {
  224. removeSpawn(_registeredSpawns.get(objectId));
  225. }
  226. /**
  227. * Sets the active state of the specified spawn.
  228. * @param spawnInst
  229. * @param isActive
  230. */
  231. public void setSpawnActive(AutoSpawnInstance spawnInst, boolean isActive)
  232. {
  233. if (spawnInst == null)
  234. {
  235. return;
  236. }
  237. int objectId = spawnInst._objectId;
  238. if (isSpawnRegistered(objectId))
  239. {
  240. ScheduledFuture<?> spawnTask = null;
  241. if (isActive)
  242. {
  243. AutoSpawner rs = new AutoSpawner(objectId);
  244. if (spawnInst._desDelay > 0)
  245. {
  246. spawnTask = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(rs, spawnInst._initDelay, spawnInst._resDelay);
  247. }
  248. else
  249. {
  250. spawnTask = ThreadPoolManager.getInstance().scheduleEffect(rs, spawnInst._initDelay);
  251. }
  252. _runningSpawns.put(objectId, spawnTask);
  253. }
  254. else
  255. {
  256. AutoDespawner rd = new AutoDespawner(objectId);
  257. spawnTask = _runningSpawns.remove(objectId);
  258. if (spawnTask != null)
  259. {
  260. spawnTask.cancel(false);
  261. }
  262. ThreadPoolManager.getInstance().scheduleEffect(rd, 0);
  263. }
  264. spawnInst.setSpawnActive(isActive);
  265. }
  266. }
  267. /**
  268. * Sets the active state of all auto spawn instances to that specified, and cancels the scheduled spawn task if necessary.
  269. * @param isActive
  270. */
  271. public void setAllActive(boolean isActive)
  272. {
  273. if (_activeState == isActive)
  274. {
  275. return;
  276. }
  277. for (AutoSpawnInstance spawnInst : _registeredSpawns.values())
  278. {
  279. setSpawnActive(spawnInst, isActive);
  280. }
  281. _activeState = isActive;
  282. }
  283. /**
  284. * Returns the number of milliseconds until the next occurrence of the given spawn.
  285. * @param spawnInst
  286. * @return
  287. */
  288. public final long getTimeToNextSpawn(AutoSpawnInstance spawnInst)
  289. {
  290. int objectId = spawnInst.getObjectId();
  291. if (!isSpawnRegistered(objectId))
  292. {
  293. return -1;
  294. }
  295. return (_runningSpawns.containsKey(objectId)) ? _runningSpawns.get(objectId).getDelay(TimeUnit.MILLISECONDS) : 0;
  296. }
  297. /**
  298. * Attempts to return the AutoSpawnInstance associated with the given NPC or Object ID type.<br>
  299. * Note: If isObjectId == false, returns first instance for the specified NPC ID.
  300. * @param id
  301. * @param isObjectId
  302. * @return AutoSpawnInstance spawnInst
  303. */
  304. public final AutoSpawnInstance getAutoSpawnInstance(int id, boolean isObjectId)
  305. {
  306. if (isObjectId)
  307. {
  308. if (isSpawnRegistered(id))
  309. {
  310. return _registeredSpawns.get(id);
  311. }
  312. }
  313. else
  314. {
  315. for (AutoSpawnInstance spawnInst : _registeredSpawns.values())
  316. {
  317. if (spawnInst.getId() == id)
  318. {
  319. return spawnInst;
  320. }
  321. }
  322. }
  323. return null;
  324. }
  325. public List<AutoSpawnInstance> getAutoSpawnInstances(int npcId)
  326. {
  327. final List<AutoSpawnInstance> result = new LinkedList<>();
  328. for (AutoSpawnInstance spawnInst : _registeredSpawns.values())
  329. {
  330. if (spawnInst.getId() == npcId)
  331. {
  332. result.add(spawnInst);
  333. }
  334. }
  335. return result;
  336. }
  337. /**
  338. * Tests if the specified object ID is assigned to an auto spawn.
  339. * @param objectId
  340. * @return isAssigned
  341. */
  342. public final boolean isSpawnRegistered(int objectId)
  343. {
  344. return _registeredSpawns.containsKey(objectId);
  345. }
  346. /**
  347. * Tests if the specified spawn instance is assigned to an auto spawn.
  348. * @param spawnInst
  349. * @return boolean isAssigned
  350. */
  351. public final boolean isSpawnRegistered(AutoSpawnInstance spawnInst)
  352. {
  353. return _registeredSpawns.containsValue(spawnInst);
  354. }
  355. /**
  356. * AutoSpawner class<br>
  357. * This handles the main spawn task for an auto spawn instance, and initializes a despawner if required.
  358. * @author Tempy
  359. */
  360. private class AutoSpawner implements Runnable
  361. {
  362. private final int _objectId;
  363. protected AutoSpawner(int objectId)
  364. {
  365. _objectId = objectId;
  366. }
  367. @Override
  368. public void run()
  369. {
  370. try
  371. {
  372. // Retrieve the required spawn instance for this spawn task.
  373. AutoSpawnInstance spawnInst = _registeredSpawns.get(_objectId);
  374. // If the spawn is not scheduled to be active, cancel the spawn
  375. // task.
  376. if (!spawnInst.isSpawnActive())
  377. {
  378. return;
  379. }
  380. Location[] locationList = spawnInst.getLocationList();
  381. // If there are no set co-ordinates, cancel the spawn task.
  382. if (locationList.length == 0)
  383. {
  384. _log.info("AutoSpawnHandler: No location co-ords specified for spawn instance (Object ID = " + _objectId + ").");
  385. return;
  386. }
  387. int locationCount = locationList.length;
  388. int locationIndex = Rnd.nextInt(locationCount);
  389. // If random spawning is disabled, the spawn at the next set of co-ordinates after the last.
  390. // If the index is greater than the number of possible spawns, reset the counter to zero.
  391. if (!spawnInst.isRandomSpawn())
  392. {
  393. locationIndex = spawnInst._lastLocIndex + 1;
  394. if (locationIndex == locationCount)
  395. {
  396. locationIndex = 0;
  397. }
  398. spawnInst._lastLocIndex = locationIndex;
  399. }
  400. // Set the X, Y and Z co-ordinates, where this spawn will take place.
  401. final int x = locationList[locationIndex].getX();
  402. final int y = locationList[locationIndex].getY();
  403. final int z = locationList[locationIndex].getZ();
  404. final int heading = locationList[locationIndex].getHeading();
  405. final L2Spawn newSpawn = new L2Spawn(spawnInst.getId());
  406. newSpawn.setX(x);
  407. newSpawn.setY(y);
  408. newSpawn.setZ(z);
  409. if (heading != -1)
  410. {
  411. newSpawn.setHeading(heading);
  412. }
  413. newSpawn.setAmount(spawnInst.getSpawnCount());
  414. if (spawnInst._desDelay == 0)
  415. {
  416. newSpawn.setRespawnDelay(spawnInst._resDelay);
  417. }
  418. // Add the new spawn information to the spawn table, but do not store it.
  419. SpawnTable.getInstance().addNewSpawn(newSpawn, false);
  420. L2Npc npcInst = null;
  421. if (spawnInst._spawnCount == 1)
  422. {
  423. npcInst = newSpawn.doSpawn();
  424. npcInst.setXYZ(npcInst.getX(), npcInst.getY(), npcInst.getZ());
  425. spawnInst.addNpcInstance(npcInst);
  426. }
  427. else
  428. {
  429. for (int i = 0; i < spawnInst._spawnCount; i++)
  430. {
  431. npcInst = newSpawn.doSpawn();
  432. // To prevent spawning of more than one NPC in the exact same spot, move it slightly by a small random offset.
  433. npcInst.setXYZ(npcInst.getX() + Rnd.nextInt(50), npcInst.getY() + Rnd.nextInt(50), npcInst.getZ());
  434. // Add the NPC instance to the list of managed instances.
  435. spawnInst.addNpcInstance(npcInst);
  436. }
  437. }
  438. if (npcInst != null)
  439. {
  440. String nearestTown = MapRegionManager.getInstance().getClosestTownName(npcInst);
  441. // Announce to all players that the spawn has taken place, with the nearest town location.
  442. if (spawnInst.isBroadcasting())
  443. {
  444. Broadcast.toAllOnlinePlayers("The " + npcInst.getName() + " has spawned near " + nearestTown + "!");
  445. }
  446. }
  447. // If there is no despawn time, do not create a despawn task.
  448. if (spawnInst.getDespawnDelay() > 0)
  449. {
  450. AutoDespawner rd = new AutoDespawner(_objectId);
  451. ThreadPoolManager.getInstance().scheduleAi(rd, spawnInst.getDespawnDelay() - 1000);
  452. }
  453. }
  454. catch (Exception e)
  455. {
  456. _log.log(Level.WARNING, "AutoSpawnHandler: An error occurred while initializing spawn instance (Object ID = " + _objectId + "): " + e.getMessage(), e);
  457. }
  458. }
  459. }
  460. /**
  461. * AutoDespawner Class<br>
  462. * Simply used as a secondary class for despawning an auto spawn instance.
  463. * @author Tempy
  464. */
  465. private class AutoDespawner implements Runnable
  466. {
  467. private final int _objectId;
  468. protected AutoDespawner(int objectId)
  469. {
  470. _objectId = objectId;
  471. }
  472. @Override
  473. public void run()
  474. {
  475. try
  476. {
  477. AutoSpawnInstance spawnInst = _registeredSpawns.get(_objectId);
  478. if (spawnInst == null)
  479. {
  480. _log.info("AutoSpawnHandler: No spawn registered for object ID = " + _objectId + ".");
  481. return;
  482. }
  483. for (L2Npc npcInst : spawnInst.getNPCInstanceList())
  484. {
  485. npcInst.deleteMe();
  486. SpawnTable.getInstance().deleteSpawn(npcInst.getSpawn(), false);
  487. spawnInst.removeNpcInstance(npcInst);
  488. }
  489. }
  490. catch (Exception e)
  491. {
  492. _log.log(Level.WARNING, "AutoSpawnHandler: An error occurred while despawning spawn (Object ID = " + _objectId + "): " + e.getMessage(), e);
  493. }
  494. }
  495. }
  496. /**
  497. * AutoSpawnInstance Class<br>
  498. * Stores information about a registered auto spawn.
  499. * @author Tempy
  500. */
  501. public static class AutoSpawnInstance implements IIdentifiable
  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 final Queue<L2Npc> _npcList = new ConcurrentLinkedQueue<>();
  512. private final List<Location> _locList = new CopyOnWriteArrayList<>();
  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. /**
  552. * Gets the NPC ID.
  553. * @return the NPC ID
  554. */
  555. @Override
  556. public int getId()
  557. {
  558. return _npcId;
  559. }
  560. public int getSpawnCount()
  561. {
  562. return _spawnCount;
  563. }
  564. public Location[] getLocationList()
  565. {
  566. return _locList.toArray(new Location[_locList.size()]);
  567. }
  568. public Queue<L2Npc> getNPCInstanceList()
  569. {
  570. return _npcList;
  571. }
  572. public List<L2Spawn> getSpawns()
  573. {
  574. final List<L2Spawn> npcSpawns = new ArrayList<>();
  575. for (L2Npc npcInst : _npcList)
  576. {
  577. npcSpawns.add(npcInst.getSpawn());
  578. }
  579. return npcSpawns;
  580. }
  581. public void setSpawnCount(int spawnCount)
  582. {
  583. _spawnCount = spawnCount;
  584. }
  585. public void setRandomSpawn(boolean randValue)
  586. {
  587. _randomSpawn = randValue;
  588. }
  589. public void setBroadcast(boolean broadcastValue)
  590. {
  591. _broadcastAnnouncement = broadcastValue;
  592. }
  593. public boolean isSpawnActive()
  594. {
  595. return _spawnActive;
  596. }
  597. public boolean isRandomSpawn()
  598. {
  599. return _randomSpawn;
  600. }
  601. public boolean isBroadcasting()
  602. {
  603. return _broadcastAnnouncement;
  604. }
  605. public boolean addSpawnLocation(int x, int y, int z, int heading)
  606. {
  607. return _locList.add(new Location(x, y, z, heading));
  608. }
  609. public boolean addSpawnLocation(int[] spawnLoc)
  610. {
  611. if (spawnLoc.length != 3)
  612. {
  613. return false;
  614. }
  615. return addSpawnLocation(spawnLoc[0], spawnLoc[1], spawnLoc[2], -1);
  616. }
  617. public Location removeSpawnLocation(int locIndex)
  618. {
  619. try
  620. {
  621. return _locList.remove(locIndex);
  622. }
  623. catch (IndexOutOfBoundsException e)
  624. {
  625. return null;
  626. }
  627. }
  628. }
  629. private static class SingletonHolder
  630. {
  631. protected static final AutoSpawnHandler _instance = new AutoSpawnHandler();
  632. }
  633. }