CastleManager.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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.List;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javolution.util.FastList;
  23. import com.l2jserver.L2DatabaseFactory;
  24. import com.l2jserver.gameserver.InstanceListManager;
  25. import com.l2jserver.gameserver.SevenSigns;
  26. import com.l2jserver.gameserver.model.L2Clan;
  27. import com.l2jserver.gameserver.model.L2ClanMember;
  28. import com.l2jserver.gameserver.model.L2Object;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.model.entity.Castle;
  31. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  32. public class CastleManager implements InstanceListManager
  33. {
  34. protected static final Logger _log = Logger.getLogger(CastleManager.class.getName());
  35. public static final CastleManager getInstance()
  36. {
  37. return SingletonHolder._instance;
  38. }
  39. private List<Castle> _castles;
  40. private static final int _castleCirclets[] = { 0, 6838, 6835, 6839, 6837, 6840, 6834, 6836, 8182, 8183 };
  41. public final int findNearestCastleIndex(L2Object obj)
  42. {
  43. return findNearestCastleIndex(obj, Long.MAX_VALUE);
  44. }
  45. public final int findNearestCastleIndex(L2Object obj, long maxDistance)
  46. {
  47. int index = getCastleIndex(obj);
  48. if (index < 0)
  49. {
  50. double distance;
  51. Castle castle;
  52. for (int i = 0; i < getCastles().size(); i++)
  53. {
  54. castle = getCastles().get(i);
  55. if (castle == null)
  56. continue;
  57. distance = castle.getDistance(obj);
  58. if (maxDistance > distance)
  59. {
  60. maxDistance = (long) distance;
  61. index = i;
  62. }
  63. }
  64. }
  65. return index;
  66. }
  67. public final Castle getCastleById(int castleId)
  68. {
  69. for (Castle temp : getCastles())
  70. {
  71. if (temp.getCastleId() == castleId)
  72. return temp;
  73. }
  74. return null;
  75. }
  76. public final Castle getCastleByOwner(L2Clan clan)
  77. {
  78. for (Castle temp : getCastles())
  79. {
  80. if (temp.getOwnerId() == clan.getClanId())
  81. return temp;
  82. }
  83. return null;
  84. }
  85. public final Castle getCastle(String name)
  86. {
  87. for (Castle temp : getCastles())
  88. {
  89. if (temp.getName().equalsIgnoreCase(name.trim()))
  90. return temp;
  91. }
  92. return null;
  93. }
  94. public final Castle getCastle(int x, int y, int z)
  95. {
  96. for (Castle temp : getCastles())
  97. {
  98. if (temp.checkIfInZone(x, y, z))
  99. return temp;
  100. }
  101. return null;
  102. }
  103. public final Castle getCastle(L2Object activeObject)
  104. {
  105. return getCastle(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  106. }
  107. public final int getCastleIndex(int castleId)
  108. {
  109. Castle castle;
  110. for (int i = 0; i < getCastles().size(); i++)
  111. {
  112. castle = getCastles().get(i);
  113. if (castle != null && castle.getCastleId() == castleId)
  114. return i;
  115. }
  116. return -1;
  117. }
  118. public final int getCastleIndex(L2Object activeObject)
  119. {
  120. return getCastleIndex(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  121. }
  122. public final int getCastleIndex(int x, int y, int z)
  123. {
  124. Castle castle;
  125. for (int i = 0; i < getCastles().size(); i++)
  126. {
  127. castle = getCastles().get(i);
  128. if (castle != null && castle.checkIfInZone(x, y, z))
  129. return i;
  130. }
  131. return -1;
  132. }
  133. public final List<Castle> getCastles()
  134. {
  135. if (_castles == null)
  136. _castles = new FastList<>();
  137. return _castles;
  138. }
  139. public final void validateTaxes(int sealStrifeOwner)
  140. {
  141. int maxTax;
  142. switch (sealStrifeOwner)
  143. {
  144. case SevenSigns.CABAL_DUSK:
  145. maxTax = 5;
  146. break;
  147. case SevenSigns.CABAL_DAWN:
  148. maxTax = 25;
  149. break;
  150. default: // no owner
  151. maxTax = 15;
  152. break;
  153. }
  154. for (Castle castle : _castles)
  155. if (castle.getTaxPercent() > maxTax)
  156. castle.setTaxPercent(maxTax);
  157. }
  158. int _castleId = 1; // from this castle
  159. public int getCirclet()
  160. {
  161. return getCircletByCastleId(_castleId);
  162. }
  163. public int getCircletByCastleId(int castleId)
  164. {
  165. if (castleId > 0 && castleId < 10)
  166. return _castleCirclets[castleId];
  167. return 0;
  168. }
  169. // remove this castle's circlets from the clan
  170. public void removeCirclet(L2Clan clan, int castleId)
  171. {
  172. for (L2ClanMember member : clan.getMembers())
  173. removeCirclet(member, castleId);
  174. }
  175. public void removeCirclet(L2ClanMember member, int castleId)
  176. {
  177. if (member == null)
  178. return;
  179. L2PcInstance player = member.getPlayerInstance();
  180. int circletId = getCircletByCastleId(castleId);
  181. if (circletId != 0)
  182. {
  183. // online-player circlet removal
  184. if (player != null)
  185. {
  186. try
  187. {
  188. L2ItemInstance circlet = player.getInventory().getItemByItemId(circletId);
  189. if (circlet != null)
  190. {
  191. if (circlet.isEquipped())
  192. player.getInventory().unEquipItemInSlot(circlet.getLocationSlot());
  193. player.destroyItemByItemId("CastleCircletRemoval", circletId, 1, player, true);
  194. }
  195. return;
  196. }
  197. catch (NullPointerException e)
  198. {
  199. // continue removing offline
  200. }
  201. }
  202. // else offline-player circlet removal
  203. Connection con = null;
  204. try
  205. {
  206. con = L2DatabaseFactory.getInstance().getConnection();
  207. PreparedStatement statement = con.prepareStatement("DELETE FROM items WHERE owner_id = ? and item_id = ?");
  208. statement.setInt(1, member.getObjectId());
  209. statement.setInt(2, circletId);
  210. statement.execute();
  211. statement.close();
  212. }
  213. catch (Exception e)
  214. {
  215. _log.log(Level.WARNING, "Failed to remove castle circlets offline for player " + member.getName() + ": " + e.getMessage(), e);
  216. }
  217. finally
  218. {
  219. L2DatabaseFactory.close(con);
  220. }
  221. }
  222. }
  223. @Override
  224. public void loadInstances()
  225. {
  226. Connection con = null;
  227. try
  228. {
  229. con = L2DatabaseFactory.getInstance().getConnection();
  230. PreparedStatement statement = con.prepareStatement("SELECT id FROM castle ORDER BY id");
  231. ResultSet rs = statement.executeQuery();
  232. while (rs.next())
  233. {
  234. getCastles().add(new Castle(rs.getInt("id")));
  235. }
  236. rs.close();
  237. statement.close();
  238. _log.info("Loaded: " + getCastles().size() + " castles");
  239. }
  240. catch (Exception e)
  241. {
  242. _log.log(Level.WARNING, "Exception: loadCastleData(): " + e.getMessage(), e);
  243. }
  244. finally
  245. {
  246. L2DatabaseFactory.close(con);
  247. }
  248. }
  249. @Override
  250. public void updateReferences()
  251. {
  252. }
  253. @Override
  254. public void activateInstances()
  255. {
  256. for (final Castle castle : _castles)
  257. {
  258. castle.activateInstance();
  259. }
  260. }
  261. private static class SingletonHolder
  262. {
  263. protected static final CastleManager _instance = new CastleManager();
  264. }
  265. }