2
0

CastleManager.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright (C) 2004-2014 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.Map;
  26. import java.util.concurrent.ConcurrentHashMap;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import javolution.util.FastList;
  30. import com.l2jserver.L2DatabaseFactory;
  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 List<Castle> _castles;
  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 < getCastles().size(); i++)
  69. {
  70. castle = getCastles().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 : getCastles())
  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 : getCastles())
  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 : getCastles())
  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 : getCastles())
  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 < getCastles().size(); i++)
  137. {
  138. castle = getCastles().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 < getCastles().size(); i++)
  154. {
  155. castle = getCastles().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. if (_castles == null)
  166. {
  167. _castles = new FastList<>();
  168. }
  169. return _castles;
  170. }
  171. public boolean hasOwnedCastle()
  172. {
  173. boolean hasOwnedCastle = false;
  174. for (Castle castle : getCastles())
  175. {
  176. if (castle.getOwnerId() > 0)
  177. {
  178. hasOwnedCastle = true;
  179. break;
  180. }
  181. }
  182. return hasOwnedCastle;
  183. }
  184. public final void validateTaxes(int sealStrifeOwner)
  185. {
  186. int maxTax;
  187. switch (sealStrifeOwner)
  188. {
  189. case SevenSigns.CABAL_DUSK:
  190. maxTax = 5;
  191. break;
  192. case SevenSigns.CABAL_DAWN:
  193. maxTax = 25;
  194. break;
  195. default: // no owner
  196. maxTax = 15;
  197. break;
  198. }
  199. for (Castle castle : _castles)
  200. {
  201. if (castle.getTaxPercent() > maxTax)
  202. {
  203. castle.setTaxPercent(maxTax);
  204. }
  205. }
  206. }
  207. public int getCirclet()
  208. {
  209. return getCircletByCastleId(1);
  210. }
  211. public int getCircletByCastleId(int castleId)
  212. {
  213. if ((castleId > 0) && (castleId < 10))
  214. {
  215. return _castleCirclets[castleId];
  216. }
  217. return 0;
  218. }
  219. // remove this castle's circlets from the clan
  220. public void removeCirclet(L2Clan clan, int castleId)
  221. {
  222. for (L2ClanMember member : clan.getMembers())
  223. {
  224. removeCirclet(member, castleId);
  225. }
  226. }
  227. public void removeCirclet(L2ClanMember member, int castleId)
  228. {
  229. if (member == null)
  230. {
  231. return;
  232. }
  233. L2PcInstance player = member.getPlayerInstance();
  234. int circletId = getCircletByCastleId(castleId);
  235. if (circletId != 0)
  236. {
  237. // online-player circlet removal
  238. if (player != null)
  239. {
  240. try
  241. {
  242. L2ItemInstance circlet = player.getInventory().getItemByItemId(circletId);
  243. if (circlet != null)
  244. {
  245. if (circlet.isEquipped())
  246. {
  247. player.getInventory().unEquipItemInSlot(circlet.getLocationSlot());
  248. }
  249. player.destroyItemByItemId("CastleCircletRemoval", circletId, 1, player, true);
  250. }
  251. return;
  252. }
  253. catch (NullPointerException e)
  254. {
  255. // continue removing offline
  256. }
  257. }
  258. // else offline-player circlet removal
  259. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  260. PreparedStatement ps = con.prepareStatement("DELETE FROM items WHERE owner_id = ? and item_id = ?"))
  261. {
  262. ps.setInt(1, member.getObjectId());
  263. ps.setInt(2, circletId);
  264. ps.execute();
  265. }
  266. catch (Exception e)
  267. {
  268. _log.log(Level.WARNING, "Failed to remove castle circlets offline for player " + member.getName() + ": " + e.getMessage(), e);
  269. }
  270. }
  271. }
  272. @Override
  273. public void loadInstances()
  274. {
  275. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  276. Statement s = con.createStatement();
  277. ResultSet rs = s.executeQuery("SELECT id FROM castle ORDER BY id"))
  278. {
  279. while (rs.next())
  280. {
  281. getCastles().add(new Castle(rs.getInt("id")));
  282. }
  283. _log.info(getClass().getSimpleName() + ": Loaded: " + getCastles().size() + " castles");
  284. }
  285. catch (Exception e)
  286. {
  287. _log.log(Level.WARNING, "Exception: loadCastleData(): " + e.getMessage(), e);
  288. }
  289. }
  290. @Override
  291. public void updateReferences()
  292. {
  293. }
  294. @Override
  295. public void activateInstances()
  296. {
  297. for (final Castle castle : _castles)
  298. {
  299. castle.activateInstance();
  300. }
  301. }
  302. public void registerSiegeDate(int castleId, long siegeDate)
  303. {
  304. _castleSiegeDate.put(castleId, siegeDate);
  305. }
  306. public int getSiegeDates(long siegeDate)
  307. {
  308. int count = 0;
  309. for (long date : _castleSiegeDate.values())
  310. {
  311. if (Math.abs(date - siegeDate) < 1000)
  312. {
  313. count++;
  314. }
  315. }
  316. return count;
  317. }
  318. public static final CastleManager getInstance()
  319. {
  320. return SingletonHolder._instance;
  321. }
  322. private static class SingletonHolder
  323. {
  324. protected static final CastleManager _instance = new CastleManager();
  325. }
  326. }