ClanHallManager.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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.ResultSet;
  18. import java.sql.Statement;
  19. import java.util.Map;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javolution.util.FastMap;
  23. import com.l2jserver.L2DatabaseFactory;
  24. import com.l2jserver.gameserver.datatables.ClanTable;
  25. import com.l2jserver.gameserver.model.L2Clan;
  26. import com.l2jserver.gameserver.model.L2Object;
  27. import com.l2jserver.gameserver.model.StatsSet;
  28. import com.l2jserver.gameserver.model.entity.Auction;
  29. import com.l2jserver.gameserver.model.entity.ClanHall;
  30. import com.l2jserver.gameserver.model.entity.clanhall.AuctionableHall;
  31. import com.l2jserver.gameserver.model.entity.clanhall.SiegableHall;
  32. import com.l2jserver.gameserver.model.zone.type.L2ClanHallZone;
  33. /**
  34. * @author Steuf
  35. */
  36. public final class ClanHallManager
  37. {
  38. protected static final Logger _log = Logger.getLogger(ClanHallManager.class.getName());
  39. private final Map<Integer, AuctionableHall> _clanHall;
  40. private final Map<Integer, AuctionableHall> _freeClanHall;
  41. private final Map<Integer, AuctionableHall> _allAuctionableClanHalls;
  42. private static Map<Integer, ClanHall> _allClanHalls = new FastMap<>();
  43. private boolean _loaded = false;
  44. public boolean loaded()
  45. {
  46. return _loaded;
  47. }
  48. protected ClanHallManager()
  49. {
  50. _clanHall = new FastMap<>();
  51. _freeClanHall = new FastMap<>();
  52. _allAuctionableClanHalls = new FastMap<>();
  53. load();
  54. }
  55. /** Load All Clan Hall */
  56. private final void load()
  57. {
  58. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  59. Statement s = con.createStatement();
  60. ResultSet rs = s.executeQuery("SELECT * FROM clanhall ORDER BY id"))
  61. {
  62. int id, ownerId, lease;
  63. while (rs.next())
  64. {
  65. StatsSet set = new StatsSet();
  66. id = rs.getInt("id");
  67. ownerId = rs.getInt("ownerId");
  68. lease = rs.getInt("lease");
  69. set.set("id", id);
  70. set.set("name", rs.getString("name"));
  71. set.set("ownerId", ownerId);
  72. set.set("lease", lease);
  73. set.set("desc", rs.getString("desc"));
  74. set.set("location", rs.getString("location"));
  75. set.set("paidUntil", rs.getLong("paidUntil"));
  76. set.set("grade", rs.getInt("Grade"));
  77. set.set("paid", rs.getBoolean("paid"));
  78. AuctionableHall ch = new AuctionableHall(set);
  79. _allAuctionableClanHalls.put(id, ch);
  80. addClanHall(ch);
  81. if (ch.getOwnerId() > 0)
  82. {
  83. _clanHall.put(id, ch);
  84. continue;
  85. }
  86. _freeClanHall.put(id, ch);
  87. Auction auc = AuctionManager.getInstance().getAuction(id);
  88. if ((auc == null) && (lease > 0))
  89. {
  90. AuctionManager.getInstance().initNPC(id);
  91. }
  92. }
  93. _log.info(getClass().getSimpleName() + ": Loaded: " + getClanHalls().size() + " clan halls");
  94. _log.info(getClass().getSimpleName() + ": Loaded: " + getFreeClanHalls().size() + " free clan halls");
  95. _loaded = true;
  96. }
  97. catch (Exception e)
  98. {
  99. _log.log(Level.WARNING, "Exception: ClanHallManager.load(): " + e.getMessage(), e);
  100. }
  101. }
  102. public static final Map<Integer, ClanHall> getAllClanHalls()
  103. {
  104. return _allClanHalls;
  105. }
  106. /**
  107. * @return all FreeClanHalls
  108. */
  109. public final Map<Integer, AuctionableHall> getFreeClanHalls()
  110. {
  111. return _freeClanHall;
  112. }
  113. /**
  114. * @return all ClanHalls that have owner
  115. */
  116. public final Map<Integer, AuctionableHall> getClanHalls()
  117. {
  118. return _clanHall;
  119. }
  120. /**
  121. * @return all ClanHalls
  122. */
  123. public final Map<Integer, AuctionableHall> getAllAuctionableClanHalls()
  124. {
  125. return _allAuctionableClanHalls;
  126. }
  127. public static final void addClanHall(ClanHall hall)
  128. {
  129. _allClanHalls.put(hall.getId(), hall);
  130. }
  131. /**
  132. * @param chId
  133. * @return true is free ClanHall
  134. */
  135. public final boolean isFree(int chId)
  136. {
  137. if (_freeClanHall.containsKey(chId))
  138. {
  139. return true;
  140. }
  141. return false;
  142. }
  143. /**
  144. * Free a ClanHall
  145. * @param chId
  146. */
  147. public final synchronized void setFree(int chId)
  148. {
  149. _freeClanHall.put(chId, _clanHall.get(chId));
  150. ClanTable.getInstance().getClan(_freeClanHall.get(chId).getOwnerId()).setHideoutId(0);
  151. _freeClanHall.get(chId).free();
  152. _clanHall.remove(chId);
  153. }
  154. /**
  155. * Set ClanHallOwner
  156. * @param chId
  157. * @param clan
  158. */
  159. public final synchronized void setOwner(int chId, L2Clan clan)
  160. {
  161. if (!_clanHall.containsKey(chId))
  162. {
  163. _clanHall.put(chId, _freeClanHall.get(chId));
  164. _freeClanHall.remove(chId);
  165. }
  166. else
  167. {
  168. _clanHall.get(chId).free();
  169. }
  170. ClanTable.getInstance().getClan(clan.getClanId()).setHideoutId(chId);
  171. _clanHall.get(chId).setOwner(clan);
  172. }
  173. /**
  174. * @param clanHallId
  175. * @return Clan Hall by Id
  176. */
  177. public final ClanHall getClanHallById(int clanHallId)
  178. {
  179. return _allClanHalls.get(clanHallId);
  180. }
  181. public final AuctionableHall getAuctionableHallById(int clanHallId)
  182. {
  183. return _allAuctionableClanHalls.get(clanHallId);
  184. }
  185. /**
  186. * @param x
  187. * @param y
  188. * @param z
  189. * @return Clan Hall by x,y,z
  190. */
  191. public final ClanHall getClanHall(int x, int y, int z)
  192. {
  193. for (ClanHall temp : getAllClanHalls().values())
  194. {
  195. if (temp.checkIfInZone(x, y, z))
  196. {
  197. return temp;
  198. }
  199. }
  200. return null;
  201. }
  202. public final ClanHall getClanHall(L2Object activeObject)
  203. {
  204. return getClanHall(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  205. }
  206. public final AuctionableHall getNearbyClanHall(int x, int y, int maxDist)
  207. {
  208. L2ClanHallZone zone = null;
  209. for (Map.Entry<Integer, AuctionableHall> ch : _clanHall.entrySet())
  210. {
  211. zone = ch.getValue().getZone();
  212. if ((zone != null) && (zone.getDistanceToZone(x, y) < maxDist))
  213. {
  214. return ch.getValue();
  215. }
  216. }
  217. for (Map.Entry<Integer, AuctionableHall> ch : _freeClanHall.entrySet())
  218. {
  219. zone = ch.getValue().getZone();
  220. if ((zone != null) && (zone.getDistanceToZone(x, y) < maxDist))
  221. {
  222. return ch.getValue();
  223. }
  224. }
  225. return null;
  226. }
  227. public final ClanHall getNearbyAbstractHall(int x, int y, int maxDist)
  228. {
  229. L2ClanHallZone zone = null;
  230. for (Map.Entry<Integer, ClanHall> ch : _allClanHalls.entrySet())
  231. {
  232. zone = ch.getValue().getZone();
  233. if ((zone != null) && (zone.getDistanceToZone(x, y) < maxDist))
  234. {
  235. return ch.getValue();
  236. }
  237. }
  238. return null;
  239. }
  240. /**
  241. * @param clan
  242. * @return Clan Hall by Owner
  243. */
  244. public final AuctionableHall getClanHallByOwner(L2Clan clan)
  245. {
  246. for (Map.Entry<Integer, AuctionableHall> ch : _clanHall.entrySet())
  247. {
  248. if (clan.getClanId() == ch.getValue().getOwnerId())
  249. {
  250. return ch.getValue();
  251. }
  252. }
  253. return null;
  254. }
  255. public final ClanHall getAbstractHallByOwner(L2Clan clan)
  256. {
  257. // Separate loops to avoid iterating over free clan halls
  258. for (Map.Entry<Integer, AuctionableHall> ch : _clanHall.entrySet())
  259. {
  260. if (clan.getClanId() == ch.getValue().getOwnerId())
  261. {
  262. return ch.getValue();
  263. }
  264. }
  265. for (Map.Entry<Integer, SiegableHall> ch : CHSiegeManager.getInstance().getConquerableHalls().entrySet())
  266. {
  267. if (clan.getClanId() == ch.getValue().getOwnerId())
  268. {
  269. return ch.getValue();
  270. }
  271. }
  272. return null;
  273. }
  274. public static ClanHallManager getInstance()
  275. {
  276. return SingletonHolder._instance;
  277. }
  278. private static class SingletonHolder
  279. {
  280. protected static final ClanHallManager _instance = new ClanHallManager();
  281. }
  282. }