2
0

SpawnTable.java 10 KB

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