SpawnTable.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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.logging.Level;
  20. import java.util.logging.Logger;
  21. import javolution.util.FastSet;
  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.L2Npc;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  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 FastSet<L2Spawn> _spawntable = new FastSet<L2Spawn>().shared();
  39. private int _npcSpawnCount;
  40. private int _customSpawnCount;
  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 FastSet<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. statement = con.prepareStatement("SELECT count, npc_templateid, locx, locy, locz, heading, respawn_delay, loc_id, periodOfDay FROM spawnlist");
  62. ResultSet rset = statement.executeQuery();
  63. L2Spawn spawnDat;
  64. L2NpcTemplate template1;
  65. while (rset.next())
  66. {
  67. template1 = NpcTable.getInstance().getTemplate(rset.getInt("npc_templateid"));
  68. if (template1 != null)
  69. {
  70. if (template1.type.equalsIgnoreCase("L2SiegeGuard"))
  71. {
  72. // Don't spawn
  73. }
  74. else if (template1.type.equalsIgnoreCase("L2RaidBoss"))
  75. {
  76. // Don't spawn raidboss
  77. }
  78. else if (!Config.ALLOW_CLASS_MASTERS && template1.type.equals("L2ClassMaster"))
  79. {
  80. // Dont' spawn class masters
  81. }
  82. else
  83. {
  84. spawnDat = new L2Spawn(template1);
  85. spawnDat.setAmount(rset.getInt("count"));
  86. spawnDat.setLocx(rset.getInt("locx"));
  87. spawnDat.setLocy(rset.getInt("locy"));
  88. spawnDat.setLocz(rset.getInt("locz"));
  89. spawnDat.setHeading(rset.getInt("heading"));
  90. spawnDat.setRespawnDelay(rset.getInt("respawn_delay"));
  91. int loc_id = rset.getInt("loc_id");
  92. spawnDat.setLocation(loc_id);
  93. switch (rset.getInt("periodOfDay"))
  94. {
  95. case 0: // default
  96. _npcSpawnCount += spawnDat.init();
  97. break;
  98. case 1: // Day
  99. DayNightSpawnManager.getInstance().addDayCreature(spawnDat);
  100. _npcSpawnCount++;
  101. break;
  102. case 2: // Night
  103. DayNightSpawnManager.getInstance().addNightCreature(spawnDat);
  104. _npcSpawnCount++;
  105. break;
  106. }
  107. _spawntable.add(spawnDat);
  108. }
  109. }
  110. else
  111. {
  112. _log.warning("SpawnTable: Data missing in NPC table for ID: " + rset.getInt("npc_templateid") + ".");
  113. }
  114. }
  115. rset.close();
  116. statement.close();
  117. }
  118. catch (Exception e)
  119. {
  120. // problem with initializing spawn, go to next one
  121. _log.log(Level.WARNING, "SpawnTable: Spawn could not be initialized: " + e.getMessage(), e);
  122. }
  123. finally
  124. {
  125. L2DatabaseFactory.close(con);
  126. }
  127. _log.info("SpawnTable: Loaded " + _spawntable.size() + " Npc Spawn Locations.");
  128. if (Config.CUSTOM_SPAWNLIST_TABLE)
  129. {
  130. try
  131. {
  132. con = L2DatabaseFactory.getInstance().getConnection();
  133. PreparedStatement statement;
  134. statement = con.prepareStatement("SELECT count, npc_templateid, locx, locy, locz, heading, respawn_delay, loc_id, periodOfDay FROM custom_spawnlist");
  135. ResultSet rset = statement.executeQuery();
  136. L2Spawn spawnDat;
  137. L2NpcTemplate template1;
  138. while (rset.next())
  139. {
  140. template1 = NpcTable.getInstance().getTemplate(rset.getInt("npc_templateid"));
  141. if (template1 != null)
  142. {
  143. if (template1.type.equalsIgnoreCase("L2SiegeGuard"))
  144. {
  145. // Don't spawn
  146. }
  147. else if (template1.type.equalsIgnoreCase("L2RaidBoss"))
  148. {
  149. // Don't spawn raidboss
  150. }
  151. else if (!Config.ALLOW_CLASS_MASTERS && template1.type.equals("L2ClassMaster"))
  152. {
  153. // Dont' spawn class masters
  154. }
  155. else
  156. {
  157. spawnDat = new L2Spawn(template1);
  158. spawnDat.setAmount(rset.getInt("count"));
  159. spawnDat.setLocx(rset.getInt("locx"));
  160. spawnDat.setLocy(rset.getInt("locy"));
  161. spawnDat.setLocz(rset.getInt("locz"));
  162. spawnDat.setHeading(rset.getInt("heading"));
  163. spawnDat.setRespawnDelay(rset.getInt("respawn_delay"));
  164. spawnDat.setCustom(true);
  165. int loc_id = rset.getInt("loc_id");
  166. spawnDat.setLocation(loc_id);
  167. switch (rset.getInt("periodOfDay"))
  168. {
  169. case 0: // default
  170. _customSpawnCount += spawnDat.init();
  171. break;
  172. case 1: // Day
  173. DayNightSpawnManager.getInstance().addDayCreature(spawnDat);
  174. _customSpawnCount++;
  175. break;
  176. case 2: // Night
  177. DayNightSpawnManager.getInstance().addNightCreature(spawnDat);
  178. _customSpawnCount++;
  179. break;
  180. }
  181. _spawntable.add(spawnDat);
  182. }
  183. }
  184. else
  185. {
  186. _log.warning("CustomSpawnTable: Data missing in NPC table for ID: " + rset.getInt("npc_templateid") + ".");
  187. }
  188. }
  189. rset.close();
  190. statement.close();
  191. }
  192. catch (Exception e)
  193. {
  194. // problem with initializing spawn, go to next one
  195. _log.log(Level.WARNING, "CustomSpawnTable: Spawn could not be initialized: " + e.getMessage(), e);
  196. }
  197. finally
  198. {
  199. L2DatabaseFactory.close(con);
  200. }
  201. _log.info("CustomSpawnTable: Loaded " + _customSpawnCount + " Npc Spawn Locations.");
  202. }
  203. if (Config.DEBUG)
  204. _log.fine("SpawnTable: Spawning completed, total number of NPCs in the world: " + (_npcSpawnCount + _customSpawnCount));
  205. }
  206. public void addNewSpawn(L2Spawn spawn, boolean storeInDb)
  207. {
  208. _spawntable.add(spawn);
  209. if (storeInDb)
  210. {
  211. Connection con = null;
  212. String spawnTable;
  213. if (spawn.isCustom() && Config.CUSTOM_SPAWNLIST_TABLE)
  214. spawnTable = "custom_spawnlist";
  215. else
  216. spawnTable = "spawnlist";
  217. try
  218. {
  219. con = L2DatabaseFactory.getInstance().getConnection();
  220. PreparedStatement statement = con.prepareStatement("INSERT INTO " + spawnTable
  221. + "(count,npc_templateid,locx,locy,locz,heading,respawn_delay,loc_id) values(?,?,?,?,?,?,?,?)");
  222. statement.setInt(1, spawn.getAmount());
  223. statement.setInt(2, spawn.getNpcid());
  224. statement.setInt(3, spawn.getLocx());
  225. statement.setInt(4, spawn.getLocy());
  226. statement.setInt(5, spawn.getLocz());
  227. statement.setInt(6, spawn.getHeading());
  228. statement.setInt(7, spawn.getRespawnDelay() / 1000);
  229. statement.setInt(8, spawn.getLocation());
  230. statement.execute();
  231. statement.close();
  232. }
  233. catch (Exception e)
  234. {
  235. // problem with storing spawn
  236. _log.log(Level.WARNING, "SpawnTable: Could not store spawn in the DB:" + e.getMessage(), e);
  237. }
  238. finally
  239. {
  240. L2DatabaseFactory.close(con);
  241. }
  242. }
  243. }
  244. public void deleteSpawn(L2Spawn spawn, boolean updateDb)
  245. {
  246. if (!_spawntable.remove(spawn))
  247. return;
  248. if (updateDb)
  249. {
  250. Connection con = null;
  251. try
  252. {
  253. con = L2DatabaseFactory.getInstance().getConnection();
  254. PreparedStatement statement = con.prepareStatement("DELETE FROM "
  255. + (spawn.isCustom() ? "custom_spawnlist" : "spawnlist") + " WHERE locx=? AND locy=? AND locz=? AND npc_templateid=? AND heading=?");
  256. statement.setInt(1, spawn.getLocx());
  257. statement.setInt(2, spawn.getLocy());
  258. statement.setInt(3, spawn.getLocz());
  259. statement.setInt(4, spawn.getNpcid());
  260. statement.setInt(5, spawn.getHeading());
  261. statement.execute();
  262. statement.close();
  263. }
  264. catch (Exception e)
  265. {
  266. // problem with deleting spawn
  267. _log.log(Level.WARNING, "SpawnTable: Spawn " + spawn + " could not be removed from DB: " + e.getMessage(), e);
  268. }
  269. finally
  270. {
  271. L2DatabaseFactory.close(con);
  272. }
  273. }
  274. }
  275. //just wrapper
  276. public void reloadAll()
  277. {
  278. fillSpawnTable();
  279. }
  280. /**
  281. * Get all the spawn of a NPC<BR><BR>
  282. *
  283. * @param npcId : ID of the NPC to find.
  284. * @return
  285. */
  286. public void findNPCInstances(L2PcInstance activeChar, int npcId, int teleportIndex, boolean showposition)
  287. {
  288. int index = 0;
  289. for (L2Spawn spawn : _spawntable)
  290. {
  291. if (npcId == spawn.getNpcid())
  292. {
  293. index++;
  294. L2Npc _npc = spawn.getLastSpawn();
  295. if (teleportIndex > -1)
  296. {
  297. if (teleportIndex == index)
  298. {
  299. if(showposition && _npc != null)
  300. activeChar.teleToLocation(_npc.getX(), _npc.getY(), _npc.getZ(), true);
  301. else
  302. activeChar.teleToLocation(spawn.getLocx(), spawn.getLocy(), spawn.getLocz(), true);
  303. }
  304. }
  305. else
  306. {
  307. if(showposition && _npc != null)
  308. activeChar.sendMessage(index + " - " + spawn.getTemplate().name + " (" + spawn + "): " + _npc.getX() + " "+ _npc.getY() + " " + _npc.getZ());
  309. else
  310. activeChar.sendMessage(index + " - " + spawn.getTemplate().name + " (" + spawn + "): " + spawn.getLocx() + " "+ spawn.getLocy() + " " + spawn.getLocz());
  311. }
  312. }
  313. }
  314. if (index == 0)
  315. activeChar.sendMessage("No current spawns found.");
  316. }
  317. @SuppressWarnings("synthetic-access")
  318. private static class SingletonHolder
  319. {
  320. protected static final SpawnTable _instance = new SpawnTable();
  321. }
  322. }