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