FortSiegeGuardManager.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.instancemanager;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.logging.Logger;
  20. import com.l2jserver.L2DatabaseFactory;
  21. import com.l2jserver.gameserver.datatables.NpcTable;
  22. import com.l2jserver.gameserver.model.L2Spawn;
  23. import com.l2jserver.gameserver.model.actor.instance.L2FortBallistaInstance;
  24. import com.l2jserver.gameserver.model.entity.Fort;
  25. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  26. import javolution.util.FastList;
  27. import javolution.util.FastMap;
  28. public class FortSiegeGuardManager
  29. {
  30. private static final Logger _log = Logger.getLogger(FortSiegeGuardManager.class.getName());
  31. private Fort _fort;
  32. protected FastMap<Integer, FastList<L2Spawn>> _siegeGuards = new FastMap<Integer, FastList<L2Spawn>>();
  33. protected FastList<L2Spawn> _siegeGuardsSpawns;
  34. public FortSiegeGuardManager(Fort fort)
  35. {
  36. _fort = fort;
  37. }
  38. /**
  39. * Spawn guards.<BR><BR>
  40. */
  41. public void spawnSiegeGuard()
  42. {
  43. try
  44. {
  45. FastList<L2Spawn> monsterList = getSiegeGuardSpawn().get(getFort().getFortId());
  46. if (monsterList != null)
  47. {
  48. for (L2Spawn spawnDat : monsterList)
  49. {
  50. spawnDat.doSpawn();
  51. if (spawnDat.getLastSpawn() instanceof L2FortBallistaInstance)
  52. spawnDat.stopRespawn();
  53. else
  54. spawnDat.startRespawn();
  55. }
  56. }
  57. }
  58. catch (Exception e)
  59. {
  60. _log.warning("Error spawning siege guards for fort " + getFort().getName() + ":" + e.getMessage());
  61. e.printStackTrace();
  62. }
  63. }
  64. /**
  65. * Unspawn guards.<BR><BR>
  66. */
  67. public void unspawnSiegeGuard()
  68. {
  69. try
  70. {
  71. FastList<L2Spawn> monsterList = getSiegeGuardSpawn().get(getFort().getFortId());
  72. if (monsterList != null)
  73. {
  74. for (L2Spawn spawnDat : monsterList)
  75. {
  76. spawnDat.stopRespawn();
  77. spawnDat.getLastSpawn().doDie(spawnDat.getLastSpawn());
  78. }
  79. }
  80. }
  81. catch (Exception e)
  82. {
  83. _log.warning("Error unspawning siege guards for fort " + getFort().getName() + ":" + e.getMessage());
  84. e.printStackTrace();
  85. }
  86. }
  87. /**
  88. * Load guards.<BR><BR>
  89. */
  90. void loadSiegeGuard()
  91. {
  92. _siegeGuards.clear();
  93. Connection con = null;
  94. try
  95. {
  96. con = L2DatabaseFactory.getInstance().getConnection();
  97. PreparedStatement statement = con.prepareStatement("SELECT * FROM fort_siege_guards Where fortId = ? ");
  98. statement.setInt(1, getFort().getFortId());
  99. ResultSet rs = statement.executeQuery();
  100. L2Spawn spawn1;
  101. L2NpcTemplate template1;
  102. _siegeGuardsSpawns = new FastList<L2Spawn>();
  103. while (rs.next())
  104. {
  105. int fortId = rs.getInt("fortId");
  106. template1 = NpcTable.getInstance().getTemplate(rs.getInt("npcId"));
  107. if (template1 != null)
  108. {
  109. spawn1 = new L2Spawn(template1);
  110. spawn1.setId(rs.getInt("id"));
  111. spawn1.setAmount(1);
  112. spawn1.setLocx(rs.getInt("x"));
  113. spawn1.setLocy(rs.getInt("y"));
  114. spawn1.setLocz(rs.getInt("z"));
  115. spawn1.setHeading(rs.getInt("heading"));
  116. spawn1.setRespawnDelay(rs.getInt("respawnDelay"));
  117. spawn1.setLocation(0);
  118. _siegeGuardsSpawns.add(spawn1);
  119. }
  120. else
  121. {
  122. _log.warning("Missing npc data in npc table for id: " + rs.getInt("npcId"));
  123. }
  124. _siegeGuards.put(fortId, _siegeGuardsSpawns);
  125. }
  126. rs.close();
  127. statement.close();
  128. }
  129. catch (Exception e1)
  130. {
  131. _log.warning("Error loading siege guard for fort " + getFort().getName() + ":" + e1);
  132. e1.printStackTrace();
  133. }
  134. finally
  135. {
  136. try
  137. {
  138. con.close();
  139. }
  140. catch (Exception e)
  141. {
  142. _log.warning("" + e.getMessage());
  143. e.printStackTrace();
  144. }
  145. }
  146. }
  147. public final Fort getFort()
  148. {
  149. return _fort;
  150. }
  151. public final FastMap<Integer, FastList<L2Spawn>> getSiegeGuardSpawn()
  152. {
  153. return _siegeGuards;
  154. }
  155. }