SpawnTable.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 net.sf.l2j.gameserver.datatables;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.util.Map;
  19. import java.util.logging.Logger;
  20. import javolution.util.FastMap;
  21. import net.sf.l2j.Config;
  22. import net.sf.l2j.L2DatabaseFactory;
  23. import net.sf.l2j.gameserver.instancemanager.DayNightSpawnManager;
  24. import net.sf.l2j.gameserver.model.L2Spawn;
  25. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  26. import net.sf.l2j.gameserver.templates.L2NpcTemplate;
  27. /**
  28. * This class ...
  29. *
  30. * @author Nightmare
  31. * @version $Revision: 1.5.2.6.2.7 $ $Date: 2005/03/27 15:29:18 $
  32. */
  33. public class SpawnTable
  34. {
  35. private static Logger _log = Logger.getLogger(SpawnTable.class.getName());
  36. private static final SpawnTable _instance = new SpawnTable();
  37. private Map<Integer, L2Spawn> _spawntable = new FastMap<Integer, L2Spawn>().setShared(true);
  38. private int _npcSpawnCount;
  39. private int _customSpawnCount;
  40. private int _highestId;
  41. public static SpawnTable getInstance()
  42. {
  43. return _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. java.sql.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. java.sql.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 + "(id,count,npc_templateid,locx,locy,locz,heading,respawn_delay,loc_id) values(?,?,?,?,?,?,?,?,?)");
  259. statement.setInt(1, spawn.getId());
  260. statement.setInt(2, spawn.getAmount());
  261. statement.setInt(3, spawn.getNpcid());
  262. statement.setInt(4, spawn.getLocx());
  263. statement.setInt(5, spawn.getLocy());
  264. statement.setInt(6, spawn.getLocz());
  265. statement.setInt(7, spawn.getHeading());
  266. statement.setInt(8, spawn.getRespawnDelay() / 1000);
  267. statement.setInt(9, spawn.getLocation());
  268. statement.execute();
  269. statement.close();
  270. }
  271. catch (Exception e)
  272. {
  273. // problem with storing spawn
  274. _log.warning("SpawnTable: Could not store spawn in the DB:" + e);
  275. }
  276. finally
  277. {
  278. try
  279. {
  280. con.close();
  281. }
  282. catch (Exception e)
  283. {
  284. }
  285. }
  286. }
  287. }
  288. public void deleteSpawn(L2Spawn spawn, boolean updateDb)
  289. {
  290. if (_spawntable.remove(spawn.getId()) == null)
  291. return;
  292. if (updateDb)
  293. {
  294. java.sql.Connection con = null;
  295. if (Config.DELETE_GMSPAWN_ON_CUSTOM)
  296. {
  297. try
  298. {
  299. con = L2DatabaseFactory.getInstance().getConnection();
  300. PreparedStatement statement = con.prepareStatement("Replace into custom_notspawned VALUES (?,?)");
  301. statement.setInt(1, spawn.getId());
  302. statement.setBoolean(2, spawn.isCustom());
  303. statement.execute();
  304. statement.close();
  305. }
  306. catch (Exception e)
  307. {
  308. // problem with inserting nospawn
  309. _log.warning("SpawnTable: Spawn " + spawn.getId() + " could not be insert into DB: " + e);
  310. }
  311. finally
  312. {
  313. try
  314. {
  315. con.close();
  316. }
  317. catch (Exception e)
  318. {
  319. }
  320. }
  321. }
  322. else
  323. {
  324. try
  325. {
  326. con = L2DatabaseFactory.getInstance().getConnection();
  327. PreparedStatement statement = con.prepareStatement("DELETE FROM " + (spawn.isCustom() ? "custom_spawnlist" : "spawnlist") + " WHERE id=?");
  328. statement.setInt(1, spawn.getId());
  329. statement.execute();
  330. statement.close();
  331. }
  332. catch (Exception e)
  333. {
  334. // problem with deleting spawn
  335. _log.warning("SpawnTable: Spawn " + spawn.getId() + " could not be removed from DB: " + e);
  336. }
  337. finally
  338. {
  339. try
  340. {
  341. con.close();
  342. }
  343. catch (Exception e)
  344. {
  345. }
  346. }
  347. }
  348. }
  349. }
  350. //just wrapper
  351. public void reloadAll()
  352. {
  353. fillSpawnTable();
  354. }
  355. /**
  356. * Get all the spawn of a NPC<BR><BR>
  357. *
  358. * @param npcId : ID of the NPC to find.
  359. * @return
  360. */
  361. public void findNPCInstances(L2PcInstance activeChar, int npcId, int teleportIndex)
  362. {
  363. int index = 0;
  364. for (L2Spawn spawn : _spawntable.values())
  365. {
  366. if (npcId == spawn.getNpcid())
  367. {
  368. index++;
  369. if (teleportIndex > -1)
  370. {
  371. if (teleportIndex == index)
  372. activeChar.teleToLocation(spawn.getLocx(), spawn.getLocy(), spawn.getLocz(), true);
  373. }
  374. else
  375. {
  376. activeChar.sendMessage(index + " - " + spawn.getTemplate().name + " (" + spawn.getId() + "): " + spawn.getLocx() + " " + spawn.getLocy() + " " + spawn.getLocz());
  377. }
  378. }
  379. }
  380. if (index == 0)
  381. activeChar.sendMessage("No current spawns found.");
  382. }
  383. }