SpawnTable.java 12 KB

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