FortSiegeGuardManager.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 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.<BR><BR>
  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. spawnDat.stopRespawn();
  54. else
  55. spawnDat.startRespawn();
  56. }
  57. }
  58. }
  59. catch (Exception e)
  60. {
  61. _log.log(Level.WARNING, "Error spawning siege guards for fort " + getFort().getName() + ":" + e.getMessage(), e);
  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. if (spawnDat.getLastSpawn() != null)
  78. spawnDat.getLastSpawn().doDie(spawnDat.getLastSpawn());
  79. }
  80. }
  81. }
  82. catch (Exception e)
  83. {
  84. _log.log(Level.WARNING, "Error unspawning siege guards for fort " + getFort().getName() + ":" + e.getMessage(), e);
  85. }
  86. }
  87. /**
  88. * Load guards.<BR><BR>
  89. */
  90. void loadSiegeGuard()
  91. {
  92. _siegeGuards.clear();
  93. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  94. {
  95. PreparedStatement statement = con.prepareStatement("SELECT * FROM fort_siege_guards Where fortId = ? ");
  96. statement.setInt(1, getFort().getFortId());
  97. ResultSet rs = statement.executeQuery();
  98. L2Spawn spawn1;
  99. L2NpcTemplate template1;
  100. _siegeGuardsSpawns = new FastList<>();
  101. while (rs.next())
  102. {
  103. int fortId = rs.getInt("fortId");
  104. template1 = NpcTable.getInstance().getTemplate(rs.getInt("npcId"));
  105. if (template1 != null)
  106. {
  107. spawn1 = new L2Spawn(template1);
  108. spawn1.setAmount(1);
  109. spawn1.setLocx(rs.getInt("x"));
  110. spawn1.setLocy(rs.getInt("y"));
  111. spawn1.setLocz(rs.getInt("z"));
  112. spawn1.setHeading(rs.getInt("heading"));
  113. spawn1.setRespawnDelay(rs.getInt("respawnDelay"));
  114. spawn1.setLocation(0);
  115. _siegeGuardsSpawns.add(spawn1);
  116. }
  117. else
  118. {
  119. _log.warning("Missing npc data in npc table for id: " + rs.getInt("npcId"));
  120. }
  121. _siegeGuards.put(fortId, _siegeGuardsSpawns);
  122. }
  123. rs.close();
  124. statement.close();
  125. }
  126. catch (Exception e)
  127. {
  128. _log.log(Level.WARNING, "Error loading siege guard for fort " + getFort().getName() + ": " + e.getMessage(), e);
  129. }
  130. }
  131. public final Fort getFort()
  132. {
  133. return _fort;
  134. }
  135. public final FastMap<Integer, FastList<L2Spawn>> getSiegeGuardSpawn()
  136. {
  137. return _siegeGuards;
  138. }
  139. }