CastleManager.java 7.3 KB

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