SpawnTable.java 9.9 KB

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