2
0

ClanHallManager.java 8.0 KB

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