L2Clan.java 73 KB

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