SpawnTable.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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.Map;
  25. import java.util.Set;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import javolution.util.FastMap;
  29. import javolution.util.FastSet;
  30. import com.l2jserver.Config;
  31. import com.l2jserver.L2DatabaseFactory;
  32. import com.l2jserver.gameserver.instancemanager.DayNightSpawnManager;
  33. import com.l2jserver.gameserver.model.L2Spawn;
  34. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  35. import com.l2jserver.gameserver.model.interfaces.IL2Procedure;
  36. /**
  37. * Spawn data retriever.
  38. * @author Zoey76
  39. */
  40. public final class SpawnTable
  41. {
  42. private static final Logger _log = Logger.getLogger(SpawnTable.class.getName());
  43. // SQL
  44. private static final String SELECT_SPAWNS = "SELECT count, npc_templateid, locx, locy, locz, heading, respawn_delay, respawn_random, loc_id, periodOfDay FROM spawnlist";
  45. private static final String SELECT_CUSTOM_SPAWNS = "SELECT count, npc_templateid, locx, locy, locz, heading, respawn_delay, respawn_random, loc_id, periodOfDay FROM custom_spawnlist";
  46. private static final Map<Integer, Set<L2Spawn>> _spawnTable = new FastMap<Integer, Set<L2Spawn>>().shared();
  47. protected SpawnTable()
  48. {
  49. load();
  50. }
  51. /**
  52. * Wrapper to load all spawns.
  53. */
  54. public void load()
  55. {
  56. if (!Config.ALT_DEV_NO_SPAWNS)
  57. {
  58. fillSpawnTable(false);
  59. final int spawnCount = _spawnTable.size();
  60. _log.info(getClass().getSimpleName() + ": Loaded " + spawnCount + " npc spawns.");
  61. if (Config.CUSTOM_SPAWNLIST_TABLE)
  62. {
  63. fillSpawnTable(true);
  64. _log.info(getClass().getSimpleName() + ": Loaded " + (_spawnTable.size() - spawnCount) + " custom npc spawns.");
  65. }
  66. }
  67. }
  68. private int fillSpawnTable(boolean isCustom)
  69. {
  70. int npcSpawnCount = 0;
  71. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  72. Statement s = con.createStatement();
  73. ResultSet rs = s.executeQuery(isCustom ? SELECT_CUSTOM_SPAWNS : SELECT_SPAWNS))
  74. {
  75. L2Spawn spawn;
  76. L2NpcTemplate npcTemplate;
  77. int npcId;
  78. while (rs.next())
  79. {
  80. npcId = rs.getInt("npc_templateid");
  81. npcTemplate = NpcTable.getInstance().getTemplate(npcId);
  82. if (npcTemplate == null)
  83. {
  84. _log.warning(getClass().getSimpleName() + ": Data missing in NPC table for ID: " + npcId + ".");
  85. continue;
  86. }
  87. if (npcTemplate.isType("L2SiegeGuard") || npcTemplate.isType("L2RaidBoss") || (!Config.ALLOW_CLASS_MASTERS && npcTemplate.isType("L2ClassMaster")))
  88. {
  89. // Don't spawn
  90. continue;
  91. }
  92. spawn = new L2Spawn(npcTemplate);
  93. spawn.setAmount(rs.getInt("count"));
  94. spawn.setLocx(rs.getInt("locx"));
  95. spawn.setLocy(rs.getInt("locy"));
  96. spawn.setLocz(rs.getInt("locz"));
  97. spawn.setHeading(rs.getInt("heading"));
  98. spawn.setRespawnDelay(rs.getInt("respawn_delay"), rs.getInt("respawn_random"));
  99. int loc_id = rs.getInt("loc_id");
  100. spawn.setLocation(loc_id);
  101. switch (rs.getInt("periodOfDay"))
  102. {
  103. case 0: // default
  104. npcSpawnCount += spawn.init();
  105. break;
  106. case 1: // Day
  107. DayNightSpawnManager.getInstance().addDayCreature(spawn);
  108. npcSpawnCount++;
  109. break;
  110. case 2: // Night
  111. DayNightSpawnManager.getInstance().addNightCreature(spawn);
  112. npcSpawnCount++;
  113. break;
  114. }
  115. addSpawn(spawn);
  116. }
  117. }
  118. catch (Exception e)
  119. {
  120. _log.log(Level.WARNING, getClass().getSimpleName() + ": Spawn could not be initialized: " + e.getMessage(), e);
  121. }
  122. return npcSpawnCount;
  123. }
  124. public Map<Integer, Set<L2Spawn>> getSpawnTable()
  125. {
  126. return _spawnTable;
  127. }
  128. public Set<L2Spawn> getSpawns(int npcId)
  129. {
  130. return _spawnTable.get(npcId);
  131. }
  132. public L2Spawn getFirstSpawn(int npcId)
  133. {
  134. if (_spawnTable.containsKey(npcId))
  135. {
  136. for (L2Spawn spawn : _spawnTable.get(npcId))
  137. {
  138. if (spawn != null)
  139. {
  140. return spawn;
  141. }
  142. }
  143. }
  144. return null;
  145. }
  146. public void addNewSpawn(L2Spawn spawn, boolean storeInDb)
  147. {
  148. addSpawn(spawn);
  149. if (storeInDb)
  150. {
  151. final String spawnTable = spawn.isCustom() && Config.CUSTOM_SPAWNLIST_TABLE ? "custom_spawnlist" : "spawnlist";
  152. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  153. PreparedStatement insert = con.prepareStatement("INSERT INTO " + spawnTable + "(count,npc_templateid,locx,locy,locz,heading,respawn_delay,respawn_random,loc_id) values(?,?,?,?,?,?,?,?,?)"))
  154. {
  155. insert.setInt(1, spawn.getAmount());
  156. insert.setInt(2, spawn.getNpcid());
  157. insert.setInt(3, spawn.getLocx());
  158. insert.setInt(4, spawn.getLocy());
  159. insert.setInt(5, spawn.getLocz());
  160. insert.setInt(6, spawn.getHeading());
  161. insert.setInt(7, spawn.getRespawnDelay() / 1000);
  162. insert.setInt(8, spawn.getRespawnMaxDelay() - spawn.getRespawnMinDelay());
  163. insert.setInt(9, spawn.getLocation());
  164. insert.execute();
  165. }
  166. catch (Exception e)
  167. {
  168. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not store spawn in the DB:" + e.getMessage(), e);
  169. }
  170. }
  171. }
  172. public void deleteSpawn(L2Spawn spawn, boolean updateDb)
  173. {
  174. if (!removeSpawn(spawn))
  175. {
  176. return;
  177. }
  178. if (updateDb)
  179. {
  180. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  181. PreparedStatement delete = con.prepareStatement("DELETE FROM " + (spawn.isCustom() ? "custom_spawnlist" : "spawnlist") + " WHERE locx=? AND locy=? AND locz=? AND npc_templateid=? AND heading=?"))
  182. {
  183. delete.setInt(1, spawn.getLocx());
  184. delete.setInt(2, spawn.getLocy());
  185. delete.setInt(3, spawn.getLocz());
  186. delete.setInt(4, spawn.getNpcid());
  187. delete.setInt(5, spawn.getHeading());
  188. delete.execute();
  189. }
  190. catch (Exception e)
  191. {
  192. _log.log(Level.WARNING, getClass().getSimpleName() + ": Spawn " + spawn + " could not be removed from DB: " + e.getMessage(), e);
  193. }
  194. }
  195. }
  196. /**
  197. * Add a spawn to the spawn set if present, otherwise add a spawn set and add the spawn to the newly created spawn set.
  198. * @param spawn the NPC spawn to add
  199. */
  200. private void addSpawn(L2Spawn spawn)
  201. {
  202. if (_spawnTable.containsKey(spawn.getNpcid()))
  203. {
  204. _spawnTable.get(spawn.getNpcid()).add(spawn);
  205. }
  206. else
  207. {
  208. final FastSet<L2Spawn> test = new FastSet<L2Spawn>().shared();
  209. test.add(spawn);
  210. _spawnTable.put(spawn.getNpcid(), test);
  211. }
  212. }
  213. /**
  214. * Remove a spawn from the spawn set, if the spawn set is empty, remove it as well.
  215. * @param spawn the NPC spawn to remove
  216. * @return {@code true} if the spawn was successfully removed, {@code false} otherwise
  217. */
  218. private boolean removeSpawn(L2Spawn spawn)
  219. {
  220. if (_spawnTable.containsKey(spawn.getNpcid()))
  221. {
  222. final Set<L2Spawn> set = _spawnTable.get(spawn.getNpcid());
  223. boolean removed = set.remove(spawn);
  224. if (set.isEmpty())
  225. {
  226. _spawnTable.remove(spawn.getNpcid());
  227. }
  228. return removed;
  229. }
  230. return false;
  231. }
  232. /**
  233. * Execute a procedure over all spawns.<br>
  234. * <font size="4" color="red">Do not use it!</font>
  235. * @param procedure the procedure to execute
  236. * @return {@code true} if all procedures were executed, {@code false} otherwise
  237. */
  238. public boolean forEachSpawn(IL2Procedure<L2Spawn> procedure)
  239. {
  240. for (Set<L2Spawn> set : _spawnTable.values())
  241. {
  242. for (L2Spawn spawn : set)
  243. {
  244. if (!procedure.execute(spawn))
  245. {
  246. return false;
  247. }
  248. }
  249. }
  250. return true;
  251. }
  252. public static SpawnTable getInstance()
  253. {
  254. return SingletonHolder._instance;
  255. }
  256. private static class SingletonHolder
  257. {
  258. protected static final SpawnTable _instance = new SpawnTable();
  259. }
  260. }