ClanHallManager.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 com.l2jserver.L2DatabaseFactory;
  23. import com.l2jserver.gameserver.datatables.ClanTable;
  24. import com.l2jserver.gameserver.model.L2Clan;
  25. import com.l2jserver.gameserver.model.entity.Auction;
  26. import com.l2jserver.gameserver.model.entity.ClanHall;
  27. import com.l2jserver.gameserver.model.zone.type.L2ClanHallZone;
  28. import javolution.util.FastMap;
  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. PreparedStatement statement;
  75. ResultSet rs;
  76. con = L2DatabaseFactory.getInstance().getConnection();
  77. statement = con.prepareStatement("SELECT * FROM clanhall ORDER BY id");
  78. rs = statement.executeQuery();
  79. while (rs.next())
  80. {
  81. id = rs.getInt("id");
  82. Name = rs.getString("name");
  83. ownerId = rs.getInt("ownerId");
  84. lease = rs.getInt("lease");
  85. Desc = rs.getString("desc");
  86. Location = rs.getString("location");
  87. paidUntil = rs.getLong("paidUntil");
  88. grade = rs.getInt("Grade");
  89. paid = rs.getBoolean("paid");
  90. ClanHall ch = new ClanHall(id, Name, ownerId, lease, Desc, Location, paidUntil, grade, paid);
  91. _allClanHalls.put(id, ch);
  92. if (ownerId > 0)
  93. {
  94. final L2Clan owner = ClanTable.getInstance().getClan(ownerId);
  95. if (owner != null)
  96. {
  97. _clanHall.put(id, ch);
  98. owner.setHasHideout(id);
  99. continue;
  100. }
  101. else
  102. ch.free();
  103. }
  104. _freeClanHall.put(id, ch);
  105. Auction auc = AuctionManager.getInstance().getAuction(id);
  106. if (auc == null && lease > 0)
  107. AuctionManager.getInstance().initNPC(id);
  108. }
  109. statement.close();
  110. _log.info("Loaded: " + getClanHalls().size() + " clan halls");
  111. _log.info("Loaded: " + getFreeClanHalls().size() + " free clan halls");
  112. _loaded = true;
  113. }
  114. catch (Exception e)
  115. {
  116. _log.log(Level.WARNING, "Exception: ClanHallManager.load(): " + e.getMessage(), e);
  117. }
  118. finally
  119. {
  120. L2DatabaseFactory.close(con);
  121. }
  122. }
  123. /** Get Map with all FreeClanHalls */
  124. public final Map<Integer, ClanHall> getFreeClanHalls()
  125. {
  126. return _freeClanHall;
  127. }
  128. /** Get Map with all ClanHalls that have owner*/
  129. public final Map<Integer, ClanHall> getClanHalls()
  130. {
  131. return _clanHall;
  132. }
  133. /** Get Map with all ClanHalls*/
  134. public final Map<Integer, ClanHall> getAllClanHalls()
  135. {
  136. return _allClanHalls;
  137. }
  138. /** Check is free ClanHall */
  139. public final boolean isFree(int chId)
  140. {
  141. if (_freeClanHall.containsKey(chId))
  142. return true;
  143. return false;
  144. }
  145. /** Free a ClanHall */
  146. public final synchronized void setFree(int chId)
  147. {
  148. _freeClanHall.put(chId, _clanHall.get(chId));
  149. ClanTable.getInstance().getClan(_freeClanHall.get(chId).getOwnerId()).setHasHideout(0);
  150. _freeClanHall.get(chId).free();
  151. _clanHall.remove(chId);
  152. }
  153. /** Set ClanHallOwner */
  154. public final synchronized void setOwner(int chId, L2Clan clan)
  155. {
  156. if (!_clanHall.containsKey(chId))
  157. {
  158. _clanHall.put(chId, _freeClanHall.get(chId));
  159. _freeClanHall.remove(chId);
  160. }
  161. else
  162. _clanHall.get(chId).free();
  163. ClanTable.getInstance().getClan(clan.getClanId()).setHasHideout(chId);
  164. _clanHall.get(chId).setOwner(clan);
  165. }
  166. /** Get Clan Hall by Id */
  167. public final ClanHall getClanHallById(int clanHallId)
  168. {
  169. if (_clanHall.containsKey(clanHallId))
  170. return _clanHall.get(clanHallId);
  171. if (_freeClanHall.containsKey(clanHallId))
  172. return _freeClanHall.get(clanHallId);
  173. _log.warning("Clan hall id " + clanHallId + " not found in clanhall table!");
  174. return null;
  175. }
  176. /** Get Clan Hall by x,y,z */
  177. /*
  178. public final ClanHall getClanHall(int x, int y, int z)
  179. {
  180. for (Map.Entry<Integer, ClanHall> ch : _clanHall.entrySet())
  181. if (ch.getValue().getZone().isInsideZone(x, y, z)) return ch.getValue();
  182. for (Map.Entry<Integer, ClanHall> ch : _freeClanHall.entrySet())
  183. if (ch.getValue().getZone().isInsideZone(x, y, z)) return ch.getValue();
  184. return null;
  185. }*/
  186. public final ClanHall getNearbyClanHall(int x, int y, int maxDist)
  187. {
  188. L2ClanHallZone zone = null;
  189. for (Map.Entry<Integer, ClanHall> ch : _clanHall.entrySet())
  190. {
  191. zone = ch.getValue().getZone();
  192. if (zone != null && zone.getDistanceToZone(x, y) < maxDist)
  193. return ch.getValue();
  194. }
  195. for (Map.Entry<Integer, ClanHall> ch : _freeClanHall.entrySet())
  196. {
  197. zone = ch.getValue().getZone();
  198. if (zone != null && zone.getDistanceToZone(x, y) < maxDist)
  199. return ch.getValue();
  200. }
  201. return null;
  202. }
  203. /** Get Clan Hall by Owner */
  204. public final ClanHall getClanHallByOwner(L2Clan clan)
  205. {
  206. for (Map.Entry<Integer, ClanHall> ch : _clanHall.entrySet())
  207. {
  208. if (clan.getClanId() == ch.getValue().getOwnerId())
  209. return ch.getValue();
  210. }
  211. return null;
  212. }
  213. @SuppressWarnings("synthetic-access")
  214. private static class SingletonHolder
  215. {
  216. protected static final ClanHallManager _instance = new ClanHallManager();
  217. }
  218. }