SpawnTable.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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.datatables;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.Map;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.L2DatabaseFactory;
  24. import com.l2jserver.gameserver.instancemanager.DayNightSpawnManager;
  25. import com.l2jserver.gameserver.model.L2Spawn;
  26. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  27. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  28. import javolution.util.FastMap;
  29. /**
  30. * This class ...
  31. *
  32. * @author Nightmare
  33. * @version $Revision: 1.5.2.6.2.7 $ $Date: 2005/03/27 15:29:18 $
  34. */
  35. public class SpawnTable
  36. {
  37. private static Logger _log = Logger.getLogger(SpawnTable.class.getName());
  38. private Map<Integer, L2Spawn> _spawntable = new FastMap<Integer, L2Spawn>().shared();
  39. private int _npcSpawnCount;
  40. private int _customSpawnCount;
  41. private int _highestId;
  42. public static SpawnTable getInstance()
  43. {
  44. return SingletonHolder._instance;
  45. }
  46. private SpawnTable()
  47. {
  48. if (!Config.ALT_DEV_NO_SPAWNS)
  49. fillSpawnTable();
  50. }
  51. public Map<Integer, L2Spawn> getSpawnTable()
  52. {
  53. return _spawntable;
  54. }
  55. private void fillSpawnTable()
  56. {
  57. Connection con = null;
  58. try
  59. {
  60. con = L2DatabaseFactory.getInstance().getConnection();
  61. PreparedStatement statement;
  62. if (Config.DELETE_GMSPAWN_ON_CUSTOM)
  63. {
  64. statement = con.prepareStatement("SELECT id, count, npc_templateid, locx, locy, locz, heading, respawn_delay, loc_id, periodOfDay FROM spawnlist where id NOT in ( select id from custom_notspawned where isCustom = false ) ORDER BY id");
  65. }
  66. else
  67. {
  68. statement = con.prepareStatement("SELECT id, count, npc_templateid, locx, locy, locz, heading, respawn_delay, loc_id, periodOfDay FROM spawnlist ORDER BY id");
  69. }
  70. ResultSet rset = statement.executeQuery();
  71. L2Spawn spawnDat;
  72. L2NpcTemplate template1;
  73. while (rset.next())
  74. {
  75. template1 = NpcTable.getInstance().getTemplate(rset.getInt("npc_templateid"));
  76. if (template1 != null)
  77. {
  78. if (template1.type.equalsIgnoreCase("L2SiegeGuard"))
  79. {
  80. // Don't spawn
  81. }
  82. else if (template1.type.equalsIgnoreCase("L2RaidBoss"))
  83. {
  84. // Don't spawn raidboss
  85. }
  86. else if (!Config.ALLOW_CLASS_MASTERS && template1.type.equals("L2ClassMaster"))
  87. {
  88. // Dont' spawn class masters
  89. }
  90. else
  91. {
  92. spawnDat = new L2Spawn(template1);
  93. spawnDat.setId(rset.getInt("id"));
  94. spawnDat.setAmount(rset.getInt("count"));
  95. spawnDat.setLocx(rset.getInt("locx"));
  96. spawnDat.setLocy(rset.getInt("locy"));
  97. spawnDat.setLocz(rset.getInt("locz"));
  98. spawnDat.setHeading(rset.getInt("heading"));
  99. spawnDat.setRespawnDelay(rset.getInt("respawn_delay"));
  100. int loc_id = rset.getInt("loc_id");
  101. spawnDat.setLocation(loc_id);
  102. switch (rset.getInt("periodOfDay"))
  103. {
  104. case 0: // default
  105. _npcSpawnCount += spawnDat.init();
  106. break;
  107. case 1: // Day
  108. DayNightSpawnManager.getInstance().addDayCreature(spawnDat);
  109. _npcSpawnCount++;
  110. break;
  111. case 2: // Night
  112. DayNightSpawnManager.getInstance().addNightCreature(spawnDat);
  113. _npcSpawnCount++;
  114. break;
  115. }
  116. _spawntable.put(spawnDat.getId(), spawnDat);
  117. if (spawnDat.getId() > _highestId)
  118. _highestId = spawnDat.getId();
  119. }
  120. }
  121. else
  122. {
  123. _log.warning("SpawnTable: Data missing in NPC table for ID: " + rset.getInt("npc_templateid") + ".");
  124. }
  125. }
  126. rset.close();
  127. statement.close();
  128. }
  129. catch (Exception e)
  130. {
  131. // problem with initializing spawn, go to next one
  132. _log.log(Level.WARNING, "SpawnTable: Spawn could not be initialized: " + e.getMessage(), e);
  133. }
  134. finally
  135. {
  136. try
  137. {
  138. con.close();
  139. }
  140. catch (Exception e)
  141. {
  142. }
  143. }
  144. _log.info("SpawnTable: Loaded " + _spawntable.size() + " Npc Spawn Locations.");
  145. if (Config.CUSTOM_SPAWNLIST_TABLE)
  146. {
  147. try
  148. {
  149. con = L2DatabaseFactory.getInstance().getConnection();
  150. PreparedStatement statement;
  151. if (Config.DELETE_GMSPAWN_ON_CUSTOM)
  152. {
  153. statement = con.prepareStatement("SELECT id, count, npc_templateid, locx, locy, locz, heading, respawn_delay, loc_id, periodOfDay FROM custom_spawnlist where id NOT in ( select id from custom_notspawned where isCustom = true ) ORDER BY id");
  154. }
  155. else
  156. {
  157. statement = con.prepareStatement("SELECT id, count, npc_templateid, locx, locy, locz, heading, respawn_delay, loc_id, periodOfDay FROM custom_spawnlist ORDER BY id");
  158. }
  159. ResultSet rset = statement.executeQuery();
  160. L2Spawn spawnDat;
  161. L2NpcTemplate template1;
  162. while (rset.next())
  163. {
  164. template1 = NpcTable.getInstance().getTemplate(rset.getInt("npc_templateid"));
  165. if (template1 != null)
  166. {
  167. if (template1.type.equalsIgnoreCase("L2SiegeGuard"))
  168. {
  169. // Don't spawn
  170. }
  171. else if (template1.type.equalsIgnoreCase("L2RaidBoss"))
  172. {
  173. // Don't spawn raidboss
  174. }
  175. else if (!Config.ALLOW_CLASS_MASTERS && template1.type.equals("L2ClassMaster"))
  176. {
  177. // Dont' spawn class masters
  178. }
  179. else
  180. {
  181. spawnDat = new L2Spawn(template1);
  182. spawnDat.setId(rset.getInt("id"));
  183. spawnDat.setAmount(rset.getInt("count"));
  184. spawnDat.setLocx(rset.getInt("locx"));
  185. spawnDat.setLocy(rset.getInt("locy"));
  186. spawnDat.setLocz(rset.getInt("locz"));
  187. spawnDat.setHeading(rset.getInt("heading"));
  188. spawnDat.setRespawnDelay(rset.getInt("respawn_delay"));
  189. spawnDat.setCustom(true);
  190. int loc_id = rset.getInt("loc_id");
  191. spawnDat.setLocation(loc_id);
  192. switch (rset.getInt("periodOfDay"))
  193. {
  194. case 0: // default
  195. _customSpawnCount += spawnDat.init();
  196. break;
  197. case 1: // Day
  198. DayNightSpawnManager.getInstance().addDayCreature(spawnDat);
  199. _customSpawnCount++;
  200. break;
  201. case 2: // Night
  202. DayNightSpawnManager.getInstance().addNightCreature(spawnDat);
  203. _customSpawnCount++;
  204. break;
  205. }
  206. _spawntable.put(spawnDat.getId(), spawnDat);
  207. if (spawnDat.getId() > _highestId)
  208. _highestId = spawnDat.getId();
  209. }
  210. }
  211. else
  212. {
  213. _log.warning("CustomSpawnTable: Data missing in NPC table for ID: " + rset.getInt("npc_templateid") + ".");
  214. }
  215. }
  216. rset.close();
  217. statement.close();
  218. }
  219. catch (Exception e)
  220. {
  221. // problem with initializing spawn, go to next one
  222. _log.log(Level.WARNING, "CustomSpawnTable: Spawn could not be initialized: " + e.getMessage(), e);
  223. }
  224. finally
  225. {
  226. try
  227. {
  228. con.close();
  229. }
  230. catch (Exception e)
  231. {
  232. }
  233. }
  234. _log.info("CustomSpawnTable: Loaded " + _customSpawnCount + " Npc Spawn Locations.");
  235. }
  236. if (Config.DEBUG)
  237. _log.fine("SpawnTable: Spawning completed, total number of NPCs in the world: " + (_npcSpawnCount + _customSpawnCount));
  238. }
  239. public L2Spawn getTemplate(int id)
  240. {
  241. return _spawntable.get(id);
  242. }
  243. public void addNewSpawn(L2Spawn spawn, boolean storeInDb)
  244. {
  245. _highestId++;
  246. spawn.setId(_highestId);
  247. _spawntable.put(_highestId, spawn);
  248. if (storeInDb)
  249. {
  250. Connection con = null;
  251. String spawnTable;
  252. if (spawn.isCustom() && Config.CUSTOM_SPAWNLIST_TABLE)
  253. spawnTable = "custom_spawnlist";
  254. else
  255. spawnTable = "spawnlist";
  256. try
  257. {
  258. con = L2DatabaseFactory.getInstance().getConnection();
  259. PreparedStatement statement = con.prepareStatement("INSERT INTO " + spawnTable
  260. + "(id,count,npc_templateid,locx,locy,locz,heading,respawn_delay,loc_id) values(?,?,?,?,?,?,?,?,?)");
  261. statement.setInt(1, spawn.getId());
  262. statement.setInt(2, spawn.getAmount());
  263. statement.setInt(3, spawn.getNpcid());
  264. statement.setInt(4, spawn.getLocx());
  265. statement.setInt(5, spawn.getLocy());
  266. statement.setInt(6, spawn.getLocz());
  267. statement.setInt(7, spawn.getHeading());
  268. statement.setInt(8, spawn.getRespawnDelay() / 1000);
  269. statement.setInt(9, spawn.getLocation());
  270. statement.execute();
  271. statement.close();
  272. }
  273. catch (Exception e)
  274. {
  275. // problem with storing spawn
  276. _log.log(Level.WARNING, "SpawnTable: Could not store spawn in the DB:" + e.getMessage(), e);
  277. }
  278. finally
  279. {
  280. try
  281. {
  282. con.close();
  283. }
  284. catch (Exception e)
  285. {
  286. }
  287. }
  288. }
  289. }
  290. public void deleteSpawn(L2Spawn spawn, boolean updateDb)
  291. {
  292. if (_spawntable.remove(spawn.getId()) == null)
  293. return;
  294. if (updateDb)
  295. {
  296. Connection con = null;
  297. if (Config.DELETE_GMSPAWN_ON_CUSTOM)
  298. {
  299. try
  300. {
  301. con = L2DatabaseFactory.getInstance().getConnection();
  302. PreparedStatement statement = con.prepareStatement("Replace into custom_notspawned VALUES (?,?)");
  303. statement.setInt(1, spawn.getId());
  304. statement.setBoolean(2, spawn.isCustom());
  305. statement.execute();
  306. statement.close();
  307. }
  308. catch (Exception e)
  309. {
  310. // problem with inserting nospawn
  311. _log.log(Level.WARNING, "SpawnTable: Spawn " + spawn.getId() + " could not be insert into DB: " + e.getMessage(), e);
  312. }
  313. finally
  314. {
  315. try
  316. {
  317. con.close();
  318. }
  319. catch (Exception e)
  320. {
  321. }
  322. }
  323. }
  324. else
  325. {
  326. try
  327. {
  328. con = L2DatabaseFactory.getInstance().getConnection();
  329. PreparedStatement statement = con.prepareStatement("DELETE FROM "
  330. + (spawn.isCustom() ? "custom_spawnlist" : "spawnlist") + " WHERE id=?");
  331. statement.setInt(1, spawn.getId());
  332. statement.execute();
  333. statement.close();
  334. }
  335. catch (Exception e)
  336. {
  337. // problem with deleting spawn
  338. _log.log(Level.WARNING, "SpawnTable: Spawn " + spawn.getId() + " could not be removed from DB: " + e.getMessage(), e);
  339. }
  340. finally
  341. {
  342. try
  343. {
  344. con.close();
  345. }
  346. catch (Exception e)
  347. {
  348. }
  349. }
  350. }
  351. }
  352. }
  353. //just wrapper
  354. public void reloadAll()
  355. {
  356. fillSpawnTable();
  357. }
  358. /**
  359. * Get all the spawn of a NPC<BR><BR>
  360. *
  361. * @param npcId : ID of the NPC to find.
  362. * @return
  363. */
  364. public void findNPCInstances(L2PcInstance activeChar, int npcId, int teleportIndex)
  365. {
  366. int index = 0;
  367. for (L2Spawn spawn : _spawntable.values())
  368. {
  369. if (npcId == spawn.getNpcid())
  370. {
  371. index++;
  372. if (teleportIndex > -1)
  373. {
  374. if (teleportIndex == index)
  375. activeChar.teleToLocation(spawn.getLocx(), spawn.getLocy(), spawn.getLocz(), true);
  376. }
  377. else
  378. {
  379. activeChar.sendMessage(index + " - " + spawn.getTemplate().name + " (" + spawn.getId() + "): " + spawn.getLocx() + " "
  380. + spawn.getLocy() + " " + spawn.getLocz());
  381. }
  382. }
  383. }
  384. if (index == 0)
  385. activeChar.sendMessage("No current spawns found.");
  386. }
  387. @SuppressWarnings("synthetic-access")
  388. private static class SingletonHolder
  389. {
  390. protected static final SpawnTable _instance = new SpawnTable();
  391. }
  392. }