SpawnTable.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. L2DatabaseFactory.close(con);
  137. }
  138. _log.info("SpawnTable: Loaded " + _spawntable.size() + " Npc Spawn Locations.");
  139. if (Config.CUSTOM_SPAWNLIST_TABLE)
  140. {
  141. try
  142. {
  143. con = L2DatabaseFactory.getInstance().getConnection();
  144. PreparedStatement statement;
  145. if (Config.DELETE_GMSPAWN_ON_CUSTOM)
  146. {
  147. 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");
  148. }
  149. else
  150. {
  151. statement = con.prepareStatement("SELECT id, count, npc_templateid, locx, locy, locz, heading, respawn_delay, loc_id, periodOfDay FROM custom_spawnlist ORDER BY id");
  152. }
  153. ResultSet rset = statement.executeQuery();
  154. L2Spawn spawnDat;
  155. L2NpcTemplate template1;
  156. while (rset.next())
  157. {
  158. template1 = NpcTable.getInstance().getTemplate(rset.getInt("npc_templateid"));
  159. if (template1 != null)
  160. {
  161. if (template1.type.equalsIgnoreCase("L2SiegeGuard"))
  162. {
  163. // Don't spawn
  164. }
  165. else if (template1.type.equalsIgnoreCase("L2RaidBoss"))
  166. {
  167. // Don't spawn raidboss
  168. }
  169. else if (!Config.ALLOW_CLASS_MASTERS && template1.type.equals("L2ClassMaster"))
  170. {
  171. // Dont' spawn class masters
  172. }
  173. else
  174. {
  175. spawnDat = new L2Spawn(template1);
  176. spawnDat.setId(rset.getInt("id"));
  177. spawnDat.setAmount(rset.getInt("count"));
  178. spawnDat.setLocx(rset.getInt("locx"));
  179. spawnDat.setLocy(rset.getInt("locy"));
  180. spawnDat.setLocz(rset.getInt("locz"));
  181. spawnDat.setHeading(rset.getInt("heading"));
  182. spawnDat.setRespawnDelay(rset.getInt("respawn_delay"));
  183. spawnDat.setCustom(true);
  184. int loc_id = rset.getInt("loc_id");
  185. spawnDat.setLocation(loc_id);
  186. switch (rset.getInt("periodOfDay"))
  187. {
  188. case 0: // default
  189. _customSpawnCount += spawnDat.init();
  190. break;
  191. case 1: // Day
  192. DayNightSpawnManager.getInstance().addDayCreature(spawnDat);
  193. _customSpawnCount++;
  194. break;
  195. case 2: // Night
  196. DayNightSpawnManager.getInstance().addNightCreature(spawnDat);
  197. _customSpawnCount++;
  198. break;
  199. }
  200. _spawntable.put(spawnDat.getId(), spawnDat);
  201. if (spawnDat.getId() > _highestId)
  202. _highestId = spawnDat.getId();
  203. }
  204. }
  205. else
  206. {
  207. _log.warning("CustomSpawnTable: Data missing in NPC table for ID: " + rset.getInt("npc_templateid") + ".");
  208. }
  209. }
  210. rset.close();
  211. statement.close();
  212. }
  213. catch (Exception e)
  214. {
  215. // problem with initializing spawn, go to next one
  216. _log.log(Level.WARNING, "CustomSpawnTable: Spawn could not be initialized: " + e.getMessage(), e);
  217. }
  218. finally
  219. {
  220. L2DatabaseFactory.close(con);
  221. }
  222. _log.info("CustomSpawnTable: Loaded " + _customSpawnCount + " Npc Spawn Locations.");
  223. }
  224. if (Config.DEBUG)
  225. _log.fine("SpawnTable: Spawning completed, total number of NPCs in the world: " + (_npcSpawnCount + _customSpawnCount));
  226. }
  227. public L2Spawn getTemplate(int id)
  228. {
  229. return _spawntable.get(id);
  230. }
  231. public void addNewSpawn(L2Spawn spawn, boolean storeInDb)
  232. {
  233. _highestId++;
  234. spawn.setId(_highestId);
  235. _spawntable.put(_highestId, spawn);
  236. if (storeInDb)
  237. {
  238. Connection con = null;
  239. String spawnTable;
  240. if (spawn.isCustom() && Config.CUSTOM_SPAWNLIST_TABLE)
  241. spawnTable = "custom_spawnlist";
  242. else
  243. spawnTable = "spawnlist";
  244. try
  245. {
  246. con = L2DatabaseFactory.getInstance().getConnection();
  247. PreparedStatement statement = con.prepareStatement("INSERT INTO " + spawnTable
  248. + "(id,count,npc_templateid,locx,locy,locz,heading,respawn_delay,loc_id) values(?,?,?,?,?,?,?,?,?)");
  249. statement.setInt(1, spawn.getId());
  250. statement.setInt(2, spawn.getAmount());
  251. statement.setInt(3, spawn.getNpcid());
  252. statement.setInt(4, spawn.getLocx());
  253. statement.setInt(5, spawn.getLocy());
  254. statement.setInt(6, spawn.getLocz());
  255. statement.setInt(7, spawn.getHeading());
  256. statement.setInt(8, spawn.getRespawnDelay() / 1000);
  257. statement.setInt(9, spawn.getLocation());
  258. statement.execute();
  259. statement.close();
  260. }
  261. catch (Exception e)
  262. {
  263. // problem with storing spawn
  264. _log.log(Level.WARNING, "SpawnTable: Could not store spawn in the DB:" + e.getMessage(), e);
  265. }
  266. finally
  267. {
  268. L2DatabaseFactory.close(con);
  269. }
  270. }
  271. }
  272. public void deleteSpawn(L2Spawn spawn, boolean updateDb)
  273. {
  274. if (_spawntable.remove(spawn.getId()) == null)
  275. return;
  276. if (updateDb)
  277. {
  278. Connection con = null;
  279. if (Config.DELETE_GMSPAWN_ON_CUSTOM)
  280. {
  281. try
  282. {
  283. con = L2DatabaseFactory.getInstance().getConnection();
  284. PreparedStatement statement = con.prepareStatement("Replace into custom_notspawned VALUES (?,?)");
  285. statement.setInt(1, spawn.getId());
  286. statement.setBoolean(2, spawn.isCustom());
  287. statement.execute();
  288. statement.close();
  289. }
  290. catch (Exception e)
  291. {
  292. // problem with inserting nospawn
  293. _log.log(Level.WARNING, "SpawnTable: Spawn " + spawn.getId() + " could not be insert into DB: " + e.getMessage(), e);
  294. }
  295. finally
  296. {
  297. L2DatabaseFactory.close(con);
  298. }
  299. }
  300. else
  301. {
  302. try
  303. {
  304. con = L2DatabaseFactory.getInstance().getConnection();
  305. PreparedStatement statement = con.prepareStatement("DELETE FROM "
  306. + (spawn.isCustom() ? "custom_spawnlist" : "spawnlist") + " WHERE id=?");
  307. statement.setInt(1, spawn.getId());
  308. statement.execute();
  309. statement.close();
  310. }
  311. catch (Exception e)
  312. {
  313. // problem with deleting spawn
  314. _log.log(Level.WARNING, "SpawnTable: Spawn " + spawn.getId() + " could not be removed from DB: " + e.getMessage(), e);
  315. }
  316. finally
  317. {
  318. L2DatabaseFactory.close(con);
  319. }
  320. }
  321. }
  322. }
  323. //just wrapper
  324. public void reloadAll()
  325. {
  326. fillSpawnTable();
  327. }
  328. /**
  329. * Get all the spawn of a NPC<BR><BR>
  330. *
  331. * @param npcId : ID of the NPC to find.
  332. * @return
  333. */
  334. public void findNPCInstances(L2PcInstance activeChar, int npcId, int teleportIndex)
  335. {
  336. int index = 0;
  337. for (L2Spawn spawn : _spawntable.values())
  338. {
  339. if (npcId == spawn.getNpcid())
  340. {
  341. index++;
  342. if (teleportIndex > -1)
  343. {
  344. if (teleportIndex == index)
  345. activeChar.teleToLocation(spawn.getLocx(), spawn.getLocy(), spawn.getLocz(), true);
  346. }
  347. else
  348. {
  349. activeChar.sendMessage(index + " - " + spawn.getTemplate().name + " (" + spawn.getId() + "): " + spawn.getLocx() + " "
  350. + spawn.getLocy() + " " + spawn.getLocz());
  351. }
  352. }
  353. }
  354. if (index == 0)
  355. activeChar.sendMessage("No current spawns found.");
  356. }
  357. @SuppressWarnings("synthetic-access")
  358. private static class SingletonHolder
  359. {
  360. protected static final SpawnTable _instance = new SpawnTable();
  361. }
  362. }