ClanHallManager.java 6.7 KB

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