CastleManager.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 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. int _castleId = 1; // from this castle
  192. public int getCirclet()
  193. {
  194. return getCircletByCastleId(_castleId);
  195. }
  196. public int getCircletByCastleId(int castleId)
  197. {
  198. if ((castleId > 0) && (castleId < 10))
  199. {
  200. return _castleCirclets[castleId];
  201. }
  202. return 0;
  203. }
  204. // remove this castle's circlets from the clan
  205. public void removeCirclet(L2Clan clan, int castleId)
  206. {
  207. for (L2ClanMember member : clan.getMembers())
  208. {
  209. removeCirclet(member, castleId);
  210. }
  211. }
  212. public void removeCirclet(L2ClanMember member, int castleId)
  213. {
  214. if (member == null)
  215. {
  216. return;
  217. }
  218. L2PcInstance player = member.getPlayerInstance();
  219. int circletId = getCircletByCastleId(castleId);
  220. if (circletId != 0)
  221. {
  222. // online-player circlet removal
  223. if (player != null)
  224. {
  225. try
  226. {
  227. L2ItemInstance circlet = player.getInventory().getItemByItemId(circletId);
  228. if (circlet != null)
  229. {
  230. if (circlet.isEquipped())
  231. {
  232. player.getInventory().unEquipItemInSlot(circlet.getLocationSlot());
  233. }
  234. player.destroyItemByItemId("CastleCircletRemoval", circletId, 1, player, true);
  235. }
  236. return;
  237. }
  238. catch (NullPointerException e)
  239. {
  240. // continue removing offline
  241. }
  242. }
  243. // else offline-player circlet removal
  244. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  245. PreparedStatement ps = con.prepareStatement("DELETE FROM items WHERE owner_id = ? and item_id = ?"))
  246. {
  247. ps.setInt(1, member.getObjectId());
  248. ps.setInt(2, circletId);
  249. ps.execute();
  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. Statement s = con.createStatement();
  262. ResultSet rs = s.executeQuery("SELECT id FROM castle ORDER BY id"))
  263. {
  264. while (rs.next())
  265. {
  266. getCastles().add(new Castle(rs.getInt("id")));
  267. }
  268. _log.info(getClass().getSimpleName() + ": Loaded: " + getCastles().size() + " castles");
  269. }
  270. catch (Exception e)
  271. {
  272. _log.log(Level.WARNING, "Exception: loadCastleData(): " + e.getMessage(), e);
  273. }
  274. }
  275. @Override
  276. public void updateReferences()
  277. {
  278. }
  279. @Override
  280. public void activateInstances()
  281. {
  282. for (final Castle castle : _castles)
  283. {
  284. castle.activateInstance();
  285. }
  286. }
  287. public static final CastleManager getInstance()
  288. {
  289. return SingletonHolder._instance;
  290. }
  291. private static class SingletonHolder
  292. {
  293. protected static final CastleManager _instance = new CastleManager();
  294. }
  295. }