ClanHallManager.java 8.0 KB

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