SpawnTable.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.datatables;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.Statement;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javolution.util.FastSet;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.instancemanager.DayNightSpawnManager;
  30. import com.l2jserver.gameserver.model.L2Spawn;
  31. import com.l2jserver.gameserver.model.actor.L2Npc;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  34. /**
  35. * @author Nightmare
  36. */
  37. public class SpawnTable
  38. {
  39. private static final Logger _log = Logger.getLogger(SpawnTable.class.getName());
  40. private final FastSet<L2Spawn> _spawntable = new FastSet<>();
  41. private int _npcSpawnCount;
  42. private int _customSpawnCount;
  43. protected SpawnTable()
  44. {
  45. _spawntable.shared();
  46. if (!Config.ALT_DEV_NO_SPAWNS)
  47. {
  48. fillSpawnTable();
  49. }
  50. }
  51. public FastSet<L2Spawn> getSpawnTable()
  52. {
  53. return _spawntable;
  54. }
  55. private void fillSpawnTable()
  56. {
  57. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  58. Statement s = con.createStatement();
  59. ResultSet rs = s.executeQuery("SELECT count, npc_templateid, locx, locy, locz, heading, respawn_delay, respawn_random, loc_id, periodOfDay FROM spawnlist"))
  60. {
  61. L2Spawn spawnDat;
  62. L2NpcTemplate template1;
  63. while (rs.next())
  64. {
  65. template1 = NpcTable.getInstance().getTemplate(rs.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(rs.getInt("count"));
  84. spawnDat.setLocx(rs.getInt("locx"));
  85. spawnDat.setLocy(rs.getInt("locy"));
  86. spawnDat.setLocz(rs.getInt("locz"));
  87. spawnDat.setHeading(rs.getInt("heading"));
  88. spawnDat.setRespawnDelay(rs.getInt("respawn_delay"), rs.getInt("respawn_random"));
  89. int loc_id = rs.getInt("loc_id");
  90. spawnDat.setLocation(loc_id);
  91. switch (rs.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(getClass().getSimpleName() + ": Data missing in NPC table for ID: " + rs.getInt("npc_templateid") + ".");
  111. }
  112. }
  113. }
  114. catch (Exception e)
  115. {
  116. // problem with initializing spawn, go to next one
  117. _log.log(Level.WARNING, getClass().getSimpleName() + ": Spawn could not be initialized: " + e.getMessage(), e);
  118. }
  119. _log.info(getClass().getSimpleName() + ": Loaded " + _spawntable.size() + " npc spawns.");
  120. if (Config.CUSTOM_SPAWNLIST_TABLE)
  121. {
  122. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  123. Statement ps = con.createStatement();
  124. ResultSet rs = ps.executeQuery("SELECT count, npc_templateid, locx, locy, locz, heading, respawn_delay, respawn_random, loc_id, periodOfDay FROM custom_spawnlist"))
  125. {
  126. L2Spawn spawnDat;
  127. L2NpcTemplate template1;
  128. while (rs.next())
  129. {
  130. template1 = NpcTable.getInstance().getTemplate(rs.getInt("npc_templateid"));
  131. if (template1 != null)
  132. {
  133. if (template1.isType("L2SiegeGuard"))
  134. {
  135. // Don't spawn
  136. }
  137. else if (template1.isType("L2RaidBoss"))
  138. {
  139. // Don't spawn raidboss
  140. }
  141. else if (!Config.ALLOW_CLASS_MASTERS && template1.isType("L2ClassMaster"))
  142. {
  143. // Dont' spawn class masters
  144. }
  145. else
  146. {
  147. spawnDat = new L2Spawn(template1);
  148. spawnDat.setAmount(rs.getInt("count"));
  149. spawnDat.setLocx(rs.getInt("locx"));
  150. spawnDat.setLocy(rs.getInt("locy"));
  151. spawnDat.setLocz(rs.getInt("locz"));
  152. spawnDat.setHeading(rs.getInt("heading"));
  153. spawnDat.setRespawnDelay(rs.getInt("respawn_delay"), rs.getInt("respawn_random"));
  154. spawnDat.setCustom(true);
  155. int loc_id = rs.getInt("loc_id");
  156. spawnDat.setLocation(loc_id);
  157. switch (rs.getInt("periodOfDay"))
  158. {
  159. case 0: // default
  160. _customSpawnCount += spawnDat.init();
  161. break;
  162. case 1: // Day
  163. DayNightSpawnManager.getInstance().addDayCreature(spawnDat);
  164. _customSpawnCount++;
  165. break;
  166. case 2: // Night
  167. DayNightSpawnManager.getInstance().addNightCreature(spawnDat);
  168. _customSpawnCount++;
  169. break;
  170. }
  171. _spawntable.add(spawnDat);
  172. }
  173. }
  174. else
  175. {
  176. _log.warning(getClass().getSimpleName() + ": Data missing in NPC table for ID: " + rs.getInt("npc_templateid") + ".");
  177. }
  178. }
  179. }
  180. catch (Exception e)
  181. {
  182. // problem with initializing spawn, go to next one
  183. _log.log(Level.WARNING, "CustomSpawnTable: Spawn could not be initialized: " + e.getMessage(), e);
  184. }
  185. _log.info(getClass().getSimpleName() + ": Loaded " + _customSpawnCount + " custom npc spawns.");
  186. }
  187. if (Config.DEBUG)
  188. {
  189. _log.fine(getClass().getSimpleName() + ": Spawning completed, total number of NPCs in the world: " + (_npcSpawnCount + _customSpawnCount));
  190. }
  191. }
  192. public void addNewSpawn(L2Spawn spawn, boolean storeInDb)
  193. {
  194. _spawntable.add(spawn);
  195. if (storeInDb)
  196. {
  197. String spawnTable;
  198. if (spawn.isCustom() && Config.CUSTOM_SPAWNLIST_TABLE)
  199. {
  200. spawnTable = "custom_spawnlist";
  201. }
  202. else
  203. {
  204. spawnTable = "spawnlist";
  205. }
  206. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  207. PreparedStatement insert = con.prepareStatement("INSERT INTO " + spawnTable + "(count,npc_templateid,locx,locy,locz,heading,respawn_delay,respawn_random,loc_id) values(?,?,?,?,?,?,?,?,?)"))
  208. {
  209. insert.setInt(1, spawn.getAmount());
  210. insert.setInt(2, spawn.getNpcid());
  211. insert.setInt(3, spawn.getLocx());
  212. insert.setInt(4, spawn.getLocy());
  213. insert.setInt(5, spawn.getLocz());
  214. insert.setInt(6, spawn.getHeading());
  215. insert.setInt(7, (int) (spawn.getRespawnDelay() / 1000));
  216. insert.setInt(8, (int) (spawn.getRespawnMaxDelay() - spawn.getRespawnMinDelay()));
  217. insert.setInt(9, spawn.getLocation());
  218. insert.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 delete = con.prepareStatement("DELETE FROM " + (spawn.isCustom() ? "custom_spawnlist" : "spawnlist") + " WHERE locx=? AND locy=? AND locz=? AND npc_templateid=? AND heading=?"))
  237. {
  238. delete.setInt(1, spawn.getLocx());
  239. delete.setInt(2, spawn.getLocy());
  240. delete.setInt(3, spawn.getLocz());
  241. delete.setInt(4, spawn.getNpcid());
  242. delete.setInt(5, spawn.getHeading());
  243. delete.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. }