ClanHallManager.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.zone.type.L2ClanHallZone;
  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. ch.free();
  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. /** Get Map with all FreeClanHalls */
  122. public final Map<Integer, ClanHall> getFreeClanHalls()
  123. {
  124. return _freeClanHall;
  125. }
  126. /** Get Map with all ClanHalls that have owner*/
  127. public final Map<Integer, ClanHall> getClanHalls()
  128. {
  129. return _clanHall;
  130. }
  131. /** Get Map with all ClanHalls*/
  132. public final Map<Integer, ClanHall> getAllClanHalls()
  133. {
  134. return _allClanHalls;
  135. }
  136. /** Check is free ClanHall */
  137. public final boolean isFree(int chId)
  138. {
  139. if (_freeClanHall.containsKey(chId))
  140. return true;
  141. return false;
  142. }
  143. /** Free a ClanHall */
  144. public final synchronized void setFree(int chId)
  145. {
  146. _freeClanHall.put(chId, _clanHall.get(chId));
  147. ClanTable.getInstance().getClan(_freeClanHall.get(chId).getOwnerId()).setHasHideout(0);
  148. _freeClanHall.get(chId).free();
  149. _clanHall.remove(chId);
  150. }
  151. /** Set ClanHallOwner */
  152. public final synchronized void setOwner(int chId, L2Clan clan)
  153. {
  154. if (!_clanHall.containsKey(chId))
  155. {
  156. _clanHall.put(chId, _freeClanHall.get(chId));
  157. _freeClanHall.remove(chId);
  158. }
  159. else
  160. _clanHall.get(chId).free();
  161. ClanTable.getInstance().getClan(clan.getClanId()).setHasHideout(chId);
  162. _clanHall.get(chId).setOwner(clan);
  163. }
  164. /** Get Clan Hall by Id */
  165. public final ClanHall getClanHallById(int clanHallId)
  166. {
  167. if (_clanHall.containsKey(clanHallId))
  168. return _clanHall.get(clanHallId);
  169. if (_freeClanHall.containsKey(clanHallId))
  170. return _freeClanHall.get(clanHallId);
  171. _log.warning("Clan hall id " + clanHallId + " not found in clanhall table!");
  172. return null;
  173. }
  174. /** Get Clan Hall by x,y,z */
  175. public final ClanHall getClanHall(int x, int y, int z)
  176. {
  177. for (ClanHall temp : getClanHalls().values())
  178. {
  179. if (temp.checkIfInZone(x, y, z))
  180. return temp;
  181. }
  182. return null;
  183. }
  184. public final ClanHall getClanHall(L2Object activeObject)
  185. {
  186. return getClanHall(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  187. }
  188. public final ClanHall getNearbyClanHall(int x, int y, int maxDist)
  189. {
  190. L2ClanHallZone zone = null;
  191. for (Map.Entry<Integer, ClanHall> ch : _clanHall.entrySet())
  192. {
  193. zone = ch.getValue().getZone();
  194. if (zone != null && zone.getDistanceToZone(x, y) < maxDist)
  195. return ch.getValue();
  196. }
  197. for (Map.Entry<Integer, ClanHall> ch : _freeClanHall.entrySet())
  198. {
  199. zone = ch.getValue().getZone();
  200. if (zone != null && zone.getDistanceToZone(x, y) < maxDist)
  201. return ch.getValue();
  202. }
  203. return null;
  204. }
  205. /** Get Clan Hall by Owner */
  206. public final ClanHall getClanHallByOwner(L2Clan clan)
  207. {
  208. for (Map.Entry<Integer, ClanHall> ch : _clanHall.entrySet())
  209. {
  210. if (clan.getClanId() == ch.getValue().getOwnerId())
  211. return ch.getValue();
  212. }
  213. return null;
  214. }
  215. @SuppressWarnings("synthetic-access")
  216. private static class SingletonHolder
  217. {
  218. protected static final ClanHallManager _instance = new ClanHallManager();
  219. }
  220. }