ClanHallManager.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. *
  32. * @author Steuf
  33. */
  34. public class ClanHallManager
  35. {
  36. protected static final Logger _log = Logger.getLogger(ClanHallManager.class.getName());
  37. private Map<Integer, ClanHall> _clanHall;
  38. private Map<Integer, ClanHall> _freeClanHall;
  39. private Map<Integer, ClanHall> _allClanHalls;
  40. private boolean _loaded = false;
  41. public static ClanHallManager getInstance()
  42. {
  43. return SingletonHolder._instance;
  44. }
  45. public boolean loaded()
  46. {
  47. return _loaded;
  48. }
  49. private ClanHallManager()
  50. {
  51. _log.info("Initializing ClanHallManager");
  52. _clanHall = new FastMap<Integer, ClanHall>();
  53. _freeClanHall = new FastMap<Integer, ClanHall>();
  54. _allClanHalls = new FastMap<Integer, ClanHall>();
  55. load();
  56. }
  57. /** Reload All Clan Hall */
  58. /* public final void reload() Cant reload atm - would loose zone info
  59. {
  60. _clanHall.clear();
  61. _freeClanHall.clear();
  62. load();
  63. }
  64. */
  65. /** Load All Clan Hall */
  66. private final void load()
  67. {
  68. Connection con = null;
  69. try
  70. {
  71. int id, ownerId, lease, grade = 0;
  72. String Name, Desc, Location;
  73. long paidUntil = 0;
  74. boolean paid = false;
  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. 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. rs.close();
  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. public final ClanHall getClanHall(int x, int y, int z)
  178. {
  179. for (ClanHall temp : getClanHalls().values())
  180. {
  181. if (temp.checkIfInZone(x, y, z))
  182. return temp;
  183. }
  184. return null;
  185. }
  186. public final ClanHall getClanHall(L2Object activeObject)
  187. {
  188. return getClanHall(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  189. }
  190. public final ClanHall getNearbyClanHall(int x, int y, int maxDist)
  191. {
  192. L2ClanHallZone zone = null;
  193. for (Map.Entry<Integer, ClanHall> ch : _clanHall.entrySet())
  194. {
  195. zone = ch.getValue().getZone();
  196. if (zone != null && zone.getDistanceToZone(x, y) < maxDist)
  197. return ch.getValue();
  198. }
  199. for (Map.Entry<Integer, ClanHall> ch : _freeClanHall.entrySet())
  200. {
  201. zone = ch.getValue().getZone();
  202. if (zone != null && zone.getDistanceToZone(x, y) < maxDist)
  203. return ch.getValue();
  204. }
  205. return null;
  206. }
  207. /** Get Clan Hall by Owner */
  208. public final ClanHall getClanHallByOwner(L2Clan clan)
  209. {
  210. for (Map.Entry<Integer, ClanHall> ch : _clanHall.entrySet())
  211. {
  212. if (clan.getClanId() == ch.getValue().getOwnerId())
  213. return ch.getValue();
  214. }
  215. return null;
  216. }
  217. @SuppressWarnings("synthetic-access")
  218. private static class SingletonHolder
  219. {
  220. protected static final ClanHallManager _instance = new ClanHallManager();
  221. }
  222. }