SpawnTable.java 10 KB

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