L2Clan.java 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474
  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.model;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import javolution.util.FastList;
  24. import javolution.util.FastMap;
  25. import net.sf.l2j.Config;
  26. import net.sf.l2j.L2DatabaseFactory;
  27. import net.sf.l2j.gameserver.communitybbs.BB.Forum;
  28. import net.sf.l2j.gameserver.communitybbs.Manager.ForumsBBSManager;
  29. import net.sf.l2j.gameserver.datatables.ClanTable;
  30. import net.sf.l2j.gameserver.datatables.SkillTable;
  31. import net.sf.l2j.gameserver.instancemanager.CastleManager;
  32. import net.sf.l2j.gameserver.instancemanager.FortManager;
  33. import net.sf.l2j.gameserver.instancemanager.SiegeManager;
  34. import net.sf.l2j.gameserver.model.actor.L2Character;
  35. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  36. import net.sf.l2j.gameserver.model.itemcontainer.ItemContainer;
  37. import net.sf.l2j.gameserver.network.SystemMessageId;
  38. import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
  39. import net.sf.l2j.gameserver.network.serverpackets.ExBrExtraUserInfo;
  40. import net.sf.l2j.gameserver.network.serverpackets.ItemList;
  41. import net.sf.l2j.gameserver.network.serverpackets.L2GameServerPacket;
  42. import net.sf.l2j.gameserver.network.serverpackets.PledgeReceiveSubPledgeCreated;
  43. import net.sf.l2j.gameserver.network.serverpackets.PledgeShowInfoUpdate;
  44. import net.sf.l2j.gameserver.network.serverpackets.PledgeShowMemberListAll;
  45. import net.sf.l2j.gameserver.network.serverpackets.PledgeShowMemberListDeleteAll;
  46. import net.sf.l2j.gameserver.network.serverpackets.PledgeShowMemberListUpdate;
  47. import net.sf.l2j.gameserver.network.serverpackets.PledgeSkillListAdd;
  48. import net.sf.l2j.gameserver.network.serverpackets.SkillCoolTime;
  49. import net.sf.l2j.gameserver.network.serverpackets.StatusUpdate;
  50. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  51. import net.sf.l2j.gameserver.network.serverpackets.UserInfo;
  52. import net.sf.l2j.gameserver.util.Util;
  53. /**
  54. * This class ...
  55. *
  56. * @version $Revision: 1.7.2.4.2.7 $ $Date: 2005/04/06 16:13:41 $
  57. */
  58. public class L2Clan
  59. {
  60. private static final Logger _log = Logger.getLogger(L2Clan.class.getName());
  61. private String _name;
  62. private int _clanId;
  63. private L2ClanMember _leader;
  64. private Map<Integer, L2ClanMember> _members = new FastMap<Integer, L2ClanMember>();
  65. private String _allyName;
  66. private int _allyId;
  67. private int _level;
  68. private int _hasCastle;
  69. private int _hasFort;
  70. private int _hasHideout;
  71. private boolean _hasCrest;
  72. private int _hiredGuards;
  73. private int _crestId;
  74. private int _crestLargeId;
  75. private int _allyCrestId;
  76. private int _auctionBiddedAt = 0;
  77. private long _allyPenaltyExpiryTime;
  78. private int _allyPenaltyType;
  79. private long _charPenaltyExpiryTime;
  80. private long _dissolvingExpiryTime;
  81. // Ally Penalty Types
  82. /** Clan leaved ally */
  83. public static final int PENALTY_TYPE_CLAN_LEAVED = 1;
  84. /** Clan was dismissed from ally */
  85. public static final int PENALTY_TYPE_CLAN_DISMISSED = 2;
  86. /** Leader clan dismiss clan from ally */
  87. public static final int PENALTY_TYPE_DISMISS_CLAN = 3;
  88. /** Leader clan dissolve ally */
  89. public static final int PENALTY_TYPE_DISSOLVE_ALLY = 4;
  90. private ItemContainer _warehouse = new ClanWarehouse(this);
  91. private List<Integer> _atWarWith = new FastList<Integer>();
  92. private List<Integer> _atWarAttackers = new FastList<Integer>();
  93. private boolean _hasCrestLarge;
  94. private Forum _forum;
  95. private List<L2Skill> _skillList = new FastList<L2Skill>();
  96. // Clan Privileges
  97. /** No privilege to manage any clan activity */
  98. public static final int CP_NOTHING = 0;
  99. /** Privilege to join clan */
  100. public static final int CP_CL_JOIN_CLAN = 2;
  101. /** Privilege to give a title */
  102. public static final int CP_CL_GIVE_TITLE = 4;
  103. /** Privilege to view warehouse content */
  104. public static final int CP_CL_VIEW_WAREHOUSE = 8;
  105. /** Privilege to manage clan ranks */
  106. public static final int CP_CL_MANAGE_RANKS = 16;
  107. public static final int CP_CL_PLEDGE_WAR = 32;
  108. public static final int CP_CL_DISMISS = 64;
  109. /** Privilege to register clan crest */
  110. public static final int CP_CL_REGISTER_CREST = 128;
  111. public static final int CP_CL_APPRENTICE = 256;
  112. public static final int CP_CL_TROOPS_FAME = 512;
  113. public static final int CP_CL_SUMMON_AIRSHIP = 1024;
  114. /** Privilege to open a door */
  115. public static final int CP_CH_OPEN_DOOR = 2048;
  116. public static final int CP_CH_OTHER_RIGHTS = 4096;
  117. public static final int CP_CH_AUCTION = 8192;
  118. public static final int CP_CH_DISMISS = 16384;
  119. public static final int CP_CH_SET_FUNCTIONS = 32768;
  120. public static final int CP_CS_OPEN_DOOR = 65536;
  121. public static final int CP_CS_MANOR_ADMIN = 131072;
  122. public static final int CP_CS_MANAGE_SIEGE = 262144;
  123. public static final int CP_CS_USE_FUNCTIONS = 524288;
  124. public static final int CP_CS_DISMISS = 1048576;
  125. public static final int CP_CS_TAXES = 2097152;
  126. public static final int CP_CS_MERCENARIES = 4194304;
  127. public static final int CP_CS_SET_FUNCTIONS = 8388608;
  128. /** Privilege to manage all clan activity */
  129. public static final int CP_ALL = 16777214;
  130. // Sub-unit types
  131. /** Clan subunit type of Academy */
  132. public static final int SUBUNIT_ACADEMY = -1;
  133. /** Clan subunit type of Royal Guard A */
  134. public static final int SUBUNIT_ROYAL1 = 100;
  135. /** Clan subunit type of Royal Guard B */
  136. public static final int SUBUNIT_ROYAL2 = 200;
  137. /** Clan subunit type of Order of Knights A-1 */
  138. public static final int SUBUNIT_KNIGHT1 = 1001;
  139. /** Clan subunit type of Order of Knights A-2 */
  140. public static final int SUBUNIT_KNIGHT2 = 1002;
  141. /** Clan subunit type of Order of Knights B-1 */
  142. public static final int SUBUNIT_KNIGHT3 = 2001;
  143. /** Clan subunit type of Order of Knights B-2 */
  144. public static final int SUBUNIT_KNIGHT4 = 2002;
  145. /** FastMap(Integer, L2Skill) containing all skills of the L2Clan */
  146. protected final Map<Integer, L2Skill> _skills = new FastMap<Integer, L2Skill>();
  147. protected final Map<Integer, RankPrivs> _privs = new FastMap<Integer, RankPrivs>();
  148. protected final Map<Integer, SubPledge> _subPledges = new FastMap<Integer, SubPledge>();
  149. private int _reputationScore = 0;
  150. private int _rank = 0;
  151. private String _notice;
  152. private boolean _noticeEnabled = false;
  153. private static final int MAX_NOTICE_LENGTH = 8192;
  154. /**
  155. * Called if a clan is referenced only by id.
  156. * In this case all other data needs to be fetched from db
  157. *
  158. * @param clanId A valid clan Id to create and restore
  159. */
  160. public L2Clan(int clanId)
  161. {
  162. _clanId = clanId;
  163. initializePrivs();
  164. restore();
  165. getWarehouse().restore();
  166. }
  167. /**
  168. * Called only if a new clan is created
  169. *
  170. * @param clanId A valid clan Id to create
  171. * @param clanName A valid clan name
  172. */
  173. public L2Clan(int clanId, String clanName)
  174. {
  175. _clanId = clanId;
  176. _name = clanName;
  177. initializePrivs();
  178. }
  179. /**
  180. * @return Returns the clanId.
  181. */
  182. public int getClanId()
  183. {
  184. return _clanId;
  185. }
  186. /**
  187. * @param clanId The clanId to set.
  188. */
  189. public void setClanId(int clanId)
  190. {
  191. _clanId = clanId;
  192. }
  193. /**
  194. * @return Returns the leaderId.
  195. */
  196. public int getLeaderId()
  197. {
  198. return (_leader != null ? _leader.getObjectId() : 0);
  199. }
  200. /**
  201. * @return L2ClanMember of clan leader.
  202. */
  203. public L2ClanMember getLeader()
  204. {
  205. return _leader;
  206. }
  207. /**
  208. * @param leaderId The leaderId to set.
  209. */
  210. public void setLeader(L2ClanMember leader)
  211. {
  212. _leader = leader;
  213. _members.put(leader.getObjectId(), leader);
  214. }
  215. public void setNewLeader(L2ClanMember member)
  216. {
  217. if (!getLeader().isOnline())
  218. {
  219. return;
  220. }
  221. if (member == null)
  222. {
  223. return;
  224. }
  225. if (!member.isOnline())
  226. {
  227. return;
  228. }
  229. L2PcInstance exLeader = getLeader().getPlayerInstance();
  230. SiegeManager.getInstance().removeSiegeSkills(exLeader);
  231. exLeader.setClan(this);
  232. exLeader.setClanPrivileges(L2Clan.CP_NOTHING);
  233. exLeader.broadcastUserInfo();
  234. setLeader(member);
  235. updateClanInDB();
  236. exLeader.setPledgeClass(exLeader.getClan().getClanMember(exLeader.getObjectId()).calculatePledgeClass(exLeader));
  237. exLeader.broadcastUserInfo();
  238. exLeader.checkItemRestriction();
  239. L2PcInstance newLeader = member.getPlayerInstance();
  240. newLeader.setClan(this);
  241. newLeader.setPledgeClass(member.calculatePledgeClass(newLeader));
  242. newLeader.setClanPrivileges(L2Clan.CP_ALL);
  243. if (getLevel() >= SiegeManager.getInstance().getSiegeClanMinLevel())
  244. {
  245. SiegeManager.getInstance().addSiegeSkills(newLeader);
  246. // Transferring siege skills TimeStamps from old leader to new leader to prevent unlimited headquarters
  247. if (!exLeader.getReuseTimeStamp().isEmpty())
  248. {
  249. for (L2Skill sk : SkillTable.getInstance().getSiegeSkills(newLeader.isNoble()))
  250. {
  251. if (exLeader.getReuseTimeStamp().containsKey(sk.getId()))
  252. newLeader.addTimeStamp(exLeader.getReuseTimeStamp().get(sk.getId()));
  253. }
  254. newLeader.sendPacket(new SkillCoolTime(newLeader));
  255. }
  256. }
  257. newLeader.broadcastUserInfo();
  258. broadcastClanStatus();
  259. SystemMessage sm = new SystemMessage(SystemMessageId.CLAN_LEADER_PRIVILEGES_HAVE_BEEN_TRANSFERRED_TO_C1);
  260. sm.addString(newLeader.getName());
  261. broadcastToOnlineMembers(sm);
  262. sm = null;
  263. }
  264. /**
  265. * @return Returns the leaderName.
  266. */
  267. public String getLeaderName()
  268. {
  269. return _members.get(Integer.valueOf(_leader.getObjectId())).getName();
  270. }
  271. /**
  272. * @return Returns the name.
  273. */
  274. public String getName()
  275. {
  276. return _name;
  277. }
  278. /**
  279. * @param name The name to set.
  280. */
  281. public void setName(String name)
  282. {
  283. _name = name;
  284. }
  285. private void addClanMember(L2ClanMember member)
  286. {
  287. _members.put(member.getObjectId(), member);
  288. }
  289. public void addClanMember(L2PcInstance player)
  290. {
  291. // Using a different constructor, to make it easier to read
  292. // L2ClanMember(L2Clan, L2PcInstance)
  293. // L2ClanMember member = new L2ClanMember(this,player.getName(), player.getLevel(), player.getClassId().getId(), player.getObjectId(), player.getPledgeType(), player.getPowerGrade(), player.getTitle(), player.getAppearance().getSex(), player.getRace().ordinal());
  294. L2ClanMember member = new L2ClanMember(this, player);
  295. // store in memory
  296. //
  297. addClanMember(member);
  298. member.setPlayerInstance(player);
  299. player.setClan(this);
  300. player.setPledgeClass(member.calculatePledgeClass(player));
  301. player.sendPacket(new PledgeShowMemberListUpdate(player));
  302. player.sendPacket(new UserInfo(player));
  303. player.sendPacket(new ExBrExtraUserInfo(player));
  304. }
  305. public void updateClanMember(L2PcInstance player)
  306. {
  307. L2ClanMember member = new L2ClanMember(player);
  308. if (player.isClanLeader())
  309. setLeader(member);
  310. addClanMember(member);
  311. }
  312. public L2ClanMember getClanMember(String name)
  313. {
  314. for (L2ClanMember temp : _members.values())
  315. {
  316. if (temp.getName().equals(name)) return temp;
  317. }
  318. return null;
  319. }
  320. public L2ClanMember getClanMember(int objectID)
  321. {
  322. return _members.get(objectID);
  323. }
  324. public void removeClanMember(int objectId, long clanJoinExpiryTime)
  325. {
  326. L2ClanMember exMember = _members.remove(objectId);
  327. if(exMember == null)
  328. {
  329. _log.warning("Member Object ID: "+objectId+" not found in clan while trying to remove");
  330. return;
  331. }
  332. int leadssubpledge = getLeaderSubPledge(objectId);
  333. if (leadssubpledge != 0)
  334. {
  335. // Sub-unit leader withdraws, position becomes vacant and leader
  336. // should appoint new via NPC
  337. getSubPledge(leadssubpledge).setLeaderId(0);
  338. updateSubPledgeInDB(leadssubpledge);
  339. }
  340. if(exMember.getApprentice() != 0)
  341. {
  342. L2ClanMember apprentice = getClanMember(exMember.getApprentice());
  343. if(apprentice != null)
  344. {
  345. if (apprentice.getPlayerInstance() != null)
  346. apprentice.getPlayerInstance().setSponsor(0);
  347. else
  348. apprentice.initApprenticeAndSponsor(0, 0);
  349. apprentice.saveApprenticeAndSponsor(0, 0);
  350. }
  351. }
  352. if(exMember.getSponsor() != 0)
  353. {
  354. L2ClanMember sponsor = getClanMember(exMember.getSponsor());
  355. if(sponsor != null)
  356. {
  357. if (sponsor.getPlayerInstance() != null)
  358. sponsor.getPlayerInstance().setApprentice(0);
  359. else
  360. sponsor.initApprenticeAndSponsor(0, 0);
  361. sponsor.saveApprenticeAndSponsor(0, 0);
  362. }
  363. }
  364. exMember.saveApprenticeAndSponsor(0, 0);
  365. if (Config.REMOVE_CASTLE_CIRCLETS)
  366. {
  367. CastleManager.getInstance().removeCirclet(exMember,getHasCastle());
  368. }
  369. if (exMember.isOnline())
  370. {
  371. L2PcInstance player = exMember.getPlayerInstance();
  372. player.setTitle("");
  373. player.setApprentice(0);
  374. player.setSponsor(0);
  375. if (player.isClanLeader())
  376. {
  377. SiegeManager.getInstance().removeSiegeSkills(player);
  378. player.setClanCreateExpiryTime(System.currentTimeMillis() + Config.ALT_CLAN_CREATE_DAYS * 86400000L); //24*60*60*1000 = 86400000
  379. }
  380. // remove Clanskills from Player
  381. for(L2Skill skill: player.getClan().getAllSkills())
  382. player.removeSkill(skill,false);
  383. // remove Residential skills
  384. if (player.getClan().getHasCastle() > 0)
  385. CastleManager.getInstance().getCastleByOwner(player.getClan()).removeResidentialSkills(player);
  386. if (player.getClan().getHasFort() > 0)
  387. FortManager.getInstance().getFortByOwner(player.getClan()).removeResidentialSkills(player);
  388. player.sendSkillList();
  389. player.setClan(null);
  390. // players leaving from clan academy have no penalty
  391. if (exMember.getPledgeType() != -1)
  392. player.setClanJoinExpiryTime(clanJoinExpiryTime);
  393. player.setPledgeClass(exMember.calculatePledgeClass(player));
  394. player.broadcastUserInfo();
  395. // disable clan tab
  396. player.sendPacket(new PledgeShowMemberListDeleteAll());
  397. }
  398. else
  399. {
  400. removeMemberInDatabase(exMember, clanJoinExpiryTime,
  401. getLeaderId()==objectId ? System.currentTimeMillis() + Config.ALT_CLAN_CREATE_DAYS * 86400000L : 0);
  402. }
  403. }
  404. public L2ClanMember[] getMembers()
  405. {
  406. return _members.values().toArray(new L2ClanMember[_members.size()]);
  407. }
  408. public int getMembersCount()
  409. {
  410. return _members.size();
  411. }
  412. public int getSubPledgeMembersCount(int subpl)
  413. {
  414. int result = 0;
  415. for (L2ClanMember temp : _members.values())
  416. {
  417. if (temp.getPledgeType() == subpl) result++;
  418. }
  419. return result;
  420. }
  421. public int getMaxNrOfMembers(int pledgetype)
  422. {
  423. int limit = 0;
  424. switch (pledgetype)
  425. {
  426. case 0:
  427. switch (getLevel())
  428. {
  429. case 4:
  430. limit = 40;
  431. break;
  432. case 3:
  433. limit = 30;
  434. break;
  435. case 2:
  436. limit = 20;
  437. break;
  438. case 1:
  439. limit = 15;
  440. break;
  441. case 0:
  442. limit = 10;
  443. break;
  444. default:
  445. limit = 40;
  446. break;
  447. }
  448. break;
  449. case -1:
  450. case 100:
  451. case 200:
  452. limit = 20;
  453. break;
  454. case 1001:
  455. case 1002:
  456. case 2001:
  457. case 2002:
  458. switch (getLevel())
  459. {
  460. case 9:
  461. case 10:
  462. limit = 25;
  463. break;
  464. default:
  465. limit = 10;
  466. break;
  467. }
  468. break;
  469. default:
  470. break;
  471. }
  472. return limit;
  473. }
  474. public L2PcInstance[] getOnlineMembers(int exclude)
  475. {
  476. List<L2PcInstance> result = new FastList<L2PcInstance>();
  477. for (L2ClanMember temp : _members.values())
  478. {
  479. try {
  480. if (temp.isOnline() && !(temp.getObjectId()==exclude))
  481. result.add(temp.getPlayerInstance());
  482. } catch (NullPointerException e) {}
  483. }
  484. return result.toArray(new L2PcInstance[result.size()]);
  485. }
  486. /**
  487. * @return
  488. */
  489. public int getAllyId()
  490. {
  491. return _allyId;
  492. }
  493. /**
  494. * @return
  495. */
  496. public String getAllyName()
  497. {
  498. return _allyName;
  499. }
  500. public void setAllyCrestId(int allyCrestId)
  501. {
  502. _allyCrestId = allyCrestId;
  503. }
  504. /**
  505. * @return
  506. */
  507. public int getAllyCrestId()
  508. {
  509. return _allyCrestId;
  510. }
  511. /**
  512. * @return
  513. */
  514. public int getLevel()
  515. {
  516. return _level;
  517. }
  518. /**
  519. * @return
  520. */
  521. public int getHasCastle()
  522. {
  523. return _hasCastle;
  524. }
  525. /**
  526. * @return
  527. */
  528. public int getHasFort()
  529. {
  530. return _hasFort;
  531. }
  532. /**
  533. * @return
  534. */
  535. public int getHasHideout()
  536. {
  537. return _hasHideout;
  538. }
  539. /**
  540. * @param crestId The id of pledge crest.
  541. */
  542. public void setCrestId(int crestId)
  543. {
  544. _crestId = crestId;
  545. }
  546. /**
  547. * @return Returns the clanCrestId.
  548. */
  549. public int getCrestId()
  550. {
  551. return _crestId;
  552. }
  553. /**
  554. * @param crestLargeId The id of pledge LargeCrest.
  555. */
  556. public void setCrestLargeId(int crestLargeId)
  557. {
  558. _crestLargeId = crestLargeId;
  559. }
  560. /**
  561. * @return Returns the clan CrestLargeId
  562. */
  563. public int getCrestLargeId()
  564. {
  565. return _crestLargeId;
  566. }
  567. /**
  568. * @param allyId The allyId to set.
  569. */
  570. public void setAllyId(int allyId)
  571. {
  572. _allyId = allyId;
  573. }
  574. /**
  575. * @param allyName The allyName to set.
  576. */
  577. public void setAllyName(String allyName)
  578. {
  579. _allyName = allyName;
  580. }
  581. /**
  582. * @param hasCastle The hasCastle to set.
  583. */
  584. public void setHasCastle(int hasCastle)
  585. {
  586. _hasCastle = hasCastle;
  587. }
  588. /**
  589. * @param hasFort The hasFort to set.
  590. */
  591. public void setHasFort(int hasFort)
  592. {
  593. _hasFort = hasFort;
  594. }
  595. /**
  596. * @param hasHideout The hasHideout to set.
  597. */
  598. public void setHasHideout(int hasHideout)
  599. {
  600. _hasHideout = hasHideout;
  601. }
  602. /**
  603. * @param level The level to set.
  604. */
  605. public void setLevel(int level)
  606. {
  607. _level = level;
  608. if(_forum == null)
  609. {
  610. if(_level >= 2)
  611. {
  612. Forum forum = ForumsBBSManager.getInstance().getForumByName("ClanRoot");
  613. if (forum != null)
  614. _forum = forum.getChildByName(_name);
  615. if(_forum == null)
  616. {
  617. _forum = ForumsBBSManager.getInstance().createNewForum(_name,ForumsBBSManager.getInstance().getForumByName("ClanRoot"),Forum.CLAN,Forum.CLANMEMBERONLY,getClanId());
  618. }
  619. }
  620. }
  621. }
  622. /**
  623. * @param player name
  624. * @return
  625. */
  626. public boolean isMember(int id)
  627. {
  628. return (id == 0 ? false :_members.containsKey(id));
  629. }
  630. public void updateClanInDB()
  631. {
  632. Connection con = null;
  633. PreparedStatement statement = null;
  634. try
  635. {
  636. con = L2DatabaseFactory.getInstance().getConnection();
  637. statement = con.prepareStatement("UPDATE clan_data SET leader_id=?,ally_id=?,ally_name=?,reputation_score=?,ally_penalty_expiry_time=?,ally_penalty_type=?,char_penalty_expiry_time=?,dissolving_expiry_time=? WHERE clan_id=?");
  638. statement.setInt(1, getLeaderId());
  639. statement.setInt(2, getAllyId());
  640. statement.setString(3, getAllyName());
  641. statement.setInt(4, getReputationScore());
  642. statement.setLong(5, getAllyPenaltyExpiryTime());
  643. statement.setInt(6, getAllyPenaltyType());
  644. statement.setLong(7, getCharPenaltyExpiryTime());
  645. statement.setLong(8, getDissolvingExpiryTime());
  646. statement.setInt(9, getClanId());
  647. statement.execute();
  648. if (Config.DEBUG) _log.fine("New clan leader saved in db: "+getClanId());
  649. }
  650. catch (Exception e)
  651. {
  652. _log.log(Level.SEVERE, "Error saving new clan leader.", e);
  653. }
  654. finally
  655. {
  656. if (statement != null)
  657. try
  658. {
  659. statement.close();
  660. }
  661. catch (Exception e)
  662. {
  663. e.printStackTrace();
  664. }
  665. if (con != null)
  666. try
  667. {
  668. con.close();
  669. }
  670. catch (Exception e)
  671. {
  672. e.printStackTrace();
  673. }
  674. }
  675. }
  676. public void store()
  677. {
  678. Connection con = null;
  679. try
  680. {
  681. con = L2DatabaseFactory.getInstance().getConnection();
  682. PreparedStatement statement = con.prepareStatement("INSERT INTO clan_data (clan_id,clan_name,clan_level,hasCastle,ally_id,ally_name,leader_id,crest_id,crest_large_id,ally_crest_id) values (?,?,?,?,?,?,?,?,?,?)");
  683. statement.setInt(1, getClanId());
  684. statement.setString(2, getName());
  685. statement.setInt(3, getLevel());
  686. statement.setInt(4, getHasCastle());
  687. statement.setInt(5, getAllyId());
  688. statement.setString(6, getAllyName());
  689. statement.setInt(7, getLeaderId());
  690. statement.setInt(8, getCrestId());
  691. statement.setInt(9,getCrestLargeId());
  692. statement.setInt(10,getAllyCrestId());
  693. statement.execute();
  694. statement.close();
  695. if (Config.DEBUG) _log.fine("New clan saved in db: "+getClanId());
  696. }
  697. catch (Exception e)
  698. {
  699. _log.log(Level.SEVERE, "Error saving new clan.", e);
  700. }
  701. finally
  702. {
  703. try { con.close(); } catch (Exception e) {}
  704. }
  705. }
  706. private void removeMemberInDatabase(L2ClanMember member, long clanJoinExpiryTime, long clanCreateExpiryTime)
  707. {
  708. Connection con = null;
  709. try
  710. {
  711. con = L2DatabaseFactory.getInstance().getConnection();
  712. PreparedStatement statement = con.prepareStatement("UPDATE characters SET clanid=0, title=?, clan_join_expiry_time=?, clan_create_expiry_time=?, clan_privs=0, wantspeace=0, subpledge=0, lvl_joined_academy=0, apprentice=0, sponsor=0 WHERE charId=?");
  713. statement.setString(1, "");
  714. statement.setLong(2, clanJoinExpiryTime);
  715. statement.setLong(3, clanCreateExpiryTime);
  716. statement.setInt(4, member.getObjectId());
  717. statement.execute();
  718. statement.close();
  719. if (Config.DEBUG) _log.fine("clan member removed in db: "+getClanId());
  720. statement = con.prepareStatement("UPDATE characters SET apprentice=0 WHERE apprentice=?");
  721. statement.setInt(1, member.getObjectId());
  722. statement.execute();
  723. statement.close();
  724. statement = con.prepareStatement("UPDATE characters SET sponsor=0 WHERE sponsor=?");
  725. statement.setInt(1, member.getObjectId());
  726. statement.execute();
  727. statement.close();
  728. }
  729. catch (Exception e)
  730. {
  731. _log.log(Level.SEVERE, "Error removing clan member.", e);
  732. }
  733. finally
  734. {
  735. try { con.close(); } catch (Exception e) {}
  736. }
  737. }
  738. @SuppressWarnings("unused")
  739. private void updateWarsInDB()
  740. {
  741. Connection con = null;
  742. try
  743. {
  744. con = L2DatabaseFactory.getInstance().getConnection();
  745. PreparedStatement statement;
  746. statement = con.prepareStatement("UPDATE clan_wars SET wantspeace1=? WHERE clan1=?");
  747. statement.setInt(1, 0);
  748. statement.setInt(2, 0);
  749. // TODO: deprecated? no execute? :o
  750. statement.close();
  751. }
  752. catch (Exception e)
  753. {
  754. _log.log(Level.SEVERE, "Error updating clan wars data.", e);
  755. }
  756. finally
  757. {
  758. try
  759. {
  760. con.close();
  761. }
  762. catch (Exception e)
  763. {
  764. }
  765. }
  766. }
  767. private void restore()
  768. {
  769. //restorewars();
  770. Connection con = null;
  771. try
  772. {
  773. L2ClanMember member;
  774. con = L2DatabaseFactory.getInstance().getConnection();
  775. PreparedStatement statement = con.prepareStatement("SELECT clan_name,clan_level,hasCastle,ally_id,ally_name,leader_id,crest_id,crest_large_id,ally_crest_id,reputation_score,auction_bid_at,ally_penalty_expiry_time,ally_penalty_type,char_penalty_expiry_time,dissolving_expiry_time FROM clan_data where clan_id=?");
  776. statement.setInt(1, getClanId());
  777. ResultSet clanData = statement.executeQuery();
  778. if (clanData.next())
  779. {
  780. setName(clanData.getString("clan_name"));
  781. setLevel(clanData.getInt("clan_level"));
  782. setHasCastle(clanData.getInt("hasCastle"));
  783. setAllyId(clanData.getInt("ally_id"));
  784. setAllyName(clanData.getString("ally_name"));
  785. setAllyPenaltyExpiryTime(clanData.getLong("ally_penalty_expiry_time"), clanData.getInt("ally_penalty_type"));
  786. if (getAllyPenaltyExpiryTime() < System.currentTimeMillis())
  787. {
  788. setAllyPenaltyExpiryTime(0, 0);
  789. }
  790. setCharPenaltyExpiryTime(clanData.getLong("char_penalty_expiry_time"));
  791. if (getCharPenaltyExpiryTime() + Config.ALT_CLAN_JOIN_DAYS * 86400000L < System.currentTimeMillis()) //24*60*60*1000 = 86400000
  792. {
  793. setCharPenaltyExpiryTime(0);
  794. }
  795. setDissolvingExpiryTime(clanData.getLong("dissolving_expiry_time"));
  796. setCrestId(clanData.getInt("crest_id"));
  797. if (getCrestId() != 0)
  798. {
  799. setHasCrest(true);
  800. }
  801. setCrestLargeId(clanData.getInt("crest_large_id"));
  802. if (getCrestLargeId() != 0)
  803. {
  804. setHasCrestLarge(true);
  805. }
  806. setAllyCrestId(clanData.getInt("ally_crest_id"));
  807. setReputationScore(clanData.getInt("reputation_score"), false);
  808. setAuctionBiddedAt(clanData.getInt("auction_bid_at"), false);
  809. int leaderId = (clanData.getInt("leader_id"));
  810. PreparedStatement statement2 = con.prepareStatement("SELECT char_name,level,classid,charId,title,power_grade,subpledge,apprentice,sponsor,sex,race FROM characters WHERE clanid=?");
  811. statement2.setInt(1, getClanId());
  812. ResultSet clanMembers = statement2.executeQuery();
  813. while (clanMembers.next())
  814. {
  815. member = new L2ClanMember(this, clanMembers.getString("char_name"), clanMembers.getInt("level"), clanMembers.getInt("classid"), clanMembers.getInt("charId"),clanMembers.getInt("subpledge"), clanMembers.getInt("power_grade"), clanMembers.getString("title"), (clanMembers.getInt("sex")!=0), clanMembers.getInt("race"));
  816. if (member.getObjectId() == leaderId)
  817. setLeader(member);
  818. else
  819. addClanMember(member);
  820. member.initApprenticeAndSponsor(clanMembers.getInt("apprentice"), clanMembers.getInt("sponsor"));
  821. }
  822. clanMembers.close();
  823. statement2.close();
  824. }
  825. clanData.close();
  826. statement.close();
  827. if (Config.DEBUG && getName() != null)
  828. _log.config("Restored clan data for \"" + getName() + "\" from database.");
  829. restoreSubPledges();
  830. restoreRankPrivs();
  831. restoreSkills();
  832. restoreNotice();
  833. }
  834. catch (Exception e)
  835. {
  836. _log.log(Level.SEVERE, "Error restoring clan data.", e);
  837. }
  838. finally
  839. {
  840. try { con.close(); } catch (Exception e) {}
  841. }
  842. }
  843. private void restoreNotice()
  844. {
  845. Connection con = null;
  846. try
  847. {
  848. con = L2DatabaseFactory.getInstance().getConnection();
  849. PreparedStatement statement = con.prepareStatement("SELECT enabled,notice FROM clan_notices WHERE clan_id=?");
  850. statement.setInt(1, getClanId());
  851. ResultSet noticeData = statement.executeQuery();
  852. while (noticeData.next())
  853. {
  854. _noticeEnabled = noticeData.getBoolean("enabled");
  855. _notice = noticeData.getString("notice");
  856. }
  857. noticeData.close();
  858. statement.close();
  859. }
  860. catch (Exception e)
  861. {
  862. _log.log(Level.SEVERE, "Error restoring clan notice.", e);
  863. }
  864. finally
  865. {
  866. try
  867. {
  868. con.close();
  869. }
  870. catch (Exception e)
  871. {
  872. }
  873. }
  874. }
  875. private void storeNotice(String notice, boolean enabled)
  876. {
  877. if (notice == null)
  878. notice = "";
  879. if (notice.length() > MAX_NOTICE_LENGTH)
  880. notice = notice.substring(0, MAX_NOTICE_LENGTH - 1);
  881. Connection con = null;
  882. try
  883. {
  884. con = L2DatabaseFactory.getInstance().getConnection();
  885. PreparedStatement statement = con.prepareStatement("INSERT INTO clan_notices (clan_id,notice,enabled) values (?,?,?) ON DUPLICATE KEY UPDATE notice=?,enabled=?");
  886. statement.setInt(1, getClanId());
  887. statement.setString(2, notice);
  888. if (enabled)
  889. statement.setString(3, "true");
  890. else
  891. statement.setString(3, "false");
  892. statement.setString(4, notice);
  893. if (enabled)
  894. statement.setString(5, "true");
  895. else
  896. statement.setString(5, "false");
  897. statement.execute();
  898. statement.close();
  899. }
  900. catch (Exception e)
  901. {
  902. _log.warning("Error could not store clan notice: " + e);
  903. }
  904. finally
  905. {
  906. try
  907. {
  908. con.close();
  909. }
  910. catch (Exception e)
  911. {
  912. }
  913. }
  914. _notice = notice;
  915. _noticeEnabled = enabled;
  916. }
  917. public void setNoticeEnabled(boolean enabled)
  918. {
  919. storeNotice(_notice, enabled);
  920. }
  921. public void setNotice(String notice)
  922. {
  923. storeNotice(notice, _noticeEnabled);
  924. }
  925. public boolean isNoticeEnabled()
  926. {
  927. return _noticeEnabled;
  928. }
  929. public String getNotice()
  930. {
  931. if (_notice == null)
  932. return "";
  933. return _notice;
  934. }
  935. private void restoreSkills()
  936. {
  937. Connection con = null;
  938. try
  939. {
  940. // Retrieve all skills of this L2PcInstance from the database
  941. con = L2DatabaseFactory.getInstance().getConnection();
  942. PreparedStatement statement = con.prepareStatement("SELECT skill_id,skill_level FROM clan_skills WHERE clan_id=?");
  943. statement.setInt(1, getClanId());
  944. ResultSet rset = statement.executeQuery();
  945. // Go though the recordset of this SQL query
  946. while (rset.next())
  947. {
  948. int id = rset.getInt("skill_id");
  949. int level = rset.getInt("skill_level");
  950. // Create a L2Skill object for each record
  951. L2Skill skill = SkillTable.getInstance().getInfo(id, level);
  952. // Add the L2Skill object to the L2Clan _skills
  953. _skills.put(skill.getId(), skill);
  954. }
  955. rset.close();
  956. statement.close();
  957. }
  958. catch (Exception e)
  959. {
  960. _log.log(Level.SEVERE, "Error restoring clan skills.", e);
  961. }
  962. finally
  963. {
  964. try { con.close(); } catch (Exception e) {}
  965. }
  966. }
  967. /** used to retrieve all skills */
  968. public final L2Skill[] getAllSkills()
  969. {
  970. if (_skills == null)
  971. return new L2Skill[0];
  972. return _skills.values().toArray(new L2Skill[_skills.values().size()]);
  973. }
  974. /** used to add a skill to skill list of this L2Clan */
  975. public L2Skill addSkill(L2Skill newSkill)
  976. {
  977. L2Skill oldSkill = null;
  978. if (newSkill != null)
  979. {
  980. // Replace oldSkill by newSkill or Add the newSkill
  981. oldSkill = _skills.put(newSkill.getId(), newSkill);
  982. }
  983. return oldSkill;
  984. }
  985. /** used to add a new skill to the list, send a packet to all online clan members, update their stats and store it in db*/
  986. public L2Skill addNewSkill(L2Skill newSkill)
  987. {
  988. L2Skill oldSkill = null;
  989. Connection con = null;
  990. if (newSkill != null)
  991. {
  992. // Replace oldSkill by newSkill or Add the newSkill
  993. oldSkill = _skills.put(newSkill.getId(), newSkill);
  994. try
  995. {
  996. con = L2DatabaseFactory.getInstance().getConnection();
  997. PreparedStatement statement;
  998. if (oldSkill != null)
  999. {
  1000. statement = con.prepareStatement("UPDATE clan_skills SET skill_level=? WHERE skill_id=? AND clan_id=?");
  1001. statement.setInt(1, newSkill.getLevel());
  1002. statement.setInt(2, oldSkill.getId());
  1003. statement.setInt(3, getClanId());
  1004. statement.execute();
  1005. statement.close();
  1006. }
  1007. else
  1008. {
  1009. statement = con.prepareStatement("INSERT INTO clan_skills (clan_id,skill_id,skill_level,skill_name) VALUES (?,?,?,?)");
  1010. statement.setInt(1, getClanId());
  1011. statement.setInt(2, newSkill.getId());
  1012. statement.setInt(3, newSkill.getLevel());
  1013. statement.setString(4, newSkill.getName());
  1014. statement.execute();
  1015. statement.close();
  1016. }
  1017. }
  1018. catch (Exception e)
  1019. {
  1020. _log.warning("Error could not store clan skills: " + e);
  1021. }
  1022. finally
  1023. {
  1024. try { con.close(); } catch (Exception e) {}
  1025. }
  1026. for (L2ClanMember temp : _members.values())
  1027. {
  1028. try {
  1029. if (temp.isOnline())
  1030. {
  1031. if (newSkill.getMinPledgeClass() <= temp.getPlayerInstance().getPledgeClass())
  1032. {
  1033. temp.getPlayerInstance().addSkill(newSkill, false); // Skill is not saved to player DB
  1034. temp.getPlayerInstance().sendPacket(new PledgeSkillListAdd(newSkill.getId(), newSkill.getLevel()));
  1035. }
  1036. }
  1037. } catch (NullPointerException e) {}
  1038. }
  1039. }
  1040. return oldSkill;
  1041. }
  1042. public void addSkillEffects()
  1043. {
  1044. for(L2Skill skill : _skills.values())
  1045. {
  1046. for (L2ClanMember temp : _members.values())
  1047. {
  1048. try{
  1049. if (temp.isOnline())
  1050. {
  1051. if (skill.getMinPledgeClass() <= temp.getPlayerInstance().getPledgeClass())
  1052. temp.getPlayerInstance().addSkill(skill, false); // Skill is not saved to player DB
  1053. }
  1054. } catch (NullPointerException e) {}
  1055. }
  1056. }
  1057. }
  1058. public void addSkillEffects(L2PcInstance cm)
  1059. {
  1060. if (cm == null)
  1061. return;
  1062. for(L2Skill skill : _skills.values())
  1063. {
  1064. //TODO add skills according to members class( in ex. don't add Clan Agillity skill's effect to lower class then Baron)
  1065. if (skill.getMinPledgeClass() <= cm.getPledgeClass())
  1066. cm.addSkill(skill, false); // Skill is not saved to player DB
  1067. }
  1068. }
  1069. /*
  1070. public void broadcastToOnlineAllyMembers(L2GameServerPacket packet)
  1071. {
  1072. if (getAllyId() == 0)
  1073. {
  1074. return;
  1075. }
  1076. for (L2Clan clan : _allyList.values())
  1077. {
  1078. clan.broadcastToOnlineMembers(packet);
  1079. }
  1080. }
  1081. */
  1082. public void broadcastToOnlineAllyMembers(L2GameServerPacket packet)
  1083. {
  1084. if (getAllyId() == 0)
  1085. {
  1086. return;
  1087. }
  1088. for (L2Clan clan : ClanTable.getInstance().getClans())
  1089. {
  1090. if (clan.getAllyId() == getAllyId())
  1091. {
  1092. clan.broadcastToOnlineMembers(packet);
  1093. }
  1094. }
  1095. }
  1096. public void broadcastToOnlineMembers(L2GameServerPacket packet)
  1097. {
  1098. for (L2ClanMember member : _members.values())
  1099. {
  1100. try {
  1101. if (member.isOnline())
  1102. member.getPlayerInstance().sendPacket(packet);
  1103. } catch (NullPointerException e) {}
  1104. }
  1105. }
  1106. public void broadcastCSToOnlineMembers(CreatureSay packet, L2PcInstance broadcaster)
  1107. {
  1108. for (L2ClanMember member : _members.values())
  1109. {
  1110. try {
  1111. if (member.isOnline() && !BlockList.isBlocked(member.getPlayerInstance(), broadcaster))
  1112. member.getPlayerInstance().sendPacket(packet);
  1113. } catch (NullPointerException e) {}
  1114. }
  1115. }
  1116. public void broadcastToOtherOnlineMembers(L2GameServerPacket packet, L2PcInstance player)
  1117. {
  1118. for (L2ClanMember member : _members.values())
  1119. {
  1120. try {
  1121. if (member.isOnline() && member.getPlayerInstance() != player)
  1122. member.getPlayerInstance().sendPacket(packet);
  1123. } catch (NullPointerException e) {}
  1124. }
  1125. }
  1126. @Override
  1127. public String toString()
  1128. {
  1129. return getName();
  1130. }
  1131. /**
  1132. * @return
  1133. */
  1134. public boolean hasCrest()
  1135. {
  1136. return _hasCrest;
  1137. }
  1138. public boolean hasCrestLarge()
  1139. {
  1140. return _hasCrestLarge;
  1141. }
  1142. public void setHasCrest(boolean flag)
  1143. {
  1144. _hasCrest = flag;
  1145. }
  1146. public void setHasCrestLarge(boolean flag)
  1147. {
  1148. _hasCrestLarge = flag;
  1149. }
  1150. public ItemContainer getWarehouse()
  1151. {
  1152. return _warehouse;
  1153. }
  1154. public boolean isAtWarWith(Integer id)
  1155. {
  1156. if (_atWarWith != null && !_atWarWith.isEmpty())
  1157. if (_atWarWith.contains(id)) return true;
  1158. return false;
  1159. }
  1160. public boolean isAtWarAttacker(Integer id)
  1161. {
  1162. if (_atWarAttackers != null && !_atWarAttackers.isEmpty())
  1163. if (_atWarAttackers.contains(id)) return true;
  1164. return false;
  1165. }
  1166. public void setEnemyClan(L2Clan clan)
  1167. {
  1168. Integer id = clan.getClanId();
  1169. _atWarWith.add(id);
  1170. }
  1171. public void setEnemyClan(Integer clan)
  1172. {
  1173. _atWarWith.add(clan);
  1174. }
  1175. public void setAttackerClan(L2Clan clan)
  1176. {
  1177. Integer id = clan.getClanId();
  1178. _atWarAttackers.add(id);
  1179. }
  1180. public void setAttackerClan(Integer clan)
  1181. {
  1182. _atWarAttackers.add(clan);
  1183. }
  1184. public void deleteEnemyClan(L2Clan clan)
  1185. {
  1186. Integer id = clan.getClanId();
  1187. _atWarWith.remove(id);
  1188. }
  1189. public void deleteAttackerClan(L2Clan clan)
  1190. {
  1191. Integer id = clan.getClanId();
  1192. _atWarAttackers.remove(id);
  1193. }
  1194. public int getHiredGuards(){ return _hiredGuards; }
  1195. public void incrementHiredGuards(){ _hiredGuards++; }
  1196. public boolean isAtWar()
  1197. {
  1198. if (_atWarWith != null && !_atWarWith.isEmpty())
  1199. return true;
  1200. return false;
  1201. }
  1202. public List<Integer> getWarList()
  1203. {
  1204. return _atWarWith;
  1205. }
  1206. public List<Integer> getAttackerList()
  1207. {
  1208. return _atWarAttackers;
  1209. }
  1210. public void broadcastClanStatus()
  1211. {
  1212. for(L2PcInstance member: getOnlineMembers(0))
  1213. {
  1214. member.sendPacket(new PledgeShowMemberListDeleteAll());
  1215. member.sendPacket(new PledgeShowMemberListAll(this, member));
  1216. }
  1217. }
  1218. public void removeSkill(int id)
  1219. {
  1220. L2Skill deleteSkill = null;
  1221. for(L2Skill sk : _skillList)
  1222. {
  1223. if(sk.getId() == id)
  1224. {
  1225. deleteSkill = sk;
  1226. return;
  1227. }
  1228. }
  1229. _skillList.remove(deleteSkill);
  1230. }
  1231. public void removeSkill(L2Skill deleteSkill)
  1232. {
  1233. _skillList.remove(deleteSkill);
  1234. }
  1235. /**
  1236. * @return
  1237. */
  1238. public List<L2Skill> getSkills()
  1239. {
  1240. return _skillList;
  1241. }
  1242. public class SubPledge
  1243. {
  1244. private int _id;
  1245. private String _subPledgeName;
  1246. private int _leaderId;
  1247. public SubPledge(int id, String name, int leaderId)
  1248. {
  1249. _id = id;
  1250. _subPledgeName = name;
  1251. _leaderId = leaderId;
  1252. }
  1253. public int getId()
  1254. {
  1255. return _id;
  1256. }
  1257. public String getName()
  1258. {
  1259. return _subPledgeName;
  1260. }
  1261. public int getLeaderId()
  1262. {
  1263. return _leaderId;
  1264. }
  1265. public void setLeaderId(int leaderId)
  1266. {
  1267. _leaderId = leaderId;
  1268. }
  1269. }
  1270. public class RankPrivs
  1271. {
  1272. private int _rankId;
  1273. private int _party;// TODO find out what this stuff means and implement it
  1274. private int _rankPrivs;
  1275. public RankPrivs(int rank, int party, int privs)
  1276. {
  1277. _rankId = rank;
  1278. _party = party;
  1279. _rankPrivs = privs;
  1280. }
  1281. public int getRank()
  1282. {
  1283. return _rankId;
  1284. }
  1285. public int getParty()
  1286. {
  1287. return _party;
  1288. }
  1289. public int getPrivs()
  1290. {
  1291. return _rankPrivs;
  1292. }
  1293. public void setPrivs(int privs)
  1294. {
  1295. _rankPrivs = privs;
  1296. }
  1297. }
  1298. private void restoreSubPledges()
  1299. {
  1300. Connection con = null;
  1301. try
  1302. {
  1303. // Retrieve all subpledges of this clan from the database
  1304. con = L2DatabaseFactory.getInstance().getConnection();
  1305. PreparedStatement statement = con.prepareStatement("SELECT sub_pledge_id,name,leader_id FROM clan_subpledges WHERE clan_id=?");
  1306. statement.setInt(1, getClanId());
  1307. ResultSet rset = statement.executeQuery();
  1308. while (rset.next())
  1309. {
  1310. int id = rset.getInt("sub_pledge_id");
  1311. String name = rset.getString("name");
  1312. int leaderId = rset.getInt("leader_id");
  1313. // Create a SubPledge object for each record
  1314. SubPledge pledge = new SubPledge(id, name, leaderId);
  1315. _subPledges.put(id, pledge);
  1316. }
  1317. rset.close();
  1318. statement.close();
  1319. }
  1320. catch (Exception e)
  1321. {
  1322. _log.warning("Could not restore clan sub-units: " + e);
  1323. }
  1324. finally
  1325. {
  1326. try { con.close(); } catch (Exception e) {}
  1327. }
  1328. }
  1329. /** used to retrieve subPledge by type */
  1330. public final SubPledge getSubPledge(int pledgeType)
  1331. {
  1332. if (_subPledges == null)
  1333. return null;
  1334. return _subPledges.get(pledgeType);
  1335. }
  1336. /** used to retrieve subPledge by type */
  1337. public final SubPledge getSubPledge(String pledgeName)
  1338. {
  1339. if (_subPledges == null)
  1340. return null;
  1341. for (SubPledge sp : _subPledges.values())
  1342. {
  1343. if (sp.getName().equalsIgnoreCase(pledgeName))
  1344. {
  1345. return sp;
  1346. }
  1347. }
  1348. return null;
  1349. }
  1350. /** used to retrieve all subPledges */
  1351. public final SubPledge[] getAllSubPledges()
  1352. {
  1353. if (_subPledges == null)
  1354. return new SubPledge[0];
  1355. return _subPledges.values().toArray(new SubPledge[_subPledges.values().size()]);
  1356. }
  1357. public SubPledge createSubPledge(L2PcInstance player, int pledgeType, int leaderId, String subPledgeName)
  1358. {
  1359. SubPledge subPledge = null;
  1360. pledgeType = getAvailablePledgeTypes(pledgeType);
  1361. if (pledgeType == 0)
  1362. {
  1363. if (pledgeType == L2Clan.SUBUNIT_ACADEMY)
  1364. player.sendPacket(new SystemMessage(SystemMessageId.CLAN_HAS_ALREADY_ESTABLISHED_A_CLAN_ACADEMY));
  1365. else
  1366. player.sendMessage("You can't create any more sub-units of this type");
  1367. return null;
  1368. }
  1369. if (_leader.getObjectId()==leaderId)
  1370. {
  1371. player.sendMessage("Leader is not correct");
  1372. return null;
  1373. }
  1374. // Royal Guard 5000 points per each
  1375. // Order of Knights 10000 points per each
  1376. if(pledgeType != -1 &&
  1377. ((getReputationScore() < 5000 && pledgeType < L2Clan.SUBUNIT_KNIGHT1) ||
  1378. (getReputationScore() < 10000 && pledgeType > L2Clan.SUBUNIT_ROYAL2)))
  1379. {
  1380. SystemMessage sp = new SystemMessage(SystemMessageId.THE_CLAN_REPUTATION_SCORE_IS_TOO_LOW);
  1381. player.sendPacket(sp);
  1382. return null;
  1383. }
  1384. else
  1385. {
  1386. Connection con = null;
  1387. try
  1388. {
  1389. con = L2DatabaseFactory.getInstance().getConnection();
  1390. PreparedStatement statement = con.prepareStatement("INSERT INTO clan_subpledges (clan_id,sub_pledge_id,name,leader_id) values (?,?,?,?)");
  1391. statement.setInt(1, getClanId());
  1392. statement.setInt(2, pledgeType);
  1393. statement.setString(3, subPledgeName);
  1394. if (pledgeType != -1)
  1395. statement.setInt(4, leaderId);
  1396. else
  1397. statement.setInt(4, 0);
  1398. statement.execute();
  1399. statement.close();
  1400. subPledge = new SubPledge(pledgeType, subPledgeName, leaderId);
  1401. _subPledges.put(pledgeType, subPledge);
  1402. if(pledgeType != -1)
  1403. {
  1404. // Royal Guard 5000 points per each
  1405. // Order of Knights 10000 points per each
  1406. if (pledgeType < L2Clan.SUBUNIT_KNIGHT1)
  1407. setReputationScore(getReputationScore() - Config.ROYAL_GUARD_COST, true);
  1408. else
  1409. setReputationScore(getReputationScore() - Config.KNIGHT_UNIT_COST, true);
  1410. //TODO: clan lvl9 or more can reinforce knights cheaper if first knight unit already created, use Config.KNIGHT_REINFORCE_COST
  1411. }
  1412. if (Config.DEBUG) _log.fine("New sub_clan saved in db: "+getClanId()+"; "+pledgeType);
  1413. }
  1414. catch (Exception e)
  1415. {
  1416. _log.log(Level.SEVERE, "Error saving sub clan data.", e);
  1417. }
  1418. finally
  1419. {
  1420. try { con.close(); } catch (Exception e) {}
  1421. }
  1422. }
  1423. broadcastToOnlineMembers(new PledgeShowInfoUpdate(_leader.getClan()));
  1424. broadcastToOnlineMembers(new PledgeReceiveSubPledgeCreated(subPledge, _leader.getClan()));
  1425. return subPledge;
  1426. }
  1427. public int getAvailablePledgeTypes(int pledgeType)
  1428. {
  1429. if (_subPledges.get(pledgeType) != null)
  1430. {
  1431. //_log.warning("found sub-unit with id: "+pledgeType);
  1432. switch(pledgeType)
  1433. {
  1434. case SUBUNIT_ACADEMY:
  1435. return 0;
  1436. case SUBUNIT_ROYAL1:
  1437. pledgeType = getAvailablePledgeTypes(SUBUNIT_ROYAL2);
  1438. break;
  1439. case SUBUNIT_ROYAL2:
  1440. return 0;
  1441. case SUBUNIT_KNIGHT1:
  1442. pledgeType = getAvailablePledgeTypes(SUBUNIT_KNIGHT2);
  1443. break;
  1444. case SUBUNIT_KNIGHT2:
  1445. pledgeType = getAvailablePledgeTypes(SUBUNIT_KNIGHT3);
  1446. break;
  1447. case SUBUNIT_KNIGHT3:
  1448. pledgeType = getAvailablePledgeTypes(SUBUNIT_KNIGHT4);
  1449. break;
  1450. case SUBUNIT_KNIGHT4:
  1451. return 0;
  1452. }
  1453. }
  1454. return pledgeType;
  1455. }
  1456. public void updateSubPledgeInDB(int pledgeType)
  1457. {
  1458. Connection con = null;
  1459. try
  1460. {
  1461. con = L2DatabaseFactory.getInstance().getConnection();
  1462. PreparedStatement statement = con.prepareStatement("UPDATE clan_subpledges SET leader_id=? WHERE clan_id=? AND sub_pledge_id=?");
  1463. statement.setInt(1, getSubPledge(pledgeType).getLeaderId());
  1464. statement.setInt(2, getClanId());
  1465. statement.setInt(3, pledgeType);
  1466. statement.execute();
  1467. statement.close();
  1468. if (Config.DEBUG)
  1469. _log.fine("New subpledge leader saved in db: "+getClanId());
  1470. }
  1471. catch (Exception e)
  1472. {
  1473. _log.log(Level.SEVERE, "Error saving new sub clan leader.", e);
  1474. }
  1475. finally
  1476. {
  1477. try { con.close(); } catch (Exception e) {}
  1478. }
  1479. }
  1480. private void restoreRankPrivs()
  1481. {
  1482. Connection con = null;
  1483. try
  1484. {
  1485. // Retrieve all skills of this L2PcInstance from the database
  1486. con = L2DatabaseFactory.getInstance().getConnection();
  1487. PreparedStatement statement = con.prepareStatement("SELECT privs,rank,party FROM clan_privs WHERE clan_id=?");
  1488. statement.setInt(1, getClanId());
  1489. //_log.warning("clanPrivs restore for ClanId : "+getClanId());
  1490. ResultSet rset = statement.executeQuery();
  1491. // Go though the recordset of this SQL query
  1492. while (rset.next())
  1493. {
  1494. int rank = rset.getInt("rank");
  1495. //int party = rset.getInt("party");
  1496. int privileges = rset.getInt("privs");
  1497. // Create a SubPledge object for each record
  1498. if (rank == -1)
  1499. continue;
  1500. _privs.get(rank).setPrivs(privileges);
  1501. }
  1502. rset.close();
  1503. statement.close();
  1504. }
  1505. catch (Exception e)
  1506. {
  1507. _log.log(Level.SEVERE, "Error restoring clan privs by rank.", e);
  1508. }
  1509. finally
  1510. {
  1511. try { con.close(); } catch (Exception e) {}
  1512. }
  1513. }
  1514. public void initializePrivs()
  1515. {
  1516. RankPrivs privs;
  1517. for (int i=1; i < 10; i++)
  1518. {
  1519. privs = new RankPrivs(i, 0, CP_NOTHING);
  1520. _privs.put(i, privs);
  1521. }
  1522. }
  1523. public int getRankPrivs(int rank)
  1524. {
  1525. if (_privs.get(rank) != null)
  1526. return _privs.get(rank).getPrivs();
  1527. else
  1528. return CP_NOTHING;
  1529. }
  1530. public void setRankPrivs(int rank, int privs)
  1531. {
  1532. if (_privs.get(rank)!= null)
  1533. {
  1534. _privs.get(rank).setPrivs(privs);
  1535. Connection con = null;
  1536. try
  1537. {
  1538. //_log.warning("requested store clan privs in db for rank: "+rank+", privs: "+privs);
  1539. // Retrieve all skills of this L2PcInstance from the database
  1540. con = L2DatabaseFactory.getInstance().getConnection();
  1541. PreparedStatement statement = con.prepareStatement("INSERT INTO clan_privs (clan_id,rank,party,privs) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE privs = ?");
  1542. statement.setInt(1, getClanId());
  1543. statement.setInt(2, rank);
  1544. statement.setInt(3, 0);
  1545. statement.setInt(4, privs);
  1546. statement.setInt(5, privs);
  1547. statement.execute();
  1548. statement.close();
  1549. }
  1550. catch (Exception e)
  1551. {
  1552. _log.warning("Could not store clan privs for rank: " + e);
  1553. }
  1554. finally
  1555. {
  1556. try { con.close(); } catch (Exception e) {}
  1557. }
  1558. for (L2ClanMember cm : getMembers())
  1559. {
  1560. if (cm.isOnline())
  1561. if (cm.getPowerGrade() == rank)
  1562. if (cm.getPlayerInstance() != null)
  1563. {
  1564. cm.getPlayerInstance().setClanPrivileges(privs);
  1565. cm.getPlayerInstance().sendPacket(new UserInfo(cm.getPlayerInstance()));
  1566. cm.getPlayerInstance().sendPacket(new ExBrExtraUserInfo(cm.getPlayerInstance()));
  1567. }
  1568. }
  1569. broadcastClanStatus();
  1570. }
  1571. else
  1572. {
  1573. _privs.put(rank, new RankPrivs(rank, 0, privs));
  1574. Connection con = null;
  1575. try
  1576. {
  1577. //_log.warning("requested store clan new privs in db for rank: "+rank);
  1578. // Retrieve all skills of this L2PcInstance from the database
  1579. con = L2DatabaseFactory.getInstance().getConnection();
  1580. PreparedStatement statement = con.prepareStatement("INSERT INTO clan_privs (clan_id,rank,party,privs) VALUES (?,?,?,?)");
  1581. statement.setInt(1, getClanId());
  1582. statement.setInt(2, rank);
  1583. statement.setInt(3, 0);
  1584. statement.setInt(4, privs);
  1585. statement.execute();
  1586. statement.close();
  1587. }
  1588. catch (Exception e)
  1589. {
  1590. _log.warning("Could not create new rank and store clan privs for rank: " + e);
  1591. }
  1592. finally
  1593. {
  1594. try { con.close(); } catch (Exception e) {}
  1595. }
  1596. }
  1597. }
  1598. /** used to retrieve all RankPrivs */
  1599. public final RankPrivs[] getAllRankPrivs()
  1600. {
  1601. if (_privs == null)
  1602. return new RankPrivs[0];
  1603. return _privs.values().toArray(new RankPrivs[_privs.values().size()]);
  1604. }
  1605. public int getLeaderSubPledge(int leaderId)
  1606. {
  1607. int id = 0;
  1608. for (SubPledge sp : _subPledges.values())
  1609. {
  1610. if (sp.getLeaderId() == 0) continue;
  1611. if (sp.getLeaderId() == leaderId)
  1612. id = sp.getId();
  1613. }
  1614. return id;
  1615. }
  1616. public void setReputationScore(int value, boolean save)
  1617. {
  1618. if(_reputationScore >= 0 && value < 0)
  1619. {
  1620. broadcastToOnlineMembers(new SystemMessage(SystemMessageId.REPUTATION_POINTS_0_OR_LOWER_CLAN_SKILLS_DEACTIVATED));
  1621. L2Skill[] skills = getAllSkills();
  1622. for (L2ClanMember member : _members.values())
  1623. {
  1624. if (member.isOnline() && member.getPlayerInstance() != null)
  1625. {
  1626. for (L2Skill sk : skills)
  1627. member.getPlayerInstance().removeSkill(sk, false);
  1628. }
  1629. }
  1630. }
  1631. else if(_reputationScore < 0 && value >= 0)
  1632. {
  1633. broadcastToOnlineMembers(new SystemMessage(SystemMessageId.CLAN_SKILLS_WILL_BE_ACTIVATED_SINCE_REPUTATION_IS_0_OR_HIGHER));
  1634. L2Skill[] skills = getAllSkills();
  1635. for (L2ClanMember member : _members.values())
  1636. {
  1637. if (member.isOnline() && member.getPlayerInstance() != null)
  1638. {
  1639. for (L2Skill sk : skills)
  1640. {
  1641. if(sk.getMinPledgeClass() <= member.getPlayerInstance().getPledgeClass())
  1642. member.getPlayerInstance().addSkill(sk, false);
  1643. }
  1644. }
  1645. }
  1646. }
  1647. _reputationScore = value;
  1648. if(_reputationScore > 100000000) _reputationScore = 100000000;
  1649. if(_reputationScore < -100000000) _reputationScore = -100000000;
  1650. broadcastToOnlineMembers(new PledgeShowInfoUpdate(this));
  1651. if (save) updateClanInDB();
  1652. }
  1653. public int getReputationScore()
  1654. {
  1655. return _reputationScore;
  1656. }
  1657. public void setRank(int rank)
  1658. {
  1659. _rank = rank;
  1660. }
  1661. public int getRank()
  1662. {
  1663. return _rank;
  1664. }
  1665. public int getAuctionBiddedAt()
  1666. {
  1667. return _auctionBiddedAt;
  1668. }
  1669. public void setAuctionBiddedAt(int id, boolean storeInDb)
  1670. {
  1671. _auctionBiddedAt = id;
  1672. if(storeInDb)
  1673. {
  1674. Connection con = null;
  1675. try
  1676. {
  1677. con = L2DatabaseFactory.getInstance().getConnection();
  1678. PreparedStatement statement = con.prepareStatement("UPDATE clan_data SET auction_bid_at=? WHERE clan_id=?");
  1679. statement.setInt(1, id);
  1680. statement.setInt(2, getClanId());
  1681. statement.execute();
  1682. statement.close();
  1683. }
  1684. catch (Exception e)
  1685. {
  1686. _log.warning("Could not store auction for clan: " + e);
  1687. }
  1688. finally
  1689. {
  1690. try { con.close(); } catch (Exception e) {}
  1691. }
  1692. }
  1693. }
  1694. /**
  1695. * Checks if activeChar and target meet various conditions to join a clan
  1696. *
  1697. * @param activeChar
  1698. * @param target
  1699. * @param pledgeType
  1700. * @return
  1701. */
  1702. public boolean checkClanJoinCondition(L2PcInstance activeChar, L2PcInstance target, int pledgeType)
  1703. {
  1704. if (activeChar == null)
  1705. {
  1706. return false;
  1707. }
  1708. if ((activeChar.getClanPrivileges() & L2Clan.CP_CL_JOIN_CLAN) != L2Clan.CP_CL_JOIN_CLAN)
  1709. {
  1710. activeChar.sendPacket(new SystemMessage(SystemMessageId.YOU_ARE_NOT_AUTHORIZED_TO_DO_THAT));
  1711. return false;
  1712. }
  1713. if (target == null)
  1714. {
  1715. activeChar.sendPacket(new SystemMessage(SystemMessageId.YOU_HAVE_INVITED_THE_WRONG_TARGET));
  1716. return false;
  1717. }
  1718. if (activeChar.getObjectId() == target.getObjectId())
  1719. {
  1720. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_INVITE_YOURSELF));
  1721. return false;
  1722. }
  1723. if (getCharPenaltyExpiryTime() > System.currentTimeMillis())
  1724. {
  1725. SystemMessage sm = new SystemMessage(SystemMessageId.YOU_MUST_WAIT_BEFORE_ACCEPTING_A_NEW_MEMBER);
  1726. sm.addString(target.getName());
  1727. activeChar.sendPacket(sm);
  1728. sm = null;
  1729. return false;
  1730. }
  1731. if (target.getClanId() != 0)
  1732. {
  1733. SystemMessage sm = new SystemMessage(SystemMessageId.S1_WORKING_WITH_ANOTHER_CLAN);
  1734. sm.addString(target.getName());
  1735. activeChar.sendPacket(sm);
  1736. sm = null;
  1737. return false;
  1738. }
  1739. if (target.getClanJoinExpiryTime() > System.currentTimeMillis())
  1740. {
  1741. SystemMessage sm = new SystemMessage(SystemMessageId.C1_MUST_WAIT_BEFORE_JOINING_ANOTHER_CLAN);
  1742. sm.addString(target.getName());
  1743. activeChar.sendPacket(sm);
  1744. sm = null;
  1745. return false;
  1746. }
  1747. if ((target.getLevel() > 40 || target.getClassId().level() >= 2) && pledgeType == -1)
  1748. {
  1749. SystemMessage sm = new SystemMessage(SystemMessageId.S1_DOESNOT_MEET_REQUIREMENTS_TO_JOIN_ACADEMY);
  1750. sm.addString(target.getName());
  1751. activeChar.sendPacket(sm);
  1752. sm = null;
  1753. activeChar.sendPacket(new SystemMessage(SystemMessageId.ACADEMY_REQUIREMENTS));
  1754. return false;
  1755. }
  1756. if (getSubPledgeMembersCount(pledgeType) >= getMaxNrOfMembers(pledgeType))
  1757. {
  1758. if (pledgeType == 0)
  1759. {
  1760. SystemMessage sm = new SystemMessage(SystemMessageId.S1_CLAN_IS_FULL);
  1761. sm.addString(getName());
  1762. activeChar.sendPacket(sm);
  1763. sm = null;
  1764. }
  1765. else
  1766. {
  1767. activeChar.sendPacket(new SystemMessage(SystemMessageId.SUBCLAN_IS_FULL));
  1768. }
  1769. return false;
  1770. }
  1771. return true;
  1772. }
  1773. /**
  1774. * Checks if activeChar and target meet various conditions to join a clan
  1775. *
  1776. * @param activeChar
  1777. * @param target
  1778. * @return
  1779. */
  1780. public boolean checkAllyJoinCondition(L2PcInstance activeChar, L2PcInstance target)
  1781. {
  1782. if (activeChar == null)
  1783. {
  1784. return false;
  1785. }
  1786. if (activeChar.getAllyId() == 0 || !activeChar.isClanLeader() || activeChar.getClanId() != activeChar.getAllyId())
  1787. {
  1788. activeChar.sendPacket(new SystemMessage(SystemMessageId.FEATURE_ONLY_FOR_ALLIANCE_LEADER));
  1789. return false;
  1790. }
  1791. L2Clan leaderClan = activeChar.getClan();
  1792. if (leaderClan.getAllyPenaltyExpiryTime() > System.currentTimeMillis())
  1793. {
  1794. if (leaderClan.getAllyPenaltyType() == PENALTY_TYPE_DISMISS_CLAN)
  1795. {
  1796. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_INVITE_CLAN_WITHIN_1_DAY));
  1797. return false;
  1798. }
  1799. }
  1800. if (target == null)
  1801. {
  1802. activeChar.sendPacket(new SystemMessage(SystemMessageId.YOU_HAVE_INVITED_THE_WRONG_TARGET));
  1803. return false;
  1804. }
  1805. if (activeChar.getObjectId() == target.getObjectId())
  1806. {
  1807. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_INVITE_YOURSELF));
  1808. return false;
  1809. }
  1810. if (target.getClan() == null)
  1811. {
  1812. activeChar.sendPacket(new SystemMessage(SystemMessageId.TARGET_MUST_BE_IN_CLAN));
  1813. return false;
  1814. }
  1815. if (!target.isClanLeader())
  1816. {
  1817. SystemMessage sm = new SystemMessage(SystemMessageId.S1_IS_NOT_A_CLAN_LEADER);
  1818. sm.addString(target.getName());
  1819. activeChar.sendPacket(sm);
  1820. sm = null;
  1821. return false;
  1822. }
  1823. L2Clan targetClan = target.getClan();
  1824. if (target.getAllyId() != 0)
  1825. {
  1826. SystemMessage sm = new SystemMessage(SystemMessageId.S1_CLAN_ALREADY_MEMBER_OF_S2_ALLIANCE);
  1827. sm.addString(targetClan.getName());
  1828. sm.addString(targetClan.getAllyName());
  1829. activeChar.sendPacket(sm);
  1830. sm = null;
  1831. return false;
  1832. }
  1833. if (targetClan.getAllyPenaltyExpiryTime() > System.currentTimeMillis())
  1834. {
  1835. if (targetClan.getAllyPenaltyType() == PENALTY_TYPE_CLAN_LEAVED)
  1836. {
  1837. SystemMessage sm = new SystemMessage(SystemMessageId.S1_CANT_ENTER_ALLIANCE_WITHIN_1_DAY);
  1838. sm.addString(target.getClan().getName());
  1839. sm.addString(target.getClan().getAllyName());
  1840. activeChar.sendPacket(sm);
  1841. sm = null;
  1842. return false;
  1843. }
  1844. if (targetClan.getAllyPenaltyType() == PENALTY_TYPE_CLAN_DISMISSED)
  1845. {
  1846. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_ENTER_ALLIANCE_WITHIN_1_DAY));
  1847. return false;
  1848. }
  1849. }
  1850. if (activeChar.isInsideZone(L2Character.ZONE_SIEGE) && target.isInsideZone(L2Character.ZONE_SIEGE))
  1851. {
  1852. activeChar.sendPacket(new SystemMessage(SystemMessageId.OPPOSING_CLAN_IS_PARTICIPATING_IN_SIEGE));
  1853. return false;
  1854. }
  1855. if (leaderClan.isAtWarWith(targetClan.getClanId()))
  1856. {
  1857. activeChar.sendPacket(new SystemMessage(SystemMessageId.MAY_NOT_ALLY_CLAN_BATTLE));
  1858. return false;
  1859. }
  1860. int numOfClansInAlly = 0;
  1861. for (L2Clan clan : ClanTable.getInstance().getClans())
  1862. {
  1863. if (clan.getAllyId() == activeChar.getAllyId())
  1864. {
  1865. ++numOfClansInAlly;
  1866. }
  1867. }
  1868. if (numOfClansInAlly >= Config.ALT_MAX_NUM_OF_CLANS_IN_ALLY)
  1869. {
  1870. activeChar.sendPacket(new SystemMessage(SystemMessageId.YOU_HAVE_EXCEEDED_THE_LIMIT));
  1871. return false;
  1872. }
  1873. return true;
  1874. }
  1875. public long getAllyPenaltyExpiryTime()
  1876. {
  1877. return _allyPenaltyExpiryTime;
  1878. }
  1879. public int getAllyPenaltyType()
  1880. {
  1881. return _allyPenaltyType;
  1882. }
  1883. public void setAllyPenaltyExpiryTime(long expiryTime, int penaltyType)
  1884. {
  1885. _allyPenaltyExpiryTime = expiryTime;
  1886. _allyPenaltyType = penaltyType;
  1887. }
  1888. public long getCharPenaltyExpiryTime()
  1889. {
  1890. return _charPenaltyExpiryTime;
  1891. }
  1892. public void setCharPenaltyExpiryTime(long time)
  1893. {
  1894. _charPenaltyExpiryTime = time;
  1895. }
  1896. public long getDissolvingExpiryTime()
  1897. {
  1898. return _dissolvingExpiryTime;
  1899. }
  1900. public void setDissolvingExpiryTime(long time)
  1901. {
  1902. _dissolvingExpiryTime = time;
  1903. }
  1904. public void createAlly(L2PcInstance player, String allyName)
  1905. {
  1906. if (null == player)
  1907. return;
  1908. if (Config.DEBUG)
  1909. _log.fine(player.getObjectId() + "(" + player.getName() + ") requested ally creation from ");
  1910. if (!player.isClanLeader())
  1911. {
  1912. player.sendPacket(new SystemMessage(SystemMessageId.ONLY_CLAN_LEADER_CREATE_ALLIANCE));
  1913. return;
  1914. }
  1915. if (getAllyId() != 0)
  1916. {
  1917. player.sendPacket(new SystemMessage(SystemMessageId.ALREADY_JOINED_ALLIANCE));
  1918. return;
  1919. }
  1920. if (getLevel() < 5)
  1921. {
  1922. player.sendPacket(new SystemMessage(SystemMessageId.TO_CREATE_AN_ALLY_YOU_CLAN_MUST_BE_LEVEL_5_OR_HIGHER));
  1923. return;
  1924. }
  1925. if (getAllyPenaltyExpiryTime() > System.currentTimeMillis())
  1926. {
  1927. if (getAllyPenaltyType() == L2Clan.PENALTY_TYPE_DISSOLVE_ALLY)
  1928. {
  1929. player.sendPacket(new SystemMessage(SystemMessageId.CANT_CREATE_ALLIANCE_10_DAYS_DISOLUTION));
  1930. return;
  1931. }
  1932. }
  1933. if (getDissolvingExpiryTime() > System.currentTimeMillis())
  1934. {
  1935. player.sendPacket(new SystemMessage(SystemMessageId.YOU_MAY_NOT_CREATE_ALLY_WHILE_DISSOLVING));
  1936. return;
  1937. }
  1938. if (!Util.isAlphaNumeric(allyName))
  1939. {
  1940. player.sendPacket(new SystemMessage(SystemMessageId.INCORRECT_ALLIANCE_NAME));
  1941. return;
  1942. }
  1943. if (allyName.length() > 16 || allyName.length() < 2)
  1944. {
  1945. player.sendPacket(new SystemMessage(SystemMessageId.INCORRECT_ALLIANCE_NAME_LENGTH));
  1946. return;
  1947. }
  1948. if (ClanTable.getInstance().isAllyExists(allyName))
  1949. {
  1950. player.sendPacket(new SystemMessage(SystemMessageId.ALLIANCE_ALREADY_EXISTS));
  1951. return;
  1952. }
  1953. setAllyId(getClanId());
  1954. setAllyName(allyName.trim());
  1955. setAllyPenaltyExpiryTime(0, 0);
  1956. updateClanInDB();
  1957. player.sendPacket(new UserInfo(player));
  1958. player.sendPacket(new ExBrExtraUserInfo(player));
  1959. //TODO: Need correct message id
  1960. player.sendMessage("Alliance " + allyName + " has been created.");
  1961. }
  1962. public void dissolveAlly(L2PcInstance player)
  1963. {
  1964. if (getAllyId() == 0)
  1965. {
  1966. player.sendPacket(new SystemMessage(SystemMessageId.NO_CURRENT_ALLIANCES));
  1967. return;
  1968. }
  1969. if (!player.isClanLeader() || getClanId() != getAllyId())
  1970. {
  1971. player.sendPacket(new SystemMessage(SystemMessageId.FEATURE_ONLY_FOR_ALLIANCE_LEADER));
  1972. return;
  1973. }
  1974. if (player.isInsideZone(L2Character.ZONE_SIEGE))
  1975. {
  1976. player.sendPacket(new SystemMessage(SystemMessageId.CANNOT_DISSOLVE_ALLY_WHILE_IN_SIEGE));
  1977. return;
  1978. }
  1979. broadcastToOnlineAllyMembers(new SystemMessage(SystemMessageId.ALLIANCE_DISOLVED));
  1980. long currentTime = System.currentTimeMillis();
  1981. for (L2Clan clan : ClanTable.getInstance().getClans())
  1982. {
  1983. if (clan.getAllyId() == getAllyId() && clan.getClanId() != getClanId())
  1984. {
  1985. clan.setAllyId(0);
  1986. clan.setAllyName(null);
  1987. clan.setAllyPenaltyExpiryTime(0, 0);
  1988. clan.updateClanInDB();
  1989. }
  1990. }
  1991. setAllyId(0);
  1992. setAllyName(null);
  1993. setAllyPenaltyExpiryTime(
  1994. currentTime + Config.ALT_CREATE_ALLY_DAYS_WHEN_DISSOLVED * 86400000L,
  1995. L2Clan.PENALTY_TYPE_DISSOLVE_ALLY); //24*60*60*1000 = 86400000
  1996. updateClanInDB();
  1997. // The clan leader should take the XP penalty of a full death.
  1998. player.deathPenalty(false, false, false);
  1999. }
  2000. public boolean levelUpClan(L2PcInstance player)
  2001. {
  2002. if (!player.isClanLeader())
  2003. {
  2004. player.sendPacket(new SystemMessage(SystemMessageId.YOU_ARE_NOT_AUTHORIZED_TO_DO_THAT));
  2005. return false;
  2006. }
  2007. if (System.currentTimeMillis() < getDissolvingExpiryTime())
  2008. {
  2009. player.sendPacket(new SystemMessage(SystemMessageId.CANNOT_RISE_LEVEL_WHILE_DISSOLUTION_IN_PROGRESS));
  2010. return false;
  2011. }
  2012. boolean increaseClanLevel = false;
  2013. switch (getLevel())
  2014. {
  2015. case 0:
  2016. {
  2017. // Upgrade to 1
  2018. if (player.getSp() >= 20000 && player.getAdena() >= 650000)
  2019. {
  2020. if (player.reduceAdena("ClanLvl", 650000, player.getTarget(), true))
  2021. {
  2022. player.setSp(player.getSp() - 20000);
  2023. SystemMessage sp = new SystemMessage(SystemMessageId.SP_DECREASED_S1);
  2024. sp.addNumber(20000);
  2025. player.sendPacket(sp);
  2026. sp = null;
  2027. increaseClanLevel = true;
  2028. }
  2029. }
  2030. break;
  2031. }
  2032. case 1:
  2033. {
  2034. // Upgrade to 2
  2035. if (player.getSp() >= 100000 && player.getAdena() >= 2500000)
  2036. {
  2037. if (player.reduceAdena("ClanLvl", 2500000, player.getTarget(), true))
  2038. {
  2039. player.setSp(player.getSp() - 100000);
  2040. SystemMessage sp = new SystemMessage(SystemMessageId.SP_DECREASED_S1);
  2041. sp.addNumber(100000);
  2042. player.sendPacket(sp);
  2043. sp = null;
  2044. increaseClanLevel = true;
  2045. }
  2046. }
  2047. break;
  2048. }
  2049. case 2:
  2050. {
  2051. // Upgrade to 3
  2052. if (player.getSp() >= 350000 && player.getInventory().getItemByItemId(1419) != null)
  2053. {
  2054. // itemId 1419 == Blood Mark
  2055. if (player.destroyItemByItemId("ClanLvl", 1419, 1, player.getTarget(), false))
  2056. {
  2057. player.setSp(player.getSp() - 350000);
  2058. SystemMessage sp = new SystemMessage(SystemMessageId.SP_DECREASED_S1);
  2059. sp.addNumber(350000);
  2060. player.sendPacket(sp);
  2061. sp = null;
  2062. SystemMessage sm = new SystemMessage(SystemMessageId.S2_S1_DISAPPEARED);
  2063. sm.addItemName(1419);
  2064. sm.addItemNumber(1);
  2065. player.sendPacket(sm);
  2066. sm = null;
  2067. increaseClanLevel = true;
  2068. }
  2069. }
  2070. break;
  2071. }
  2072. case 3:
  2073. {
  2074. // Upgrade to 4
  2075. if (player.getSp() >= 1000000 && player.getInventory().getItemByItemId(3874) != null)
  2076. {
  2077. // itemId 3874 == Alliance Manifesto
  2078. if (player.destroyItemByItemId("ClanLvl", 3874, 1, player.getTarget(), false))
  2079. {
  2080. player.setSp(player.getSp() - 1000000);
  2081. SystemMessage sp = new SystemMessage(SystemMessageId.SP_DECREASED_S1);
  2082. sp.addNumber(1000000);
  2083. player.sendPacket(sp);
  2084. sp = null;
  2085. SystemMessage sm = new SystemMessage(SystemMessageId.S2_S1_DISAPPEARED);
  2086. sm.addItemName(3874);
  2087. sm.addItemNumber(1);
  2088. player.sendPacket(sm);
  2089. sm = null;
  2090. increaseClanLevel = true;
  2091. }
  2092. }
  2093. break;
  2094. }
  2095. case 4:
  2096. {
  2097. // Upgrade to 5
  2098. if (player.getSp() >= 2500000 && player.getInventory().getItemByItemId(3870) != null)
  2099. {
  2100. // itemId 3870 == Seal of Aspiration
  2101. if (player.destroyItemByItemId("ClanLvl", 3870, 1, player.getTarget(), false))
  2102. {
  2103. player.setSp(player.getSp() - 2500000);
  2104. SystemMessage sp = new SystemMessage(SystemMessageId.SP_DECREASED_S1);
  2105. sp.addNumber(2500000);
  2106. player.sendPacket(sp);
  2107. sp = null;
  2108. SystemMessage sm = new SystemMessage(SystemMessageId.S2_S1_DISAPPEARED);
  2109. sm.addItemName(3870);
  2110. sm.addItemNumber(1);
  2111. player.sendPacket(sm);
  2112. sm = null;
  2113. increaseClanLevel = true;
  2114. }
  2115. }
  2116. break;
  2117. }
  2118. case 5:
  2119. // Upgrade to 6
  2120. if(getReputationScore() >= Config.CLAN_LEVEL_6_COST && getMembersCount() >= 30)
  2121. {
  2122. setReputationScore(getReputationScore() - Config.CLAN_LEVEL_6_COST, true);
  2123. SystemMessage cr = new SystemMessage(SystemMessageId.S1_DEDUCTED_FROM_CLAN_REP);
  2124. cr.addNumber(Config.CLAN_LEVEL_6_COST);
  2125. player.sendPacket(cr);
  2126. cr = null;
  2127. increaseClanLevel = true;
  2128. }
  2129. break;
  2130. case 6:
  2131. // Upgrade to 7
  2132. if(getReputationScore() >= Config.CLAN_LEVEL_7_COST && getMembersCount() >= 80)
  2133. {
  2134. setReputationScore(getReputationScore() - Config.CLAN_LEVEL_7_COST, true);
  2135. SystemMessage cr = new SystemMessage(SystemMessageId.S1_DEDUCTED_FROM_CLAN_REP);
  2136. cr.addNumber(Config.CLAN_LEVEL_7_COST);
  2137. player.sendPacket(cr);
  2138. cr = null;
  2139. increaseClanLevel = true;
  2140. }
  2141. break;
  2142. case 7:
  2143. // Upgrade to 8
  2144. if(getReputationScore() >= Config.CLAN_LEVEL_8_COST && getMembersCount() >= 120)
  2145. {
  2146. setReputationScore(getReputationScore() - Config.CLAN_LEVEL_8_COST, true);
  2147. SystemMessage cr = new SystemMessage(SystemMessageId.S1_DEDUCTED_FROM_CLAN_REP);
  2148. cr.addNumber(Config.CLAN_LEVEL_8_COST);
  2149. player.sendPacket(cr);
  2150. cr = null;
  2151. increaseClanLevel = true;
  2152. }
  2153. break;
  2154. case 8:
  2155. // Upgrade to 9
  2156. if(getReputationScore() >= Config.CLAN_LEVEL_9_COST && player.getInventory().getItemByItemId(9910) != null && getMembersCount() >= 120)
  2157. {
  2158. // itemId 9910 == Blood Oath
  2159. if (player.destroyItemByItemId("ClanLvl", 9910, 150, player.getTarget(), false))
  2160. {
  2161. setReputationScore(getReputationScore() - Config.CLAN_LEVEL_9_COST, true);
  2162. SystemMessage cr = new SystemMessage(SystemMessageId.S1_DEDUCTED_FROM_CLAN_REP);
  2163. cr.addNumber(Config.CLAN_LEVEL_9_COST);
  2164. player.sendPacket(cr);
  2165. cr = null;
  2166. SystemMessage sm = new SystemMessage(SystemMessageId.S2_S1_DISAPPEARED);
  2167. sm.addItemName(9910);
  2168. sm.addItemNumber(150);
  2169. player.sendPacket(sm);
  2170. increaseClanLevel = true;
  2171. }
  2172. }
  2173. break;
  2174. case 9:
  2175. // Upgrade to 10
  2176. if(getReputationScore() >= Config.CLAN_LEVEL_10_COST && player.getInventory().getItemByItemId(9911) != null && getMembersCount() >= 140)
  2177. {
  2178. // itemId 9911 == Blood Alliance
  2179. if (player.destroyItemByItemId("ClanLvl", 9911, 5, player.getTarget(), false))
  2180. {
  2181. setReputationScore(getReputationScore() - Config.CLAN_LEVEL_10_COST, true);
  2182. SystemMessage cr = new SystemMessage(SystemMessageId.S1_DEDUCTED_FROM_CLAN_REP);
  2183. cr.addNumber(Config.CLAN_LEVEL_10_COST);
  2184. player.sendPacket(cr);
  2185. cr = null;
  2186. SystemMessage sm = new SystemMessage(SystemMessageId.S2_S1_DISAPPEARED);
  2187. sm.addItemName(9911);
  2188. sm.addItemNumber(5);
  2189. player.sendPacket(sm);
  2190. increaseClanLevel = true;
  2191. }
  2192. }
  2193. break;
  2194. default:
  2195. return false;
  2196. }
  2197. if (!increaseClanLevel)
  2198. {
  2199. SystemMessage sm = new SystemMessage(SystemMessageId.FAILED_TO_INCREASE_CLAN_LEVEL);
  2200. player.sendPacket(sm);
  2201. return false;
  2202. }
  2203. // the player should know that he has less sp now :p
  2204. StatusUpdate su = new StatusUpdate(player.getObjectId());
  2205. su.addAttribute(StatusUpdate.SP, player.getSp());
  2206. player.sendPacket(su);
  2207. ItemList il = new ItemList(player, false);
  2208. player.sendPacket(il);
  2209. changeLevel(getLevel() + 1);
  2210. return true;
  2211. }
  2212. public void changeLevel(int level)
  2213. {
  2214. Connection con = null;
  2215. try
  2216. {
  2217. con = L2DatabaseFactory.getInstance().getConnection();
  2218. PreparedStatement statement = con.prepareStatement("UPDATE clan_data SET clan_level = ? WHERE clan_id = ?");
  2219. statement.setInt(1, level);
  2220. statement.setInt(2, getClanId());
  2221. statement.execute();
  2222. statement.close();
  2223. }
  2224. catch (Exception e)
  2225. {
  2226. _log.warning("could not increase clan level:" + e);
  2227. }
  2228. finally
  2229. {
  2230. try { con.close(); } catch (Exception e) {}
  2231. }
  2232. setLevel(level);
  2233. if (getLeader().isOnline())
  2234. {
  2235. L2PcInstance leader = getLeader().getPlayerInstance();
  2236. if (4 < level)
  2237. {
  2238. SiegeManager.getInstance().addSiegeSkills(leader);
  2239. }
  2240. else if (5 > level)
  2241. {
  2242. SiegeManager.getInstance().removeSiegeSkills(leader);
  2243. }
  2244. if (4 < level)
  2245. {
  2246. leader.sendPacket(new SystemMessage(SystemMessageId.CLAN_CAN_ACCUMULATE_CLAN_REPUTATION_POINTS));
  2247. }
  2248. }
  2249. // notify all the members about it
  2250. broadcastToOnlineMembers(new SystemMessage(SystemMessageId.CLAN_LEVEL_INCREASED));
  2251. broadcastToOnlineMembers(new PledgeShowInfoUpdate(this));
  2252. /*
  2253. * Micht :
  2254. * - use PledgeShowInfoUpdate instead of PledgeStatusChanged
  2255. * to update clan level ingame
  2256. * - remove broadcastClanStatus() to avoid members duplication
  2257. */
  2258. //clan.broadcastToOnlineMembers(new PledgeStatusChanged(clan));
  2259. //clan.broadcastClanStatus();
  2260. }
  2261. }