L2Clan.java 70 KB

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