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.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  30. import com.l2jserver.gameserver.model.L2Spawn;
  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 Map<Integer, List<L2Spawn>> _siegeGuards = new HashMap<>();
  37. public FortSiegeGuardManager(Fort fort)
  38. {
  39. _fort = fort;
  40. }
  41. /**
  42. * Spawn guards.
  43. */
  44. public void spawnSiegeGuard()
  45. {
  46. try
  47. {
  48. final List<L2Spawn> monsterList = _siegeGuards.get(getFort().getResidenceId());
  49. if (monsterList != null)
  50. {
  51. for (L2Spawn spawnDat : monsterList)
  52. {
  53. spawnDat.doSpawn();
  54. if (spawnDat.getRespawnDelay() == 0)
  55. {
  56. spawnDat.stopRespawn();
  57. }
  58. else
  59. {
  60. spawnDat.startRespawn();
  61. }
  62. }
  63. }
  64. }
  65. catch (Exception e)
  66. {
  67. _log.log(Level.WARNING, "Error spawning siege guards for fort " + getFort().getName() + ":" + e.getMessage(), e);
  68. }
  69. }
  70. /**
  71. * Unspawn guards.
  72. */
  73. public void unspawnSiegeGuard()
  74. {
  75. try
  76. {
  77. final List<L2Spawn> monsterList = _siegeGuards.get(getFort().getResidenceId());
  78. if (monsterList != null)
  79. {
  80. for (L2Spawn spawnDat : monsterList)
  81. {
  82. spawnDat.stopRespawn();
  83. if (spawnDat.getLastSpawn() != null)
  84. {
  85. spawnDat.getLastSpawn().doDie(spawnDat.getLastSpawn());
  86. }
  87. }
  88. }
  89. }
  90. catch (Exception e)
  91. {
  92. _log.log(Level.WARNING, "Error unspawning siege guards for fort " + getFort().getName() + ":" + e.getMessage(), e);
  93. }
  94. }
  95. /**
  96. * Load guards.
  97. */
  98. void loadSiegeGuard()
  99. {
  100. _siegeGuards.clear();
  101. try (Connection con = ConnectionFactory.getInstance().getConnection();
  102. PreparedStatement ps = con.prepareStatement("SELECT npcId, x, y, z, heading, respawnDelay FROM fort_siege_guards WHERE fortId = ?"))
  103. {
  104. final int fortId = getFort().getResidenceId();
  105. ps.setInt(1, fortId);
  106. try (ResultSet rs = ps.executeQuery())
  107. {
  108. final List<L2Spawn> siegeGuardSpawns = new ArrayList<>();
  109. while (rs.next())
  110. {
  111. final L2Spawn spawn = new L2Spawn(rs.getInt("npcId"));
  112. spawn.setAmount(1);
  113. spawn.setX(rs.getInt("x"));
  114. spawn.setY(rs.getInt("y"));
  115. spawn.setZ(rs.getInt("z"));
  116. spawn.setHeading(rs.getInt("heading"));
  117. spawn.setRespawnDelay(rs.getInt("respawnDelay"));
  118. spawn.setLocationId(0);
  119. siegeGuardSpawns.add(spawn);
  120. }
  121. _siegeGuards.put(fortId, siegeGuardSpawns);
  122. }
  123. }
  124. catch (Exception e)
  125. {
  126. _log.log(Level.WARNING, "Error loading siege guard for fort " + getFort().getName() + ": " + e.getMessage(), e);
  127. }
  128. }
  129. public final Fort getFort()
  130. {
  131. return _fort;
  132. }
  133. public final Map<Integer, List<L2Spawn>> getSiegeGuardSpawn()
  134. {
  135. return _siegeGuards;
  136. }
  137. }