FortSiegeGuardManager.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2004-2015 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.model.L2Spawn;
  29. import com.l2jserver.gameserver.model.entity.Fort;
  30. public final class FortSiegeGuardManager
  31. {
  32. private static final Logger _log = Logger.getLogger(FortSiegeGuardManager.class.getName());
  33. private final Fort _fort;
  34. private final FastMap<Integer, FastList<L2Spawn>> _siegeGuards = new FastMap<>();
  35. public FortSiegeGuardManager(Fort fort)
  36. {
  37. _fort = fort;
  38. }
  39. /**
  40. * Spawn guards.
  41. */
  42. public void spawnSiegeGuard()
  43. {
  44. try
  45. {
  46. final FastList<L2Spawn> monsterList = getSiegeGuardSpawn().get(getFort().getResidenceId());
  47. if (monsterList != null)
  48. {
  49. for (L2Spawn spawnDat : monsterList)
  50. {
  51. spawnDat.doSpawn();
  52. if (spawnDat.getRespawnDelay() == 0)
  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. final FastList<L2Spawn> monsterList = getSiegeGuardSpawn().get(getFort().getResidenceId());
  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. PreparedStatement ps = con.prepareStatement("SELECT npcId, x, y, z, heading, respawnDelay FROM fort_siege_guards WHERE fortId = ?"))
  101. {
  102. final int fortId = getFort().getResidenceId();
  103. ps.setInt(1, fortId);
  104. try (ResultSet rs = ps.executeQuery())
  105. {
  106. FastList<L2Spawn> siegeGuardSpawns = new FastList<>();
  107. while (rs.next())
  108. {
  109. final L2Spawn spawn = new L2Spawn(rs.getInt("npcId"));
  110. spawn.setAmount(1);
  111. spawn.setX(rs.getInt("x"));
  112. spawn.setY(rs.getInt("y"));
  113. spawn.setZ(rs.getInt("z"));
  114. spawn.setHeading(rs.getInt("heading"));
  115. spawn.setRespawnDelay(rs.getInt("respawnDelay"));
  116. spawn.setLocationId(0);
  117. siegeGuardSpawns.add(spawn);
  118. }
  119. _siegeGuards.put(fortId, siegeGuardSpawns);
  120. }
  121. }
  122. catch (Exception e)
  123. {
  124. _log.log(Level.WARNING, "Error loading siege guard for fort " + getFort().getName() + ": " + e.getMessage(), e);
  125. }
  126. }
  127. public final Fort getFort()
  128. {
  129. return _fort;
  130. }
  131. public final FastMap<Integer, FastList<L2Spawn>> getSiegeGuardSpawn()
  132. {
  133. return _siegeGuards;
  134. }
  135. }