2
0

FortSiegeGuardManager.java 4.4 KB

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