SpawnTable.java 9.9 KB

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