ClanHallManager.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.Logger;
  21. import com.l2jserver.L2DatabaseFactory;
  22. import com.l2jserver.gameserver.datatables.ClanTable;
  23. import com.l2jserver.gameserver.model.L2Clan;
  24. import com.l2jserver.gameserver.model.entity.Auction;
  25. import com.l2jserver.gameserver.model.entity.ClanHall;
  26. import com.l2jserver.gameserver.model.zone.type.L2ClanHallZone;
  27. import javolution.util.FastMap;
  28. /**
  29. *
  30. * @author Steuf
  31. */
  32. public class ClanHallManager
  33. {
  34. protected static final Logger _log = Logger.getLogger(ClanHallManager.class.getName());
  35. private Map<Integer, ClanHall> _clanHall;
  36. private Map<Integer, ClanHall> _freeClanHall;
  37. private Map<Integer, ClanHall> _allClanHalls;
  38. private boolean _loaded = false;
  39. public static ClanHallManager getInstance()
  40. {
  41. return SingletonHolder._instance;
  42. }
  43. public boolean loaded()
  44. {
  45. return _loaded;
  46. }
  47. private ClanHallManager()
  48. {
  49. _log.info("Initializing ClanHallManager");
  50. _clanHall = new FastMap<Integer, ClanHall>();
  51. _freeClanHall = new FastMap<Integer, ClanHall>();
  52. _allClanHalls = new FastMap<Integer, ClanHall>();
  53. load();
  54. }
  55. /** Reload All Clan Hall */
  56. /* public final void reload() Cant reload atm - would loose zone info
  57. {
  58. _clanHall.clear();
  59. _freeClanHall.clear();
  60. load();
  61. }
  62. */
  63. /** Load All Clan Hall */
  64. private final void load()
  65. {
  66. Connection con = null;
  67. try
  68. {
  69. int id, ownerId, lease, grade = 0;
  70. String Name, Desc, Location;
  71. long paidUntil = 0;
  72. boolean paid = false;
  73. PreparedStatement statement;
  74. ResultSet rs;
  75. con = L2DatabaseFactory.getInstance().getConnection();
  76. statement = con.prepareStatement("SELECT * FROM clanhall ORDER BY id");
  77. rs = statement.executeQuery();
  78. while (rs.next())
  79. {
  80. id = rs.getInt("id");
  81. Name = rs.getString("name");
  82. ownerId = rs.getInt("ownerId");
  83. lease = rs.getInt("lease");
  84. Desc = rs.getString("desc");
  85. Location = rs.getString("location");
  86. paidUntil = rs.getLong("paidUntil");
  87. grade = rs.getInt("Grade");
  88. paid = rs.getBoolean("paid");
  89. ClanHall ch = new ClanHall(id, Name, ownerId, lease, Desc, Location, paidUntil, grade, paid);
  90. _allClanHalls.put(id, ch);
  91. if (ownerId > 0)
  92. {
  93. final L2Clan owner = ClanTable.getInstance().getClan(ownerId);
  94. if (owner != null)
  95. {
  96. _clanHall.put(id, ch);
  97. owner.setHasHideout(id);
  98. continue;
  99. }
  100. else
  101. ch.free();
  102. }
  103. _freeClanHall.put(id, ch);
  104. Auction auc = AuctionManager.getInstance().getAuction(id);
  105. if (auc == null && lease > 0)
  106. AuctionManager.getInstance().initNPC(id);
  107. }
  108. statement.close();
  109. _log.info("Loaded: " + getClanHalls().size() + " clan halls");
  110. _log.info("Loaded: " + getFreeClanHalls().size() + " free clan halls");
  111. _loaded = true;
  112. }
  113. catch (Exception e)
  114. {
  115. _log.warning("Exception: ClanHallManager.load(): " + e.getMessage());
  116. e.printStackTrace();
  117. }
  118. finally
  119. {
  120. try
  121. {
  122. con.close();
  123. }
  124. catch (Exception e)
  125. {
  126. }
  127. }
  128. }
  129. /** Get Map with all FreeClanHalls */
  130. public final Map<Integer, ClanHall> getFreeClanHalls()
  131. {
  132. return _freeClanHall;
  133. }
  134. /** Get Map with all ClanHalls that have owner*/
  135. public final Map<Integer, ClanHall> getClanHalls()
  136. {
  137. return _clanHall;
  138. }
  139. /** Get Map with all ClanHalls*/
  140. public final Map<Integer, ClanHall> getAllClanHalls()
  141. {
  142. return _allClanHalls;
  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. if (_clanHall.containsKey(clanHallId))
  176. return _clanHall.get(clanHallId);
  177. if (_freeClanHall.containsKey(clanHallId))
  178. return _freeClanHall.get(clanHallId);
  179. _log.warning("Clan hall id " + clanHallId + " not found in clanhall table!");
  180. return null;
  181. }
  182. /** Get Clan Hall by x,y,z */
  183. /*
  184. public final ClanHall getClanHall(int x, int y, int z)
  185. {
  186. for (Map.Entry<Integer, ClanHall> ch : _clanHall.entrySet())
  187. if (ch.getValue().getZone().isInsideZone(x, y, z)) return ch.getValue();
  188. for (Map.Entry<Integer, ClanHall> ch : _freeClanHall.entrySet())
  189. if (ch.getValue().getZone().isInsideZone(x, y, z)) return ch.getValue();
  190. return null;
  191. }*/
  192. public final ClanHall getNearbyClanHall(int x, int y, int maxDist)
  193. {
  194. L2ClanHallZone zone = null;
  195. for (Map.Entry<Integer, ClanHall> ch : _clanHall.entrySet())
  196. {
  197. zone = ch.getValue().getZone();
  198. if (zone != null && zone.getDistanceToZone(x, y) < maxDist)
  199. return ch.getValue();
  200. }
  201. for (Map.Entry<Integer, ClanHall> ch : _freeClanHall.entrySet())
  202. {
  203. zone = ch.getValue().getZone();
  204. if (zone != null && zone.getDistanceToZone(x, y) < maxDist)
  205. return ch.getValue();
  206. }
  207. return null;
  208. }
  209. /** Get Clan Hall by Owner */
  210. public final ClanHall getClanHallByOwner(L2Clan clan)
  211. {
  212. for (Map.Entry<Integer, ClanHall> ch : _clanHall.entrySet())
  213. {
  214. if (clan.getClanId() == ch.getValue().getOwnerId())
  215. return ch.getValue();
  216. }
  217. return null;
  218. }
  219. @SuppressWarnings("synthetic-access")
  220. private static class SingletonHolder
  221. {
  222. protected static final ClanHallManager _instance = new ClanHallManager();
  223. }
  224. }