2
0

CastleManager.java 7.7 KB

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