FortSiegeGuardManager.java 4.3 KB

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