L2Clan.java 77 KB

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