CastleManager.java 7.6 KB

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