CastleManager.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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[] =
  41. {
  42. 0,
  43. 6838,
  44. 6835,
  45. 6839,
  46. 6837,
  47. 6840,
  48. 6834,
  49. 6836,
  50. 8182,
  51. 8183
  52. };
  53. public final int findNearestCastleIndex(L2Object obj)
  54. {
  55. return findNearestCastleIndex(obj, Long.MAX_VALUE);
  56. }
  57. public final int findNearestCastleIndex(L2Object obj, long maxDistance)
  58. {
  59. int index = getCastleIndex(obj);
  60. if (index < 0)
  61. {
  62. double distance;
  63. Castle castle;
  64. for (int i = 0; i < getCastles().size(); i++)
  65. {
  66. castle = getCastles().get(i);
  67. if (castle == null)
  68. {
  69. continue;
  70. }
  71. distance = castle.getDistance(obj);
  72. if (maxDistance > distance)
  73. {
  74. maxDistance = (long) distance;
  75. index = i;
  76. }
  77. }
  78. }
  79. return index;
  80. }
  81. public final Castle getCastleById(int castleId)
  82. {
  83. for (Castle temp : getCastles())
  84. {
  85. if (temp.getCastleId() == castleId)
  86. {
  87. return temp;
  88. }
  89. }
  90. return null;
  91. }
  92. public final Castle getCastleByOwner(L2Clan clan)
  93. {
  94. for (Castle temp : getCastles())
  95. {
  96. if (temp.getOwnerId() == clan.getClanId())
  97. {
  98. return temp;
  99. }
  100. }
  101. return null;
  102. }
  103. public final Castle getCastle(String name)
  104. {
  105. for (Castle temp : getCastles())
  106. {
  107. if (temp.getName().equalsIgnoreCase(name.trim()))
  108. {
  109. return temp;
  110. }
  111. }
  112. return null;
  113. }
  114. public final Castle getCastle(int x, int y, int z)
  115. {
  116. for (Castle temp : getCastles())
  117. {
  118. if (temp.checkIfInZone(x, y, z))
  119. {
  120. return temp;
  121. }
  122. }
  123. return null;
  124. }
  125. public final Castle getCastle(L2Object activeObject)
  126. {
  127. return getCastle(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  128. }
  129. public final int getCastleIndex(int castleId)
  130. {
  131. Castle castle;
  132. for (int i = 0; i < getCastles().size(); i++)
  133. {
  134. castle = getCastles().get(i);
  135. if ((castle != null) && (castle.getCastleId() == castleId))
  136. {
  137. return i;
  138. }
  139. }
  140. return -1;
  141. }
  142. public final int getCastleIndex(L2Object activeObject)
  143. {
  144. return getCastleIndex(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  145. }
  146. public final int getCastleIndex(int x, int y, int z)
  147. {
  148. Castle castle;
  149. for (int i = 0; i < getCastles().size(); i++)
  150. {
  151. castle = getCastles().get(i);
  152. if ((castle != null) && castle.checkIfInZone(x, y, z))
  153. {
  154. return i;
  155. }
  156. }
  157. return -1;
  158. }
  159. public final List<Castle> getCastles()
  160. {
  161. if (_castles == null)
  162. {
  163. _castles = new FastList<>();
  164. }
  165. return _castles;
  166. }
  167. public final void validateTaxes(int sealStrifeOwner)
  168. {
  169. int maxTax;
  170. switch (sealStrifeOwner)
  171. {
  172. case SevenSigns.CABAL_DUSK:
  173. maxTax = 5;
  174. break;
  175. case SevenSigns.CABAL_DAWN:
  176. maxTax = 25;
  177. break;
  178. default: // no owner
  179. maxTax = 15;
  180. break;
  181. }
  182. for (Castle castle : _castles)
  183. {
  184. if (castle.getTaxPercent() > maxTax)
  185. {
  186. castle.setTaxPercent(maxTax);
  187. }
  188. }
  189. }
  190. int _castleId = 1; // from this castle
  191. public int getCirclet()
  192. {
  193. return getCircletByCastleId(_castleId);
  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. {
  245. PreparedStatement statement = con.prepareStatement("DELETE FROM items WHERE owner_id = ? and item_id = ?");
  246. statement.setInt(1, member.getObjectId());
  247. statement.setInt(2, circletId);
  248. statement.execute();
  249. statement.close();
  250. }
  251. catch (Exception e)
  252. {
  253. _log.log(Level.WARNING, "Failed to remove castle circlets offline for player " + member.getName() + ": " + e.getMessage(), e);
  254. }
  255. }
  256. }
  257. @Override
  258. public void loadInstances()
  259. {
  260. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  261. {
  262. PreparedStatement statement = con.prepareStatement("SELECT id FROM castle ORDER BY id");
  263. ResultSet rs = statement.executeQuery();
  264. while (rs.next())
  265. {
  266. getCastles().add(new Castle(rs.getInt("id")));
  267. }
  268. rs.close();
  269. statement.close();
  270. _log.info(getClass().getSimpleName() + ": Loaded: " + getCastles().size() + " castles");
  271. }
  272. catch (Exception e)
  273. {
  274. _log.log(Level.WARNING, "Exception: loadCastleData(): " + e.getMessage(), e);
  275. }
  276. }
  277. @Override
  278. public void updateReferences()
  279. {
  280. }
  281. @Override
  282. public void activateInstances()
  283. {
  284. for (final Castle castle : _castles)
  285. {
  286. castle.activateInstance();
  287. }
  288. }
  289. private static class SingletonHolder
  290. {
  291. protected static final CastleManager _instance = new CastleManager();
  292. }
  293. }