CastleManager.java 7.7 KB

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