2
0

FortSiegeGuardManager.java 4.3 KB

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