L2Clan.java 72 KB

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