L2Clan.java 72 KB

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