CastleManager.java 7.2 KB

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