CastleManager.java 7.9 KB

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