ClanHallManager.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. if(ownerId == 0)
  93. {
  94. _freeClanHall.put(id,new ClanHall(id,Name,ownerId,lease,Desc,Location,paidUntil,grade,paid));
  95. }
  96. else
  97. {
  98. if(ClanTable.getInstance().getClan(rs.getInt("ownerId")) != null)
  99. {
  100. _clanHall.put(id,new ClanHall(id,Name,ownerId,lease,Desc,Location,paidUntil,grade,paid));
  101. ClanTable.getInstance().getClan(rs.getInt("ownerId")).setHasHideout(id);
  102. }
  103. else
  104. {
  105. _freeClanHall.put(id,new ClanHall(id,Name,ownerId,lease,Desc,Location,paidUntil,grade,paid));
  106. _freeClanHall.get(id).free();
  107. AuctionManager.getInstance().initNPC(id);
  108. }
  109. }
  110. _allClanHalls.put(id,new ClanHall(id,Name,ownerId,lease,Desc,Location,paidUntil,grade,paid));
  111. }
  112. statement.close();
  113. _log.info("Loaded: "+getClanHalls().size() +" clan halls");
  114. _log.info("Loaded: "+getFreeClanHalls().size() +" free clan halls");
  115. _loaded = true;
  116. }
  117. catch (Exception e)
  118. {
  119. _log.warning("Exception: ClanHallManager.load(): " + e.getMessage());
  120. e.printStackTrace();
  121. }
  122. finally
  123. {
  124. try
  125. {
  126. con.close();
  127. } catch (Exception e)
  128. {
  129. }
  130. }
  131. }
  132. /** Get Map with all FreeClanHalls */
  133. public final Map<Integer, ClanHall> getFreeClanHalls()
  134. {
  135. return _freeClanHall;
  136. }
  137. /** Get Map with all ClanHalls that have owner*/
  138. public final Map<Integer, ClanHall> getClanHalls()
  139. {
  140. return _clanHall;
  141. }
  142. /** Get Map with all ClanHalls*/
  143. public final Map<Integer, ClanHall> getAllClanHalls()
  144. {
  145. return _allClanHalls;
  146. }
  147. /** Check is free ClanHall */
  148. public final boolean isFree(int chId)
  149. {
  150. if(_freeClanHall.containsKey(chId))
  151. return true;
  152. return false;
  153. }
  154. /** Free a ClanHall */
  155. public final synchronized void setFree(int chId)
  156. {
  157. _freeClanHall.put(chId,_clanHall.get(chId));
  158. ClanTable.getInstance().getClan(_freeClanHall.get(chId).getOwnerId()).setHasHideout(0);
  159. _freeClanHall.get(chId).free();
  160. _clanHall.remove(chId);
  161. }
  162. /** Set ClanHallOwner */
  163. public final synchronized void setOwner(int chId, L2Clan clan)
  164. {
  165. if(!_clanHall.containsKey(chId))
  166. {
  167. _clanHall.put(chId,_freeClanHall.get(chId));
  168. _freeClanHall.remove(chId);
  169. }else
  170. _clanHall.get(chId).free();
  171. ClanTable.getInstance().getClan(clan.getClanId()).setHasHideout(chId);
  172. _clanHall.get(chId).setOwner(clan);
  173. }
  174. /** Get Clan Hall by Id */
  175. public final ClanHall getClanHallById(int clanHallId)
  176. {
  177. if(_clanHall.containsKey(clanHallId))
  178. return _clanHall.get(clanHallId);
  179. if(_freeClanHall.containsKey(clanHallId))
  180. return _freeClanHall.get(clanHallId);
  181. return null;
  182. }
  183. /** Get Clan Hall by x,y,z *//*
  184. public final ClanHall getClanHall(int x, int y, int z)
  185. {
  186. for (Map.Entry<Integer, ClanHall> ch : _clanHall.entrySet())
  187. if (ch.getValue().getZone().isInsideZone(x, y, z)) return ch.getValue();
  188. for (Map.Entry<Integer, ClanHall> ch : _freeClanHall.entrySet())
  189. if (ch.getValue().getZone().isInsideZone(x, y, z)) return ch.getValue();
  190. return null;
  191. }*/
  192. public final ClanHall getNearbyClanHall(int x, int y, int maxDist)
  193. {
  194. L2ClanHallZone zone = null;
  195. for (Map.Entry<Integer, ClanHall> ch : _clanHall.entrySet())
  196. {
  197. zone = ch.getValue().getZone();
  198. if (zone != null && zone.getDistanceToZone(x, y) < maxDist)
  199. return ch.getValue();
  200. }
  201. for (Map.Entry<Integer, ClanHall> ch : _freeClanHall.entrySet())
  202. {
  203. zone = ch.getValue().getZone();
  204. if (zone != null && zone.getDistanceToZone(x, y) < maxDist)
  205. return ch.getValue();
  206. }
  207. return null;
  208. }
  209. /** Get Clan Hall by Owner */
  210. public final ClanHall getClanHallByOwner(L2Clan clan)
  211. {
  212. for (Map.Entry<Integer, ClanHall> ch : _clanHall.entrySet())
  213. if (clan.getClanId() == ch.getValue().getOwnerId())
  214. return ch.getValue();
  215. return null;
  216. }
  217. }