CastleManager.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.instancemanager;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.Statement;
  24. import java.util.List;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import javolution.util.FastList;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.InstanceListManager;
  30. import com.l2jserver.gameserver.SevenSigns;
  31. import com.l2jserver.gameserver.model.L2Clan;
  32. import com.l2jserver.gameserver.model.L2ClanMember;
  33. import com.l2jserver.gameserver.model.L2Object;
  34. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  35. import com.l2jserver.gameserver.model.entity.Castle;
  36. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  37. public final class CastleManager implements InstanceListManager
  38. {
  39. private static final Logger _log = Logger.getLogger(CastleManager.class.getName());
  40. private List<Castle> _castles;
  41. private static final int _castleCirclets[] =
  42. {
  43. 0,
  44. 6838,
  45. 6835,
  46. 6839,
  47. 6837,
  48. 6840,
  49. 6834,
  50. 6836,
  51. 8182,
  52. 8183
  53. };
  54. public final int findNearestCastleIndex(L2Object obj)
  55. {
  56. return findNearestCastleIndex(obj, Long.MAX_VALUE);
  57. }
  58. public final int findNearestCastleIndex(L2Object obj, long maxDistance)
  59. {
  60. int index = getCastleIndex(obj);
  61. if (index < 0)
  62. {
  63. double distance;
  64. Castle castle;
  65. for (int i = 0; i < getCastles().size(); i++)
  66. {
  67. castle = getCastles().get(i);
  68. if (castle == null)
  69. {
  70. continue;
  71. }
  72. distance = castle.getDistance(obj);
  73. if (maxDistance > distance)
  74. {
  75. maxDistance = (long) distance;
  76. index = i;
  77. }
  78. }
  79. }
  80. return index;
  81. }
  82. public final Castle getCastleById(int castleId)
  83. {
  84. for (Castle temp : getCastles())
  85. {
  86. if (temp.getCastleId() == castleId)
  87. {
  88. return temp;
  89. }
  90. }
  91. return null;
  92. }
  93. public final Castle getCastleByOwner(L2Clan clan)
  94. {
  95. for (Castle temp : getCastles())
  96. {
  97. if (temp.getOwnerId() == clan.getClanId())
  98. {
  99. return temp;
  100. }
  101. }
  102. return null;
  103. }
  104. public final Castle getCastle(String name)
  105. {
  106. for (Castle temp : getCastles())
  107. {
  108. if (temp.getName().equalsIgnoreCase(name.trim()))
  109. {
  110. return temp;
  111. }
  112. }
  113. return null;
  114. }
  115. public final Castle getCastle(int x, int y, int z)
  116. {
  117. for (Castle temp : getCastles())
  118. {
  119. if (temp.checkIfInZone(x, y, z))
  120. {
  121. return temp;
  122. }
  123. }
  124. return null;
  125. }
  126. public final Castle getCastle(L2Object activeObject)
  127. {
  128. return getCastle(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  129. }
  130. public final int getCastleIndex(int castleId)
  131. {
  132. Castle castle;
  133. for (int i = 0; i < getCastles().size(); i++)
  134. {
  135. castle = getCastles().get(i);
  136. if ((castle != null) && (castle.getCastleId() == castleId))
  137. {
  138. return i;
  139. }
  140. }
  141. return -1;
  142. }
  143. public final int getCastleIndex(L2Object activeObject)
  144. {
  145. return getCastleIndex(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  146. }
  147. public final int getCastleIndex(int x, int y, int z)
  148. {
  149. Castle castle;
  150. for (int i = 0; i < getCastles().size(); i++)
  151. {
  152. castle = getCastles().get(i);
  153. if ((castle != null) && castle.checkIfInZone(x, y, z))
  154. {
  155. return i;
  156. }
  157. }
  158. return -1;
  159. }
  160. public final List<Castle> getCastles()
  161. {
  162. if (_castles == null)
  163. {
  164. _castles = new FastList<>();
  165. }
  166. return _castles;
  167. }
  168. public final void validateTaxes(int sealStrifeOwner)
  169. {
  170. int maxTax;
  171. switch (sealStrifeOwner)
  172. {
  173. case SevenSigns.CABAL_DUSK:
  174. maxTax = 5;
  175. break;
  176. case SevenSigns.CABAL_DAWN:
  177. maxTax = 25;
  178. break;
  179. default: // no owner
  180. maxTax = 15;
  181. break;
  182. }
  183. for (Castle castle : _castles)
  184. {
  185. if (castle.getTaxPercent() > maxTax)
  186. {
  187. castle.setTaxPercent(maxTax);
  188. }
  189. }
  190. }
  191. public int getCirclet()
  192. {
  193. return getCircletByCastleId(1);
  194. }
  195. public int getCircletByCastleId(int castleId)
  196. {
  197. if ((castleId > 0) && (castleId < 10))
  198. {
  199. return _castleCirclets[castleId];
  200. }
  201. return 0;
  202. }
  203. // remove this castle's circlets from the clan
  204. public void removeCirclet(L2Clan clan, int castleId)
  205. {
  206. for (L2ClanMember member : clan.getMembers())
  207. {
  208. removeCirclet(member, castleId);
  209. }
  210. }
  211. public void removeCirclet(L2ClanMember member, int castleId)
  212. {
  213. if (member == null)
  214. {
  215. return;
  216. }
  217. L2PcInstance player = member.getPlayerInstance();
  218. int circletId = getCircletByCastleId(castleId);
  219. if (circletId != 0)
  220. {
  221. // online-player circlet removal
  222. if (player != null)
  223. {
  224. try
  225. {
  226. L2ItemInstance circlet = player.getInventory().getItemByItemId(circletId);
  227. if (circlet != null)
  228. {
  229. if (circlet.isEquipped())
  230. {
  231. player.getInventory().unEquipItemInSlot(circlet.getLocationSlot());
  232. }
  233. player.destroyItemByItemId("CastleCircletRemoval", circletId, 1, player, true);
  234. }
  235. return;
  236. }
  237. catch (NullPointerException e)
  238. {
  239. // continue removing offline
  240. }
  241. }
  242. // else offline-player circlet removal
  243. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  244. PreparedStatement ps = con.prepareStatement("DELETE FROM items WHERE owner_id = ? and item_id = ?"))
  245. {
  246. ps.setInt(1, member.getObjectId());
  247. ps.setInt(2, circletId);
  248. ps.execute();
  249. }
  250. catch (Exception e)
  251. {
  252. _log.log(Level.WARNING, "Failed to remove castle circlets offline for player " + member.getName() + ": " + e.getMessage(), e);
  253. }
  254. }
  255. }
  256. @Override
  257. public void loadInstances()
  258. {
  259. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  260. Statement s = con.createStatement();
  261. ResultSet rs = s.executeQuery("SELECT id FROM castle ORDER BY id"))
  262. {
  263. while (rs.next())
  264. {
  265. getCastles().add(new Castle(rs.getInt("id")));
  266. }
  267. _log.info(getClass().getSimpleName() + ": Loaded: " + getCastles().size() + " castles");
  268. }
  269. catch (Exception e)
  270. {
  271. _log.log(Level.WARNING, "Exception: loadCastleData(): " + e.getMessage(), e);
  272. }
  273. }
  274. @Override
  275. public void updateReferences()
  276. {
  277. }
  278. @Override
  279. public void activateInstances()
  280. {
  281. for (final Castle castle : _castles)
  282. {
  283. castle.activateInstance();
  284. }
  285. }
  286. public static final CastleManager getInstance()
  287. {
  288. return SingletonHolder._instance;
  289. }
  290. private static class SingletonHolder
  291. {
  292. protected static final CastleManager _instance = new CastleManager();
  293. }
  294. }