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