L2Clan.java 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.util.Collection;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.concurrent.atomic.AtomicInteger;
  28. import java.util.logging.Level;
  29. import java.util.logging.Logger;
  30. import javolution.util.FastList;
  31. import javolution.util.FastMap;
  32. import com.l2jserver.Config;
  33. import com.l2jserver.L2DatabaseFactory;
  34. import com.l2jserver.gameserver.communitybbs.BB.Forum;
  35. import com.l2jserver.gameserver.communitybbs.Manager.ForumsBBSManager;
  36. import com.l2jserver.gameserver.datatables.CharNameTable;
  37. import com.l2jserver.gameserver.datatables.ClanTable;
  38. import com.l2jserver.gameserver.datatables.CrestTable;
  39. import com.l2jserver.gameserver.datatables.SkillData;
  40. import com.l2jserver.gameserver.instancemanager.CastleManager;
  41. import com.l2jserver.gameserver.instancemanager.FortManager;
  42. import com.l2jserver.gameserver.instancemanager.SiegeManager;
  43. import com.l2jserver.gameserver.instancemanager.TerritoryWarManager;
  44. import com.l2jserver.gameserver.instancemanager.TerritoryWarManager.Territory;
  45. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  46. import com.l2jserver.gameserver.model.events.EventDispatcher;
  47. import com.l2jserver.gameserver.model.events.impl.character.player.clan.OnPlayerClanJoin;
  48. import com.l2jserver.gameserver.model.events.impl.character.player.clan.OnPlayerClanLeaderChange;
  49. import com.l2jserver.gameserver.model.events.impl.character.player.clan.OnPlayerClanLeft;
  50. import com.l2jserver.gameserver.model.events.impl.character.player.clan.OnPlayerClanLvlUp;
  51. import com.l2jserver.gameserver.model.interfaces.IIdentifiable;
  52. import com.l2jserver.gameserver.model.interfaces.INamable;
  53. import com.l2jserver.gameserver.model.itemcontainer.ClanWarehouse;
  54. import com.l2jserver.gameserver.model.itemcontainer.ItemContainer;
  55. import com.l2jserver.gameserver.model.skills.Skill;
  56. import com.l2jserver.gameserver.model.zone.ZoneId;
  57. import com.l2jserver.gameserver.network.SystemMessageId;
  58. import com.l2jserver.gameserver.network.communityserver.CommunityServerThread;
  59. import com.l2jserver.gameserver.network.communityserver.writepackets.WorldInfo;
  60. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  61. import com.l2jserver.gameserver.network.serverpackets.ExBrExtraUserInfo;
  62. import com.l2jserver.gameserver.network.serverpackets.ExSubPledgeSkillAdd;
  63. import com.l2jserver.gameserver.network.serverpackets.ItemList;
  64. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  65. import com.l2jserver.gameserver.network.serverpackets.PledgeReceiveSubPledgeCreated;
  66. import com.l2jserver.gameserver.network.serverpackets.PledgeShowInfoUpdate;
  67. import com.l2jserver.gameserver.network.serverpackets.PledgeShowMemberListAll;
  68. import com.l2jserver.gameserver.network.serverpackets.PledgeShowMemberListDeleteAll;
  69. import com.l2jserver.gameserver.network.serverpackets.PledgeShowMemberListUpdate;
  70. import com.l2jserver.gameserver.network.serverpackets.PledgeSkillList;
  71. import com.l2jserver.gameserver.network.serverpackets.PledgeSkillList.SubPledgeSkill;
  72. import com.l2jserver.gameserver.network.serverpackets.PledgeSkillListAdd;
  73. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  74. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  75. import com.l2jserver.gameserver.network.serverpackets.UserInfo;
  76. import com.l2jserver.gameserver.util.Util;
  77. import com.l2jserver.util.EnumIntBitmask;
  78. public class L2Clan implements IIdentifiable, INamable
  79. {
  80. private static final Logger _log = Logger.getLogger(L2Clan.class.getName());
  81. // SQL queries
  82. private static final String INSERT_CLAN_DATA = "INSERT INTO clan_data (clan_id,clan_name,clan_level,hasCastle,blood_alliance_count,blood_oath_count,ally_id,ally_name,leader_id,crest_id,crest_large_id,ally_crest_id,new_leader_id) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
  83. private static final String SELECT_CLAN_DATA = "SELECT * FROM clan_data where clan_id=?";
  84. // Ally Penalty Types
  85. /** Clan leaved ally */
  86. public static final int PENALTY_TYPE_CLAN_LEAVED = 1;
  87. /** Clan was dismissed from ally */
  88. public static final int PENALTY_TYPE_CLAN_DISMISSED = 2;
  89. /** Leader clan dismiss clan from ally */
  90. public static final int PENALTY_TYPE_DISMISS_CLAN = 3;
  91. /** Leader clan dissolve ally */
  92. public static final int PENALTY_TYPE_DISSOLVE_ALLY = 4;
  93. // Sub-unit types
  94. /** Clan subunit type of Academy */
  95. public static final int SUBUNIT_ACADEMY = -1;
  96. /** Clan subunit type of Royal Guard A */
  97. public static final int SUBUNIT_ROYAL1 = 100;
  98. /** Clan subunit type of Royal Guard B */
  99. public static final int SUBUNIT_ROYAL2 = 200;
  100. /** Clan subunit type of Order of Knights A-1 */
  101. public static final int SUBUNIT_KNIGHT1 = 1001;
  102. /** Clan subunit type of Order of Knights A-2 */
  103. public static final int SUBUNIT_KNIGHT2 = 1002;
  104. /** Clan subunit type of Order of Knights B-1 */
  105. public static final int SUBUNIT_KNIGHT3 = 2001;
  106. /** Clan subunit type of Order of Knights B-2 */
  107. public static final int SUBUNIT_KNIGHT4 = 2002;
  108. private String _name;
  109. private int _clanId;
  110. private L2ClanMember _leader;
  111. private final Map<Integer, L2ClanMember> _members = new FastMap<>();
  112. private String _allyName;
  113. private int _allyId;
  114. private int _level;
  115. private int _castleId;
  116. private int _fortId;
  117. private int _hideoutId;
  118. private int _hiredGuards;
  119. private int _crestId;
  120. private int _crestLargeId;
  121. private int _allyCrestId;
  122. private int _auctionBiddedAt = 0;
  123. private long _allyPenaltyExpiryTime;
  124. private int _allyPenaltyType;
  125. private long _charPenaltyExpiryTime;
  126. private long _dissolvingExpiryTime;
  127. private int _bloodAllianceCount;
  128. private int _bloodOathCount;
  129. private final ItemContainer _warehouse = new ClanWarehouse(this);
  130. private final List<Integer> _atWarWith = new FastList<>();
  131. private final List<Integer> _atWarAttackers = new FastList<>();
  132. private Forum _forum;
  133. /** FastMap(Integer, L2Skill) containing all skills of the L2Clan */
  134. private final Map<Integer, Skill> _skills = new FastMap<>();
  135. private final Map<Integer, RankPrivs> _privs = new FastMap<>();
  136. private final Map<Integer, SubPledge> _subPledges = new FastMap<>();
  137. private final Map<Integer, Skill> _subPledgeSkills = new FastMap<>();
  138. private int _reputationScore = 0;
  139. private int _rank = 0;
  140. private String _notice;
  141. private boolean _noticeEnabled = false;
  142. private static final int MAX_NOTICE_LENGTH = 8192;
  143. private int _newLeaderId;
  144. private final AtomicInteger _siegeKills = new AtomicInteger();
  145. private final AtomicInteger _siegeDeaths = new AtomicInteger();
  146. /**
  147. * Called if a clan is referenced only by id. In this case all other data needs to be fetched from db
  148. * @param clanId A valid clan Id to create and restore
  149. */
  150. public L2Clan(int clanId)
  151. {
  152. _clanId = clanId;
  153. initializePrivs();
  154. restore();
  155. getWarehouse().restore();
  156. }
  157. /**
  158. * Called only if a new clan is created
  159. * @param clanId A valid clan Id to create
  160. * @param clanName A valid clan name
  161. */
  162. public L2Clan(int clanId, String clanName)
  163. {
  164. _clanId = clanId;
  165. _name = clanName;
  166. initializePrivs();
  167. }
  168. /**
  169. * @return Returns the clanId.
  170. */
  171. @Override
  172. public int getId()
  173. {
  174. return _clanId;
  175. }
  176. /**
  177. * @param clanId The clanId to set.
  178. */
  179. public void setClanId(int clanId)
  180. {
  181. _clanId = clanId;
  182. }
  183. /**
  184. * @return Returns the leaderId.
  185. */
  186. public int getLeaderId()
  187. {
  188. return (_leader != null ? _leader.getObjectId() : 0);
  189. }
  190. /**
  191. * @return L2ClanMember of clan leader.
  192. */
  193. public L2ClanMember getLeader()
  194. {
  195. return _leader;
  196. }
  197. /**
  198. * @param leader the leader to set.
  199. */
  200. public void setLeader(L2ClanMember leader)
  201. {
  202. _leader = leader;
  203. _members.put(leader.getObjectId(), leader);
  204. }
  205. public void setNewLeader(L2ClanMember member)
  206. {
  207. final L2PcInstance newLeader = member.getPlayerInstance();
  208. final L2ClanMember exMember = getLeader();
  209. final L2PcInstance exLeader = exMember.getPlayerInstance();
  210. // Notify to scripts
  211. EventDispatcher.getInstance().notifyEventAsync(new OnPlayerClanLeaderChange(exMember, member, this));
  212. if (exLeader != null)
  213. {
  214. if (exLeader.isFlying())
  215. {
  216. exLeader.dismount();
  217. }
  218. if (getLevel() >= SiegeManager.getInstance().getSiegeClanMinLevel())
  219. {
  220. SiegeManager.getInstance().removeSiegeSkills(exLeader);
  221. }
  222. exLeader.getClanPrivileges().clear();
  223. exLeader.broadcastUserInfo();
  224. }
  225. else
  226. {
  227. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  228. PreparedStatement ps = con.prepareStatement("UPDATE characters SET clan_privs = ? WHERE charId = ?"))
  229. {
  230. ps.setInt(1, 0);
  231. ps.setInt(2, getLeaderId());
  232. ps.execute();
  233. }
  234. catch (Exception e)
  235. {
  236. _log.log(Level.WARNING, "Couldn't update clan privs for old clan leader", e);
  237. }
  238. }
  239. setLeader(member);
  240. if (getNewLeaderId() != 0)
  241. {
  242. setNewLeaderId(0, true);
  243. }
  244. updateClanInDB();
  245. if (exLeader != null)
  246. {
  247. exLeader.setPledgeClass(L2ClanMember.calculatePledgeClass(exLeader));
  248. exLeader.broadcastUserInfo();
  249. exLeader.checkItemRestriction();
  250. }
  251. if (newLeader != null)
  252. {
  253. newLeader.setPledgeClass(L2ClanMember.calculatePledgeClass(newLeader));
  254. newLeader.getClanPrivileges().setAll();
  255. if (getLevel() >= SiegeManager.getInstance().getSiegeClanMinLevel())
  256. {
  257. SiegeManager.getInstance().addSiegeSkills(newLeader);
  258. }
  259. newLeader.broadcastUserInfo();
  260. }
  261. else
  262. {
  263. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  264. PreparedStatement ps = con.prepareStatement("UPDATE characters SET clan_privs = ? WHERE charId = ?"))
  265. {
  266. ps.setInt(1, EnumIntBitmask.getAllBitmask(ClanPrivilege.class));
  267. ps.setInt(2, getLeaderId());
  268. ps.execute();
  269. }
  270. catch (Exception e)
  271. {
  272. _log.log(Level.WARNING, "Couldn't update clan privs for new clan leader", e);
  273. }
  274. }
  275. broadcastClanStatus();
  276. broadcastToOnlineMembers(SystemMessage.getSystemMessage(SystemMessageId.CLAN_LEADER_PRIVILEGES_HAVE_BEEN_TRANSFERRED_TO_C1).addString(member.getName()));
  277. if (CommunityServerThread.getInstance() != null)
  278. {
  279. CommunityServerThread.getInstance().sendPacket(new WorldInfo(null, this, WorldInfo.TYPE_UPDATE_CLAN_DATA));
  280. }
  281. _log.log(Level.INFO, "Leader of Clan: " + getName() + " changed to: " + member.getName() + " ex leader: " + exMember.getName());
  282. }
  283. /**
  284. * @return the clan leader's name.
  285. */
  286. public String getLeaderName()
  287. {
  288. if (_leader == null)
  289. {
  290. _log.warning(L2Clan.class.getName() + ": Clan " + getName() + " without clan leader!");
  291. return "";
  292. }
  293. return _leader.getName();
  294. }
  295. /**
  296. * @return the clan name.
  297. */
  298. @Override
  299. public String getName()
  300. {
  301. return _name;
  302. }
  303. /**
  304. * @param name The name to set.
  305. */
  306. public void setName(String name)
  307. {
  308. _name = name;
  309. }
  310. /**
  311. * Adds a clan member to the clan.
  312. * @param member the clan member.
  313. */
  314. private void addClanMember(L2ClanMember member)
  315. {
  316. _members.put(member.getObjectId(), member);
  317. }
  318. /**
  319. * Adds a clan member to the clan.<br>
  320. * Using a different constructor, to make it easier to read.
  321. * @param player the clan member
  322. */
  323. public void addClanMember(L2PcInstance player)
  324. {
  325. final L2ClanMember member = new L2ClanMember(this, player);
  326. // store in memory
  327. addClanMember(member);
  328. member.setPlayerInstance(player);
  329. player.setClan(this);
  330. player.setPledgeClass(L2ClanMember.calculatePledgeClass(player));
  331. player.sendPacket(new PledgeShowMemberListUpdate(player));
  332. player.sendPacket(new PledgeSkillList(this));
  333. addSkillEffects(player);
  334. // notify CB server about the change
  335. CommunityServerThread.getInstance().sendPacket(new WorldInfo(null, this, WorldInfo.TYPE_UPDATE_CLAN_DATA));
  336. // Notify to scripts
  337. EventDispatcher.getInstance().notifyEventAsync(new OnPlayerClanJoin(member, this));
  338. }
  339. /**
  340. * Updates player status in clan.
  341. * @param player the player to be updated.
  342. */
  343. public void updateClanMember(L2PcInstance player)
  344. {
  345. final L2ClanMember member = new L2ClanMember(player.getClan(), player);
  346. if (player.isClanLeader())
  347. {
  348. setLeader(member);
  349. }
  350. addClanMember(member);
  351. // notify CB server about the change
  352. CommunityServerThread.getInstance().sendPacket(new WorldInfo(null, this, WorldInfo.TYPE_UPDATE_CLAN_DATA));
  353. }
  354. /**
  355. * @param name the name of the required clan member.
  356. * @return the clan member for a given name.
  357. */
  358. public L2ClanMember getClanMember(String name)
  359. {
  360. for (L2ClanMember temp : _members.values())
  361. {
  362. if (temp.getName().equals(name))
  363. {
  364. return temp;
  365. }
  366. }
  367. return null;
  368. }
  369. /**
  370. * @param objectId the required clan member object Id.
  371. * @return the clan member for a given {@code objectId}.
  372. */
  373. public L2ClanMember getClanMember(int objectId)
  374. {
  375. return _members.get(objectId);
  376. }
  377. /**
  378. * @param objectId the object Id of the member that will be removed.
  379. * @param clanJoinExpiryTime time penalty to join a clan.
  380. */
  381. public void removeClanMember(int objectId, long clanJoinExpiryTime)
  382. {
  383. final L2ClanMember exMember = _members.remove(objectId);
  384. if (exMember == null)
  385. {
  386. _log.warning("Member Object ID: " + objectId + " not found in clan while trying to remove");
  387. return;
  388. }
  389. final int leadssubpledge = getLeaderSubPledge(objectId);
  390. if (leadssubpledge != 0)
  391. {
  392. // Sub-unit leader withdraws, position becomes vacant and leader
  393. // should appoint new via NPC
  394. getSubPledge(leadssubpledge).setLeaderId(0);
  395. updateSubPledgeInDB(leadssubpledge);
  396. }
  397. if (exMember.getApprentice() != 0)
  398. {
  399. final L2ClanMember apprentice = getClanMember(exMember.getApprentice());
  400. if (apprentice != null)
  401. {
  402. if (apprentice.getPlayerInstance() != null)
  403. {
  404. apprentice.getPlayerInstance().setSponsor(0);
  405. }
  406. else
  407. {
  408. apprentice.setApprenticeAndSponsor(0, 0);
  409. }
  410. apprentice.saveApprenticeAndSponsor(0, 0);
  411. }
  412. }
  413. if (exMember.getSponsor() != 0)
  414. {
  415. final L2ClanMember sponsor = getClanMember(exMember.getSponsor());
  416. if (sponsor != null)
  417. {
  418. if (sponsor.getPlayerInstance() != null)
  419. {
  420. sponsor.getPlayerInstance().setApprentice(0);
  421. }
  422. else
  423. {
  424. sponsor.setApprenticeAndSponsor(0, 0);
  425. }
  426. sponsor.saveApprenticeAndSponsor(0, 0);
  427. }
  428. }
  429. exMember.saveApprenticeAndSponsor(0, 0);
  430. if (Config.REMOVE_CASTLE_CIRCLETS)
  431. {
  432. CastleManager.getInstance().removeCirclet(exMember, getCastleId());
  433. }
  434. if (exMember.isOnline())
  435. {
  436. L2PcInstance player = exMember.getPlayerInstance();
  437. if (!player.isNoble())
  438. {
  439. player.setTitle("");
  440. }
  441. player.setApprentice(0);
  442. player.setSponsor(0);
  443. if (player.isClanLeader())
  444. {
  445. SiegeManager.getInstance().removeSiegeSkills(player);
  446. player.setClanCreateExpiryTime(System.currentTimeMillis() + (Config.ALT_CLAN_CREATE_DAYS * 86400000L)); // 24*60*60*1000 = 86400000
  447. }
  448. // remove Clan skills from Player
  449. removeSkillEffects(player);
  450. // remove Residential skills
  451. if (player.getClan().getCastleId() > 0)
  452. {
  453. CastleManager.getInstance().getCastleByOwner(player.getClan()).removeResidentialSkills(player);
  454. }
  455. if (player.getClan().getFortId() > 0)
  456. {
  457. FortManager.getInstance().getFortByOwner(player.getClan()).removeResidentialSkills(player);
  458. }
  459. player.sendSkillList();
  460. player.setClan(null);
  461. // players leaving from clan academy have no penalty
  462. if (exMember.getPledgeType() != -1)
  463. {
  464. player.setClanJoinExpiryTime(clanJoinExpiryTime);
  465. }
  466. player.setPledgeClass(L2ClanMember.calculatePledgeClass(player));
  467. player.broadcastUserInfo();
  468. // disable clan tab
  469. player.sendPacket(PledgeShowMemberListDeleteAll.STATIC_PACKET);
  470. }
  471. else
  472. {
  473. removeMemberInDatabase(exMember, clanJoinExpiryTime, getLeaderId() == objectId ? System.currentTimeMillis() + (Config.ALT_CLAN_CREATE_DAYS * 86400000L) : 0);
  474. }
  475. // notify CB server about the change
  476. if (CommunityServerThread.getInstance() != null)
  477. {
  478. CommunityServerThread.getInstance().sendPacket(new WorldInfo(null, this, WorldInfo.TYPE_UPDATE_CLAN_DATA));
  479. }
  480. // Notify to scripts
  481. EventDispatcher.getInstance().notifyEventAsync(new OnPlayerClanLeft(exMember, this));
  482. }
  483. public L2ClanMember[] getMembers()
  484. {
  485. return _members.values().toArray(new L2ClanMember[_members.size()]);
  486. }
  487. public int getMembersCount()
  488. {
  489. return _members.size();
  490. }
  491. public int getSubPledgeMembersCount(int subpl)
  492. {
  493. int result = 0;
  494. for (L2ClanMember temp : _members.values())
  495. {
  496. if (temp.getPledgeType() == subpl)
  497. {
  498. result++;
  499. }
  500. }
  501. return result;
  502. }
  503. /**
  504. * @param pledgeType the Id of the pledge type.
  505. * @return the maximum number of members allowed for a given {@code pledgeType}.
  506. */
  507. public int getMaxNrOfMembers(int pledgeType)
  508. {
  509. int limit = 0;
  510. switch (pledgeType)
  511. {
  512. case 0:
  513. switch (getLevel())
  514. {
  515. case 3:
  516. limit = 30;
  517. break;
  518. case 2:
  519. limit = 20;
  520. break;
  521. case 1:
  522. limit = 15;
  523. break;
  524. case 0:
  525. limit = 10;
  526. break;
  527. default:
  528. limit = 40;
  529. break;
  530. }
  531. break;
  532. case -1:
  533. limit = 20;
  534. break;
  535. case 100:
  536. case 200:
  537. switch (getLevel())
  538. {
  539. case 11:
  540. limit = 30;
  541. break;
  542. default:
  543. limit = 20;
  544. break;
  545. }
  546. break;
  547. case 1001:
  548. case 1002:
  549. case 2001:
  550. case 2002:
  551. switch (getLevel())
  552. {
  553. case 9:
  554. case 10:
  555. case 11:
  556. limit = 25;
  557. break;
  558. default:
  559. limit = 10;
  560. break;
  561. }
  562. break;
  563. default:
  564. break;
  565. }
  566. return limit;
  567. }
  568. /**
  569. * @param exclude the object Id to exclude from list.
  570. * @return all online members excluding the one with object id {code exclude}.
  571. */
  572. public FastList<L2PcInstance> getOnlineMembers(int exclude)
  573. {
  574. final FastList<L2PcInstance> onlineMembers = new FastList<>();
  575. for (L2ClanMember temp : _members.values())
  576. {
  577. if ((temp != null) && temp.isOnline() && (temp.getObjectId() != exclude))
  578. {
  579. onlineMembers.add(temp.getPlayerInstance());
  580. }
  581. }
  582. return onlineMembers;
  583. }
  584. /**
  585. * @return the online clan member count.
  586. */
  587. public int getOnlineMembersCount()
  588. {
  589. int count = 0;
  590. for (L2ClanMember temp : _members.values())
  591. {
  592. if ((temp == null) || !temp.isOnline())
  593. {
  594. continue;
  595. }
  596. count++;
  597. }
  598. return count;
  599. }
  600. /**
  601. * @return the alliance Id.
  602. */
  603. public int getAllyId()
  604. {
  605. return _allyId;
  606. }
  607. /**
  608. * @return the alliance name.
  609. */
  610. public String getAllyName()
  611. {
  612. return _allyName;
  613. }
  614. /**
  615. * @param allyCrestId the alliance crest Id to be set.
  616. */
  617. public void setAllyCrestId(int allyCrestId)
  618. {
  619. _allyCrestId = allyCrestId;
  620. }
  621. /**
  622. * @return the alliance crest Id.
  623. */
  624. public int getAllyCrestId()
  625. {
  626. return _allyCrestId;
  627. }
  628. /**
  629. * @return the clan level.
  630. */
  631. public int getLevel()
  632. {
  633. return _level;
  634. }
  635. /**
  636. * Sets the clan level and updates the clan forum if it's needed.
  637. * @param level the clan level to be set.
  638. */
  639. public void setLevel(int level)
  640. {
  641. _level = level;
  642. if ((_level >= 2) && (_forum == null) && (Config.COMMUNITY_TYPE > 0))
  643. {
  644. final Forum forum = ForumsBBSManager.getInstance().getForumByName("ClanRoot");
  645. if (forum != null)
  646. {
  647. _forum = forum.getChildByName(_name);
  648. if (_forum == null)
  649. {
  650. _forum = ForumsBBSManager.getInstance().createNewForum(_name, ForumsBBSManager.getInstance().getForumByName("ClanRoot"), Forum.CLAN, Forum.CLANMEMBERONLY, getId());
  651. }
  652. }
  653. }
  654. }
  655. /**
  656. * @return the castle Id for this clan if owns a castle, zero otherwise.
  657. */
  658. public int getCastleId()
  659. {
  660. return _castleId;
  661. }
  662. /**
  663. * @return the fort Id for this clan if owns a fort, zero otherwise.
  664. */
  665. public int getFortId()
  666. {
  667. return _fortId;
  668. }
  669. /**
  670. * @return the hideout Id for this clan if owns a hideout, zero otherwise.
  671. */
  672. public int getHideoutId()
  673. {
  674. return _hideoutId;
  675. }
  676. /**
  677. * @param crestId the Id of the clan crest to be set.
  678. */
  679. public void setCrestId(int crestId)
  680. {
  681. _crestId = crestId;
  682. }
  683. /**
  684. * @return Returns the clanCrestId.
  685. */
  686. public int getCrestId()
  687. {
  688. return _crestId;
  689. }
  690. /**
  691. * @param crestLargeId The id of pledge LargeCrest.
  692. */
  693. public void setCrestLargeId(int crestLargeId)
  694. {
  695. _crestLargeId = crestLargeId;
  696. }
  697. /**
  698. * @return Returns the clan CrestLargeId
  699. */
  700. public int getCrestLargeId()
  701. {
  702. return _crestLargeId;
  703. }
  704. /**
  705. * @param allyId The allyId to set.
  706. */
  707. public void setAllyId(int allyId)
  708. {
  709. _allyId = allyId;
  710. }
  711. /**
  712. * @param allyName The allyName to set.
  713. */
  714. public void setAllyName(String allyName)
  715. {
  716. _allyName = allyName;
  717. }
  718. /**
  719. * @param castleId the castle Id to set.
  720. */
  721. public void setCastleId(int castleId)
  722. {
  723. _castleId = castleId;
  724. }
  725. /**
  726. * @param fortId the fort Id to set.
  727. */
  728. public void setFortId(int fortId)
  729. {
  730. _fortId = fortId;
  731. }
  732. /**
  733. * @param hideoutId the hideout Id to set.
  734. */
  735. public void setHideoutId(int hideoutId)
  736. {
  737. _hideoutId = hideoutId;
  738. }
  739. /**
  740. * @param id the Id of the player to be verified.
  741. * @return {code true} if the player belongs to the clan.
  742. */
  743. public boolean isMember(int id)
  744. {
  745. return (id == 0 ? false : _members.containsKey(id));
  746. }
  747. /**
  748. * @return the Blood Alliance count for this clan
  749. */
  750. public int getBloodAllianceCount()
  751. {
  752. return _bloodAllianceCount;
  753. }
  754. /**
  755. * Increase Blood Alliance count by config predefined count and updates the database.
  756. */
  757. public void increaseBloodAllianceCount()
  758. {
  759. _bloodAllianceCount += SiegeManager.getInstance().getBloodAllianceReward();
  760. updateBloodAllianceCountInDB();
  761. }
  762. /**
  763. * Reset the Blood Alliance count to zero and updates the database.
  764. */
  765. public void resetBloodAllianceCount()
  766. {
  767. _bloodAllianceCount = 0;
  768. updateBloodAllianceCountInDB();
  769. }
  770. /**
  771. * Store current Bloood Alliances count in database.
  772. */
  773. public void updateBloodAllianceCountInDB()
  774. {
  775. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  776. PreparedStatement ps = con.prepareStatement("UPDATE clan_data SET blood_alliance_count=? WHERE clan_id=?"))
  777. {
  778. ps.setInt(1, getBloodAllianceCount());
  779. ps.setInt(2, getId());
  780. ps.execute();
  781. }
  782. catch (Exception e)
  783. {
  784. _log.log(Level.WARNING, "Exception on updateBloodAllianceCountInDB(): " + e.getMessage(), e);
  785. }
  786. }
  787. /**
  788. * @return the Blood Oath count for this clan
  789. */
  790. public int getBloodOathCount()
  791. {
  792. return _bloodOathCount;
  793. }
  794. /**
  795. * Increase Blood Oath count by config predefined count and updates the database.
  796. */
  797. public void increaseBloodOathCount()
  798. {
  799. _bloodOathCount += Config.FS_BLOOD_OATH_COUNT;
  800. updateBloodOathCountInDB();
  801. }
  802. /**
  803. * Reset the Blood Oath count to zero and updates the database.
  804. */
  805. public void resetBloodOathCount()
  806. {
  807. _bloodOathCount = 0;
  808. updateBloodOathCountInDB();
  809. }
  810. /**
  811. * Store current Bloood Alliances count in database.
  812. */
  813. public void updateBloodOathCountInDB()
  814. {
  815. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  816. PreparedStatement ps = con.prepareStatement("UPDATE clan_data SET blood_oath_count=? WHERE clan_id=?"))
  817. {
  818. ps.setInt(1, getBloodOathCount());
  819. ps.setInt(2, getId());
  820. ps.execute();
  821. }
  822. catch (Exception e)
  823. {
  824. _log.log(Level.WARNING, "Exception on updateBloodAllianceCountInDB(): " + e.getMessage(), e);
  825. }
  826. }
  827. /**
  828. * Store in database current clan's reputation.
  829. */
  830. public void updateClanScoreInDB()
  831. {
  832. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  833. PreparedStatement ps = con.prepareStatement("UPDATE clan_data SET reputation_score=? WHERE clan_id=?"))
  834. {
  835. ps.setInt(1, getReputationScore());
  836. ps.setInt(2, getId());
  837. ps.execute();
  838. }
  839. catch (Exception e)
  840. {
  841. _log.log(Level.WARNING, "Exception on updateClanScoreInDb(): " + e.getMessage(), e);
  842. }
  843. }
  844. /**
  845. * Updates in database clan information:
  846. * <ul>
  847. * <li>Clan leader Id</li>
  848. * <li>Alliance Id</li>
  849. * <li>Alliance name</li>
  850. * <li>Clan's reputation</li>
  851. * <li>Alliance's penalty expiration time</li>
  852. * <li>Alliance's penalty type</li>
  853. * <li>Character's penalty expiration time</li>
  854. * <li>Dissolving expiration time</li>
  855. * <li>Clan's id</li>
  856. * </ul>
  857. */
  858. public void updateClanInDB()
  859. {
  860. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  861. PreparedStatement ps = 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=?,new_leader_id=? WHERE clan_id=?"))
  862. {
  863. ps.setInt(1, getLeaderId());
  864. ps.setInt(2, getAllyId());
  865. ps.setString(3, getAllyName());
  866. ps.setInt(4, getReputationScore());
  867. ps.setLong(5, getAllyPenaltyExpiryTime());
  868. ps.setInt(6, getAllyPenaltyType());
  869. ps.setLong(7, getCharPenaltyExpiryTime());
  870. ps.setLong(8, getDissolvingExpiryTime());
  871. ps.setInt(9, getNewLeaderId());
  872. ps.setInt(10, getId());
  873. ps.execute();
  874. if (Config.DEBUG)
  875. {
  876. _log.fine("New clan leader saved in db: " + getId());
  877. }
  878. }
  879. catch (Exception e)
  880. {
  881. _log.log(Level.SEVERE, "Error saving clan: " + e.getMessage(), e);
  882. }
  883. }
  884. /**
  885. * Stores in database clan information:
  886. * <ul>
  887. * <li>Clan Id</li>
  888. * <li>Clan name</li>
  889. * <li>Clan level</li>
  890. * <li>Has castle</li>
  891. * <li>Alliance Id</li>
  892. * <li>Alliance name</li>
  893. * <li>Clan leader Id</li>
  894. * <li>Clan crest Id</li>
  895. * <li>Clan large crest Id</li>
  896. * <li>Alliance crest Id</li>
  897. * </ul>
  898. */
  899. public void store()
  900. {
  901. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  902. PreparedStatement ps = con.prepareStatement(INSERT_CLAN_DATA))
  903. {
  904. ps.setInt(1, getId());
  905. ps.setString(2, getName());
  906. ps.setInt(3, getLevel());
  907. ps.setInt(4, getCastleId());
  908. ps.setInt(5, getBloodAllianceCount());
  909. ps.setInt(6, getBloodOathCount());
  910. ps.setInt(7, getAllyId());
  911. ps.setString(8, getAllyName());
  912. ps.setInt(9, getLeaderId());
  913. ps.setInt(10, getCrestId());
  914. ps.setInt(11, getCrestLargeId());
  915. ps.setInt(12, getAllyCrestId());
  916. ps.setInt(13, getNewLeaderId());
  917. ps.execute();
  918. if (Config.DEBUG)
  919. {
  920. _log.fine("New clan saved in db: " + getId());
  921. }
  922. }
  923. catch (Exception e)
  924. {
  925. _log.log(Level.SEVERE, "Error saving new clan: " + e.getMessage(), e);
  926. }
  927. }
  928. /**
  929. * @param member the clan member to be removed.
  930. * @param clanJoinExpiryTime
  931. * @param clanCreateExpiryTime
  932. */
  933. private void removeMemberInDatabase(L2ClanMember member, long clanJoinExpiryTime, long clanCreateExpiryTime)
  934. {
  935. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  936. PreparedStatement ps1 = 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=?");
  937. PreparedStatement ps2 = con.prepareStatement("UPDATE characters SET apprentice=0 WHERE apprentice=?");
  938. PreparedStatement ps3 = con.prepareStatement("UPDATE characters SET sponsor=0 WHERE sponsor=?"))
  939. {
  940. ps1.setString(1, "");
  941. ps1.setLong(2, clanJoinExpiryTime);
  942. ps1.setLong(3, clanCreateExpiryTime);
  943. ps1.setInt(4, member.getObjectId());
  944. ps1.execute();
  945. if (Config.DEBUG)
  946. {
  947. _log.fine("clan member removed in db: " + getId());
  948. }
  949. // Remove apprentice.
  950. ps2.setInt(1, member.getObjectId());
  951. ps2.execute();
  952. // Remove sponsor.
  953. ps3.setInt(1, member.getObjectId());
  954. ps3.execute();
  955. }
  956. catch (Exception e)
  957. {
  958. _log.log(Level.SEVERE, "Error removing clan member: " + e.getMessage(), e);
  959. }
  960. }
  961. private void restore()
  962. {
  963. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  964. PreparedStatement ps = con.prepareStatement(SELECT_CLAN_DATA))
  965. {
  966. ps.setInt(1, getId());
  967. try (ResultSet clanData = ps.executeQuery())
  968. {
  969. if (clanData.next())
  970. {
  971. setName(clanData.getString("clan_name"));
  972. setLevel(clanData.getInt("clan_level"));
  973. setCastleId(clanData.getInt("hasCastle"));
  974. _bloodAllianceCount = clanData.getInt("blood_alliance_count");
  975. _bloodOathCount = clanData.getInt("blood_oath_count");
  976. setAllyId(clanData.getInt("ally_id"));
  977. setAllyName(clanData.getString("ally_name"));
  978. setAllyPenaltyExpiryTime(clanData.getLong("ally_penalty_expiry_time"), clanData.getInt("ally_penalty_type"));
  979. if (getAllyPenaltyExpiryTime() < System.currentTimeMillis())
  980. {
  981. setAllyPenaltyExpiryTime(0, 0);
  982. }
  983. setCharPenaltyExpiryTime(clanData.getLong("char_penalty_expiry_time"));
  984. if ((getCharPenaltyExpiryTime() + (Config.ALT_CLAN_JOIN_DAYS * 86400000L)) < System.currentTimeMillis()) // 24*60*60*1000 = 86400000
  985. {
  986. setCharPenaltyExpiryTime(0);
  987. }
  988. setDissolvingExpiryTime(clanData.getLong("dissolving_expiry_time"));
  989. setCrestId(clanData.getInt("crest_id"));
  990. setCrestLargeId(clanData.getInt("crest_large_id"));
  991. setAllyCrestId(clanData.getInt("ally_crest_id"));
  992. setReputationScore(clanData.getInt("reputation_score"), false);
  993. setAuctionBiddedAt(clanData.getInt("auction_bid_at"), false);
  994. setNewLeaderId(clanData.getInt("new_leader_id"), false);
  995. final int leaderId = (clanData.getInt("leader_id"));
  996. ps.clearParameters();
  997. try (PreparedStatement select = con.prepareStatement("SELECT char_name,level,classid,charId,title,power_grade,subpledge,apprentice,sponsor,sex,race FROM characters WHERE clanid=?"))
  998. {
  999. select.setInt(1, getId());
  1000. try (ResultSet clanMember = select.executeQuery())
  1001. {
  1002. L2ClanMember member = null;
  1003. while (clanMember.next())
  1004. {
  1005. member = new L2ClanMember(this, clanMember);
  1006. if (member.getObjectId() == leaderId)
  1007. {
  1008. setLeader(member);
  1009. }
  1010. else
  1011. {
  1012. addClanMember(member);
  1013. }
  1014. }
  1015. }
  1016. }
  1017. }
  1018. }
  1019. if (Config.DEBUG && (getName() != null))
  1020. {
  1021. _log.info("Restored clan data for \"" + getName() + "\" from database.");
  1022. }
  1023. restoreSubPledges();
  1024. restoreRankPrivs();
  1025. restoreSkills();
  1026. restoreNotice();
  1027. }
  1028. catch (Exception e)
  1029. {
  1030. _log.log(Level.SEVERE, "Error restoring clan data: " + e.getMessage(), e);
  1031. }
  1032. }
  1033. private void restoreNotice()
  1034. {
  1035. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1036. PreparedStatement ps = con.prepareStatement("SELECT enabled,notice FROM clan_notices WHERE clan_id=?"))
  1037. {
  1038. ps.setInt(1, getId());
  1039. try (ResultSet noticeData = ps.executeQuery())
  1040. {
  1041. while (noticeData.next())
  1042. {
  1043. _noticeEnabled = noticeData.getBoolean("enabled");
  1044. _notice = noticeData.getString("notice");
  1045. }
  1046. }
  1047. }
  1048. catch (Exception e)
  1049. {
  1050. _log.log(Level.SEVERE, "Error restoring clan notice: " + e.getMessage(), e);
  1051. }
  1052. }
  1053. private void storeNotice(String notice, boolean enabled)
  1054. {
  1055. if (notice == null)
  1056. {
  1057. notice = "";
  1058. }
  1059. if (notice.length() > MAX_NOTICE_LENGTH)
  1060. {
  1061. notice = notice.substring(0, MAX_NOTICE_LENGTH - 1);
  1062. }
  1063. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1064. PreparedStatement ps = con.prepareStatement("INSERT INTO clan_notices (clan_id,notice,enabled) values (?,?,?) ON DUPLICATE KEY UPDATE notice=?,enabled=?"))
  1065. {
  1066. ps.setInt(1, getId());
  1067. ps.setString(2, notice);
  1068. if (enabled)
  1069. {
  1070. ps.setString(3, "true");
  1071. }
  1072. else
  1073. {
  1074. ps.setString(3, "false");
  1075. }
  1076. ps.setString(4, notice);
  1077. if (enabled)
  1078. {
  1079. ps.setString(5, "true");
  1080. }
  1081. else
  1082. {
  1083. ps.setString(5, "false");
  1084. }
  1085. ps.execute();
  1086. }
  1087. catch (Exception e)
  1088. {
  1089. _log.log(Level.WARNING, "Error could not store clan notice: " + e.getMessage(), e);
  1090. }
  1091. _notice = notice;
  1092. _noticeEnabled = enabled;
  1093. }
  1094. public void setNoticeEnabled(boolean enabled)
  1095. {
  1096. storeNotice(_notice, enabled);
  1097. }
  1098. public void setNotice(String notice)
  1099. {
  1100. storeNotice(notice, _noticeEnabled);
  1101. }
  1102. public boolean isNoticeEnabled()
  1103. {
  1104. return _noticeEnabled;
  1105. }
  1106. public String getNotice()
  1107. {
  1108. if (_notice == null)
  1109. {
  1110. return "";
  1111. }
  1112. return _notice;
  1113. }
  1114. private void restoreSkills()
  1115. {
  1116. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1117. PreparedStatement ps = con.prepareStatement("SELECT skill_id,skill_level,sub_pledge_id FROM clan_skills WHERE clan_id=?"))
  1118. {
  1119. // Retrieve all skills of this L2PcInstance from the database
  1120. ps.setInt(1, getId());
  1121. try (ResultSet rset = ps.executeQuery())
  1122. {
  1123. // Go though the recordset of this SQL query
  1124. while (rset.next())
  1125. {
  1126. int id = rset.getInt("skill_id");
  1127. int level = rset.getInt("skill_level");
  1128. // Create a L2Skill object for each record
  1129. Skill skill = SkillData.getInstance().getSkill(id, level);
  1130. // Add the L2Skill object to the L2Clan _skills
  1131. int subType = rset.getInt("sub_pledge_id");
  1132. if (subType == -2)
  1133. {
  1134. _skills.put(skill.getId(), skill);
  1135. }
  1136. else if (subType == 0)
  1137. {
  1138. _subPledgeSkills.put(skill.getId(), skill);
  1139. }
  1140. else
  1141. {
  1142. SubPledge subunit = _subPledges.get(subType);
  1143. if (subunit != null)
  1144. {
  1145. subunit.addNewSkill(skill);
  1146. }
  1147. else
  1148. {
  1149. _log.info("Missing subpledge " + subType + " for clan " + this + ", skill skipped.");
  1150. }
  1151. }
  1152. }
  1153. }
  1154. }
  1155. catch (Exception e)
  1156. {
  1157. _log.log(Level.SEVERE, "Error restoring clan skills: " + e.getMessage(), e);
  1158. }
  1159. }
  1160. /**
  1161. * @return all the clan skills.
  1162. */
  1163. public final Skill[] getAllSkills()
  1164. {
  1165. if (_skills == null)
  1166. {
  1167. return new Skill[0];
  1168. }
  1169. return _skills.values().toArray(new Skill[_skills.values().size()]);
  1170. }
  1171. /**
  1172. * @return the map containing this clan skills.
  1173. */
  1174. public Map<Integer, Skill> getSkills()
  1175. {
  1176. return _skills;
  1177. }
  1178. /**
  1179. * Used to add a skill to skill list of this L2Clan
  1180. * @param newSkill
  1181. * @return
  1182. */
  1183. public Skill addSkill(Skill newSkill)
  1184. {
  1185. Skill oldSkill = null;
  1186. if (newSkill != null)
  1187. {
  1188. // Replace oldSkill by newSkill or Add the newSkill
  1189. oldSkill = _skills.put(newSkill.getId(), newSkill);
  1190. }
  1191. return oldSkill;
  1192. }
  1193. public Skill addNewSkill(Skill newSkill)
  1194. {
  1195. return addNewSkill(newSkill, -2);
  1196. }
  1197. /**
  1198. * 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
  1199. * @param newSkill
  1200. * @param subType
  1201. * @return
  1202. */
  1203. public Skill addNewSkill(Skill newSkill, int subType)
  1204. {
  1205. Skill oldSkill = null;
  1206. if (newSkill != null)
  1207. {
  1208. if (subType == -2)
  1209. {
  1210. oldSkill = _skills.put(newSkill.getId(), newSkill);
  1211. }
  1212. else if (subType == 0)
  1213. {
  1214. oldSkill = _subPledgeSkills.put(newSkill.getId(), newSkill);
  1215. }
  1216. else
  1217. {
  1218. SubPledge subunit = getSubPledge(subType);
  1219. if (subunit != null)
  1220. {
  1221. oldSkill = subunit.addNewSkill(newSkill);
  1222. }
  1223. else
  1224. {
  1225. _log.log(Level.WARNING, "Subpledge " + subType + " does not exist for clan " + this);
  1226. return oldSkill;
  1227. }
  1228. }
  1229. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  1230. {
  1231. if (oldSkill != null)
  1232. {
  1233. try (PreparedStatement ps = con.prepareStatement("UPDATE clan_skills SET skill_level=? WHERE skill_id=? AND clan_id=?"))
  1234. {
  1235. ps.setInt(1, newSkill.getLevel());
  1236. ps.setInt(2, oldSkill.getId());
  1237. ps.setInt(3, getId());
  1238. ps.execute();
  1239. }
  1240. }
  1241. else
  1242. {
  1243. try (PreparedStatement ps = con.prepareStatement("INSERT INTO clan_skills (clan_id,skill_id,skill_level,skill_name,sub_pledge_id) VALUES (?,?,?,?,?)"))
  1244. {
  1245. ps.setInt(1, getId());
  1246. ps.setInt(2, newSkill.getId());
  1247. ps.setInt(3, newSkill.getLevel());
  1248. ps.setString(4, newSkill.getName());
  1249. ps.setInt(5, subType);
  1250. ps.execute();
  1251. }
  1252. }
  1253. }
  1254. catch (Exception e)
  1255. {
  1256. _log.log(Level.WARNING, "Error could not store clan skills: " + e.getMessage(), e);
  1257. }
  1258. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.CLAN_SKILL_S1_ADDED);
  1259. sm.addSkillName(newSkill.getId());
  1260. for (L2ClanMember temp : _members.values())
  1261. {
  1262. if ((temp != null) && (temp.getPlayerInstance() != null) && temp.isOnline())
  1263. {
  1264. if (subType == -2)
  1265. {
  1266. if (newSkill.getMinPledgeClass() <= temp.getPlayerInstance().getPledgeClass())
  1267. {
  1268. temp.getPlayerInstance().addSkill(newSkill, false); // Skill is not saved to player DB
  1269. temp.getPlayerInstance().sendPacket(new PledgeSkillListAdd(newSkill.getId(), newSkill.getLevel()));
  1270. temp.getPlayerInstance().sendPacket(sm);
  1271. temp.getPlayerInstance().sendSkillList();
  1272. }
  1273. }
  1274. else
  1275. {
  1276. if (temp.getPledgeType() == subType)
  1277. {
  1278. temp.getPlayerInstance().addSkill(newSkill, false); // Skill is not saved to player DB
  1279. temp.getPlayerInstance().sendPacket(new ExSubPledgeSkillAdd(subType, newSkill.getId(), newSkill.getLevel()));
  1280. temp.getPlayerInstance().sendPacket(sm);
  1281. temp.getPlayerInstance().sendSkillList();
  1282. }
  1283. }
  1284. }
  1285. }
  1286. }
  1287. return oldSkill;
  1288. }
  1289. public void addSkillEffects()
  1290. {
  1291. for (Skill skill : _skills.values())
  1292. {
  1293. for (L2ClanMember temp : _members.values())
  1294. {
  1295. try
  1296. {
  1297. if ((temp != null) && temp.isOnline())
  1298. {
  1299. if (skill.getMinPledgeClass() <= temp.getPlayerInstance().getPledgeClass())
  1300. {
  1301. temp.getPlayerInstance().addSkill(skill, false); // Skill is not saved to player DB
  1302. }
  1303. }
  1304. }
  1305. catch (NullPointerException e)
  1306. {
  1307. _log.log(Level.WARNING, e.getMessage(), e);
  1308. }
  1309. }
  1310. }
  1311. }
  1312. public void addSkillEffects(L2PcInstance player)
  1313. {
  1314. if (player == null)
  1315. {
  1316. return;
  1317. }
  1318. for (Skill skill : _skills.values())
  1319. {
  1320. if (skill.getMinPledgeClass() <= player.getPledgeClass())
  1321. {
  1322. player.addSkill(skill, false); // Skill is not saved to player DB
  1323. }
  1324. }
  1325. if (player.getPledgeType() == 0)
  1326. {
  1327. for (Skill skill : _subPledgeSkills.values())
  1328. {
  1329. player.addSkill(skill, false); // Skill is not saved to player DB
  1330. }
  1331. }
  1332. else
  1333. {
  1334. SubPledge subunit = getSubPledge(player.getPledgeType());
  1335. if (subunit == null)
  1336. {
  1337. return;
  1338. }
  1339. for (Skill skill : subunit.getSkills())
  1340. {
  1341. player.addSkill(skill, false); // Skill is not saved to player DB
  1342. }
  1343. }
  1344. if (_reputationScore < 0)
  1345. {
  1346. skillsStatus(player, true);
  1347. }
  1348. }
  1349. public void removeSkillEffects(L2PcInstance player)
  1350. {
  1351. if (player == null)
  1352. {
  1353. return;
  1354. }
  1355. for (Skill skill : _skills.values())
  1356. {
  1357. player.removeSkill(skill, false); // Skill is not saved to player DB
  1358. }
  1359. if (player.getPledgeType() == 0)
  1360. {
  1361. for (Skill skill : _subPledgeSkills.values())
  1362. {
  1363. player.removeSkill(skill, false); // Skill is not saved to player DB
  1364. }
  1365. }
  1366. else
  1367. {
  1368. SubPledge subunit = getSubPledge(player.getPledgeType());
  1369. if (subunit == null)
  1370. {
  1371. return;
  1372. }
  1373. for (Skill skill : subunit.getSkills())
  1374. {
  1375. player.removeSkill(skill, false); // Skill is not saved to player DB
  1376. }
  1377. }
  1378. }
  1379. public void skillsStatus(L2PcInstance player, boolean disable)
  1380. {
  1381. if (player == null)
  1382. {
  1383. return;
  1384. }
  1385. for (Skill skill : _skills.values())
  1386. {
  1387. if (disable)
  1388. {
  1389. player.disableSkill(skill, -1);
  1390. }
  1391. else
  1392. {
  1393. player.enableSkill(skill);
  1394. }
  1395. }
  1396. if (player.getPledgeType() == 0)
  1397. {
  1398. for (Skill skill : _subPledgeSkills.values())
  1399. {
  1400. if (disable)
  1401. {
  1402. player.disableSkill(skill, -1);
  1403. }
  1404. else
  1405. {
  1406. player.enableSkill(skill);
  1407. }
  1408. }
  1409. }
  1410. else
  1411. {
  1412. final SubPledge subunit = getSubPledge(player.getPledgeType());
  1413. if (subunit != null)
  1414. {
  1415. for (Skill skill : subunit.getSkills())
  1416. {
  1417. if (disable)
  1418. {
  1419. player.disableSkill(skill, -1);
  1420. }
  1421. else
  1422. {
  1423. player.enableSkill(skill);
  1424. }
  1425. }
  1426. }
  1427. }
  1428. }
  1429. public void broadcastToOnlineAllyMembers(L2GameServerPacket packet)
  1430. {
  1431. for (L2Clan clan : ClanTable.getInstance().getClanAllies(getAllyId()))
  1432. {
  1433. clan.broadcastToOnlineMembers(packet);
  1434. }
  1435. }
  1436. public void broadcastToOnlineMembers(L2GameServerPacket packet)
  1437. {
  1438. for (L2ClanMember member : _members.values())
  1439. {
  1440. if ((member != null) && member.isOnline())
  1441. {
  1442. member.getPlayerInstance().sendPacket(packet);
  1443. }
  1444. }
  1445. }
  1446. public void broadcastCSToOnlineMembers(CreatureSay packet, L2PcInstance broadcaster)
  1447. {
  1448. for (L2ClanMember member : _members.values())
  1449. {
  1450. if ((member != null) && member.isOnline() && !BlockList.isBlocked(member.getPlayerInstance(), broadcaster))
  1451. {
  1452. member.getPlayerInstance().sendPacket(packet);
  1453. }
  1454. }
  1455. }
  1456. public void broadcastToOtherOnlineMembers(L2GameServerPacket packet, L2PcInstance player)
  1457. {
  1458. for (L2ClanMember member : _members.values())
  1459. {
  1460. if ((member != null) && member.isOnline() && (member.getPlayerInstance() != player))
  1461. {
  1462. member.getPlayerInstance().sendPacket(packet);
  1463. }
  1464. }
  1465. }
  1466. @Override
  1467. public String toString()
  1468. {
  1469. return getName() + "[" + getId() + "]";
  1470. }
  1471. public ItemContainer getWarehouse()
  1472. {
  1473. return _warehouse;
  1474. }
  1475. public boolean isAtWarWith(Integer id)
  1476. {
  1477. if (!_atWarWith.isEmpty())
  1478. {
  1479. if (_atWarWith.contains(id))
  1480. {
  1481. return true;
  1482. }
  1483. }
  1484. return false;
  1485. }
  1486. public boolean isAtWarWith(L2Clan clan)
  1487. {
  1488. if (clan == null)
  1489. {
  1490. return false;
  1491. }
  1492. if (!_atWarWith.isEmpty())
  1493. {
  1494. if (_atWarWith.contains(clan.getId()))
  1495. {
  1496. return true;
  1497. }
  1498. }
  1499. return false;
  1500. }
  1501. public boolean isAtWarAttacker(Integer id)
  1502. {
  1503. if ((_atWarAttackers != null) && !_atWarAttackers.isEmpty())
  1504. {
  1505. if (_atWarAttackers.contains(id))
  1506. {
  1507. return true;
  1508. }
  1509. }
  1510. return false;
  1511. }
  1512. public void setEnemyClan(L2Clan clan)
  1513. {
  1514. Integer id = clan.getId();
  1515. _atWarWith.add(id);
  1516. }
  1517. public void setEnemyClan(Integer clan)
  1518. {
  1519. _atWarWith.add(clan);
  1520. }
  1521. public void setAttackerClan(L2Clan clan)
  1522. {
  1523. Integer id = clan.getId();
  1524. _atWarAttackers.add(id);
  1525. }
  1526. public void setAttackerClan(Integer clan)
  1527. {
  1528. _atWarAttackers.add(clan);
  1529. }
  1530. public void deleteEnemyClan(L2Clan clan)
  1531. {
  1532. Integer id = clan.getId();
  1533. _atWarWith.remove(id);
  1534. }
  1535. public void deleteAttackerClan(L2Clan clan)
  1536. {
  1537. Integer id = clan.getId();
  1538. _atWarAttackers.remove(id);
  1539. }
  1540. public int getHiredGuards()
  1541. {
  1542. return _hiredGuards;
  1543. }
  1544. public void incrementHiredGuards()
  1545. {
  1546. _hiredGuards++;
  1547. }
  1548. public boolean isAtWar()
  1549. {
  1550. if ((_atWarWith != null) && !_atWarWith.isEmpty())
  1551. {
  1552. return true;
  1553. }
  1554. return false;
  1555. }
  1556. public List<Integer> getWarList()
  1557. {
  1558. return _atWarWith;
  1559. }
  1560. public List<Integer> getAttackerList()
  1561. {
  1562. return _atWarAttackers;
  1563. }
  1564. public void broadcastClanStatus()
  1565. {
  1566. for (L2PcInstance member : getOnlineMembers(0))
  1567. {
  1568. member.sendPacket(PledgeShowMemberListDeleteAll.STATIC_PACKET);
  1569. member.sendPacket(new PledgeShowMemberListAll(this, member));
  1570. }
  1571. }
  1572. public static class SubPledge
  1573. {
  1574. private final int _id;
  1575. private String _subPledgeName;
  1576. private int _leaderId;
  1577. private final Map<Integer, Skill> _subPledgeSkills = new FastMap<>();
  1578. public SubPledge(int id, String name, int leaderId)
  1579. {
  1580. _id = id;
  1581. _subPledgeName = name;
  1582. _leaderId = leaderId;
  1583. }
  1584. public int getId()
  1585. {
  1586. return _id;
  1587. }
  1588. public String getName()
  1589. {
  1590. return _subPledgeName;
  1591. }
  1592. public void setName(String name)
  1593. {
  1594. _subPledgeName = name;
  1595. }
  1596. public int getLeaderId()
  1597. {
  1598. return _leaderId;
  1599. }
  1600. public void setLeaderId(int leaderId)
  1601. {
  1602. _leaderId = leaderId;
  1603. }
  1604. public Skill addNewSkill(Skill skill)
  1605. {
  1606. return _subPledgeSkills.put(skill.getId(), skill);
  1607. }
  1608. public Collection<Skill> getSkills()
  1609. {
  1610. return _subPledgeSkills.values();
  1611. }
  1612. public Skill getSkill(int id)
  1613. {
  1614. return _subPledgeSkills.get(id);
  1615. }
  1616. }
  1617. public static class RankPrivs
  1618. {
  1619. private final int _rankId;
  1620. private final int _party;// TODO find out what this stuff means and implement it
  1621. private final EnumIntBitmask<ClanPrivilege> _rankPrivs;
  1622. public RankPrivs(int rank, int party, int privs)
  1623. {
  1624. _rankId = rank;
  1625. _party = party;
  1626. _rankPrivs = new EnumIntBitmask<>(ClanPrivilege.class, privs);
  1627. }
  1628. public RankPrivs(int rank, int party, EnumIntBitmask<ClanPrivilege> rankPrivs)
  1629. {
  1630. _rankId = rank;
  1631. _party = party;
  1632. _rankPrivs = rankPrivs;
  1633. }
  1634. public int getRank()
  1635. {
  1636. return _rankId;
  1637. }
  1638. public int getParty()
  1639. {
  1640. return _party;
  1641. }
  1642. public EnumIntBitmask<ClanPrivilege> getPrivs()
  1643. {
  1644. return _rankPrivs;
  1645. }
  1646. public void setPrivs(int privs)
  1647. {
  1648. _rankPrivs.setBitmask(privs);
  1649. }
  1650. }
  1651. private void restoreSubPledges()
  1652. {
  1653. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1654. PreparedStatement ps = con.prepareStatement("SELECT sub_pledge_id,name,leader_id FROM clan_subpledges WHERE clan_id=?"))
  1655. {
  1656. // Retrieve all subpledges of this clan from the database
  1657. ps.setInt(1, getId());
  1658. try (ResultSet rset = ps.executeQuery())
  1659. {
  1660. while (rset.next())
  1661. {
  1662. int id = rset.getInt("sub_pledge_id");
  1663. String name = rset.getString("name");
  1664. int leaderId = rset.getInt("leader_id");
  1665. // Create a SubPledge object for each record
  1666. SubPledge pledge = new SubPledge(id, name, leaderId);
  1667. _subPledges.put(id, pledge);
  1668. }
  1669. }
  1670. }
  1671. catch (Exception e)
  1672. {
  1673. _log.log(Level.WARNING, "Could not restore clan sub-units: " + e.getMessage(), e);
  1674. }
  1675. }
  1676. /**
  1677. * used to retrieve subPledge by type
  1678. * @param pledgeType
  1679. * @return
  1680. */
  1681. public final SubPledge getSubPledge(int pledgeType)
  1682. {
  1683. if (_subPledges == null)
  1684. {
  1685. return null;
  1686. }
  1687. return _subPledges.get(pledgeType);
  1688. }
  1689. /**
  1690. * Used to retrieve subPledge by type
  1691. * @param pledgeName
  1692. * @return
  1693. */
  1694. public final SubPledge getSubPledge(String pledgeName)
  1695. {
  1696. if (_subPledges == null)
  1697. {
  1698. return null;
  1699. }
  1700. for (SubPledge sp : _subPledges.values())
  1701. {
  1702. if (sp.getName().equalsIgnoreCase(pledgeName))
  1703. {
  1704. return sp;
  1705. }
  1706. }
  1707. return null;
  1708. }
  1709. /**
  1710. * Used to retrieve all subPledges
  1711. * @return
  1712. */
  1713. public final SubPledge[] getAllSubPledges()
  1714. {
  1715. if (_subPledges == null)
  1716. {
  1717. return new SubPledge[0];
  1718. }
  1719. return _subPledges.values().toArray(new SubPledge[_subPledges.values().size()]);
  1720. }
  1721. public SubPledge createSubPledge(L2PcInstance player, int pledgeType, int leaderId, String subPledgeName)
  1722. {
  1723. SubPledge subPledge = null;
  1724. pledgeType = getAvailablePledgeTypes(pledgeType);
  1725. if (pledgeType == 0)
  1726. {
  1727. if (pledgeType == L2Clan.SUBUNIT_ACADEMY)
  1728. {
  1729. player.sendPacket(SystemMessageId.CLAN_HAS_ALREADY_ESTABLISHED_A_CLAN_ACADEMY);
  1730. }
  1731. else
  1732. {
  1733. player.sendMessage("You can't create any more sub-units of this type");
  1734. }
  1735. return null;
  1736. }
  1737. if (_leader.getObjectId() == leaderId)
  1738. {
  1739. player.sendMessage("Leader is not correct");
  1740. return null;
  1741. }
  1742. // Royal Guard 5000 points per each
  1743. // Order of Knights 10000 points per each
  1744. if ((pledgeType != -1) && (((getReputationScore() < Config.ROYAL_GUARD_COST) && (pledgeType < L2Clan.SUBUNIT_KNIGHT1)) || ((getReputationScore() < Config.KNIGHT_UNIT_COST) && (pledgeType > L2Clan.SUBUNIT_ROYAL2))))
  1745. {
  1746. player.sendPacket(SystemMessageId.THE_CLAN_REPUTATION_SCORE_IS_TOO_LOW);
  1747. return null;
  1748. }
  1749. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1750. PreparedStatement ps = con.prepareStatement("INSERT INTO clan_subpledges (clan_id,sub_pledge_id,name,leader_id) values (?,?,?,?)"))
  1751. {
  1752. ps.setInt(1, getId());
  1753. ps.setInt(2, pledgeType);
  1754. ps.setString(3, subPledgeName);
  1755. ps.setInt(4, pledgeType != -1 ? leaderId : 0);
  1756. ps.execute();
  1757. subPledge = new SubPledge(pledgeType, subPledgeName, leaderId);
  1758. _subPledges.put(pledgeType, subPledge);
  1759. if (pledgeType != -1)
  1760. {
  1761. // Royal Guard 5000 points per each
  1762. // Order of Knights 10000 points per each
  1763. if (pledgeType < L2Clan.SUBUNIT_KNIGHT1)
  1764. {
  1765. setReputationScore(getReputationScore() - Config.ROYAL_GUARD_COST, true);
  1766. }
  1767. else
  1768. {
  1769. setReputationScore(getReputationScore() - Config.KNIGHT_UNIT_COST, true);
  1770. // TODO: clan lvl9 or more can reinforce knights cheaper if first knight unit already created, use Config.KNIGHT_REINFORCE_COST
  1771. }
  1772. }
  1773. if (Config.DEBUG)
  1774. {
  1775. _log.fine("New sub_clan saved in db: " + getId() + "; " + pledgeType);
  1776. }
  1777. }
  1778. catch (Exception e)
  1779. {
  1780. _log.log(Level.SEVERE, "Error saving sub clan data: " + e.getMessage(), e);
  1781. }
  1782. broadcastToOnlineMembers(new PledgeShowInfoUpdate(_leader.getClan()));
  1783. broadcastToOnlineMembers(new PledgeReceiveSubPledgeCreated(subPledge, _leader.getClan()));
  1784. return subPledge;
  1785. }
  1786. public int getAvailablePledgeTypes(int pledgeType)
  1787. {
  1788. if (_subPledges.get(pledgeType) != null)
  1789. {
  1790. // _log.warning("found sub-unit with id: "+pledgeType);
  1791. switch (pledgeType)
  1792. {
  1793. case SUBUNIT_ACADEMY:
  1794. return 0;
  1795. case SUBUNIT_ROYAL1:
  1796. pledgeType = getAvailablePledgeTypes(SUBUNIT_ROYAL2);
  1797. break;
  1798. case SUBUNIT_ROYAL2:
  1799. return 0;
  1800. case SUBUNIT_KNIGHT1:
  1801. pledgeType = getAvailablePledgeTypes(SUBUNIT_KNIGHT2);
  1802. break;
  1803. case SUBUNIT_KNIGHT2:
  1804. pledgeType = getAvailablePledgeTypes(SUBUNIT_KNIGHT3);
  1805. break;
  1806. case SUBUNIT_KNIGHT3:
  1807. pledgeType = getAvailablePledgeTypes(SUBUNIT_KNIGHT4);
  1808. break;
  1809. case SUBUNIT_KNIGHT4:
  1810. return 0;
  1811. }
  1812. }
  1813. return pledgeType;
  1814. }
  1815. public void updateSubPledgeInDB(int pledgeType)
  1816. {
  1817. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1818. PreparedStatement ps = con.prepareStatement("UPDATE clan_subpledges SET leader_id=?, name=? WHERE clan_id=? AND sub_pledge_id=?"))
  1819. {
  1820. ps.setInt(1, getSubPledge(pledgeType).getLeaderId());
  1821. ps.setString(2, getSubPledge(pledgeType).getName());
  1822. ps.setInt(3, getId());
  1823. ps.setInt(4, pledgeType);
  1824. ps.execute();
  1825. if (Config.DEBUG)
  1826. {
  1827. _log.fine("Subpledge updated in db: " + getId());
  1828. }
  1829. }
  1830. catch (Exception e)
  1831. {
  1832. _log.log(Level.SEVERE, "Error updating subpledge: " + e.getMessage(), e);
  1833. }
  1834. }
  1835. private void restoreRankPrivs()
  1836. {
  1837. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1838. PreparedStatement ps = con.prepareStatement("SELECT privs,rank,party FROM clan_privs WHERE clan_id=?"))
  1839. {
  1840. // Retrieve all skills of this L2PcInstance from the database
  1841. ps.setInt(1, getId());
  1842. // _log.warning("clanPrivs restore for ClanId : "+getClanId());
  1843. try (ResultSet rset = ps.executeQuery())
  1844. {
  1845. // Go though the recordset of this SQL query
  1846. while (rset.next())
  1847. {
  1848. int rank = rset.getInt("rank");
  1849. // int party = rset.getInt("party");
  1850. int privileges = rset.getInt("privs");
  1851. // Create a SubPledge object for each record
  1852. if (rank == -1)
  1853. {
  1854. continue;
  1855. }
  1856. _privs.get(rank).setPrivs(privileges);
  1857. }
  1858. }
  1859. }
  1860. catch (Exception e)
  1861. {
  1862. _log.log(Level.SEVERE, "Error restoring clan privs by rank: " + e.getMessage(), e);
  1863. }
  1864. }
  1865. public void initializePrivs()
  1866. {
  1867. RankPrivs privs;
  1868. for (int i = 1; i < 10; i++)
  1869. {
  1870. privs = new RankPrivs(i, 0, new EnumIntBitmask<>(ClanPrivilege.class, false));
  1871. _privs.put(i, privs);
  1872. }
  1873. }
  1874. public EnumIntBitmask<ClanPrivilege> getRankPrivs(int rank)
  1875. {
  1876. if (_privs.get(rank) != null)
  1877. {
  1878. return _privs.get(rank).getPrivs();
  1879. }
  1880. return new EnumIntBitmask<>(ClanPrivilege.class, false);
  1881. }
  1882. public void setRankPrivs(int rank, int privs)
  1883. {
  1884. if (_privs.get(rank) != null)
  1885. {
  1886. _privs.get(rank).setPrivs(privs);
  1887. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1888. PreparedStatement ps = con.prepareStatement("INSERT INTO clan_privs (clan_id,rank,party,privs) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE privs = ?"))
  1889. {
  1890. // Retrieve all skills of this L2PcInstance from the database
  1891. ps.setInt(1, getId());
  1892. ps.setInt(2, rank);
  1893. ps.setInt(3, 0);
  1894. ps.setInt(4, privs);
  1895. ps.setInt(5, privs);
  1896. ps.execute();
  1897. }
  1898. catch (Exception e)
  1899. {
  1900. _log.log(Level.WARNING, "Could not store clan privs for rank: " + e.getMessage(), e);
  1901. }
  1902. for (L2ClanMember cm : getMembers())
  1903. {
  1904. if (cm.isOnline())
  1905. {
  1906. if (cm.getPowerGrade() == rank)
  1907. {
  1908. if (cm.getPlayerInstance() != null)
  1909. {
  1910. cm.getPlayerInstance().getClanPrivileges().setBitmask(privs);
  1911. cm.getPlayerInstance().sendPacket(new UserInfo(cm.getPlayerInstance()));
  1912. cm.getPlayerInstance().sendPacket(new ExBrExtraUserInfo(cm.getPlayerInstance()));
  1913. }
  1914. }
  1915. }
  1916. }
  1917. broadcastClanStatus();
  1918. }
  1919. else
  1920. {
  1921. _privs.put(rank, new RankPrivs(rank, 0, privs));
  1922. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1923. PreparedStatement ps = con.prepareStatement("INSERT INTO clan_privs (clan_id,rank,party,privs) VALUES (?,?,?,?)"))
  1924. {
  1925. // Retrieve all skills of this L2PcInstance from the database
  1926. ps.setInt(1, getId());
  1927. ps.setInt(2, rank);
  1928. ps.setInt(3, 0);
  1929. ps.setInt(4, privs);
  1930. ps.execute();
  1931. }
  1932. catch (Exception e)
  1933. {
  1934. _log.log(Level.WARNING, "Could not create new rank and store clan privs for rank: " + e.getMessage(), e);
  1935. }
  1936. }
  1937. }
  1938. /**
  1939. * @return all RankPrivs.
  1940. */
  1941. public final RankPrivs[] getAllRankPrivs()
  1942. {
  1943. if (_privs == null)
  1944. {
  1945. return new RankPrivs[0];
  1946. }
  1947. return _privs.values().toArray(new RankPrivs[_privs.values().size()]);
  1948. }
  1949. public int getLeaderSubPledge(int leaderId)
  1950. {
  1951. int id = 0;
  1952. for (SubPledge sp : _subPledges.values())
  1953. {
  1954. if (sp.getLeaderId() == 0)
  1955. {
  1956. continue;
  1957. }
  1958. if (sp.getLeaderId() == leaderId)
  1959. {
  1960. id = sp.getId();
  1961. }
  1962. }
  1963. return id;
  1964. }
  1965. public synchronized void addReputationScore(int value, boolean save)
  1966. {
  1967. setReputationScore(getReputationScore() + value, save);
  1968. }
  1969. public synchronized void takeReputationScore(int value, boolean save)
  1970. {
  1971. setReputationScore(getReputationScore() - value, save);
  1972. }
  1973. private void setReputationScore(int value, boolean save)
  1974. {
  1975. if ((_reputationScore >= 0) && (value < 0))
  1976. {
  1977. broadcastToOnlineMembers(SystemMessage.getSystemMessage(SystemMessageId.REPUTATION_POINTS_0_OR_LOWER_CLAN_SKILLS_DEACTIVATED));
  1978. for (L2ClanMember member : _members.values())
  1979. {
  1980. if (member.isOnline() && (member.getPlayerInstance() != null))
  1981. {
  1982. skillsStatus(member.getPlayerInstance(), true);
  1983. }
  1984. }
  1985. }
  1986. else if ((_reputationScore < 0) && (value >= 0))
  1987. {
  1988. broadcastToOnlineMembers(SystemMessage.getSystemMessage(SystemMessageId.CLAN_SKILLS_WILL_BE_ACTIVATED_SINCE_REPUTATION_IS_0_OR_HIGHER));
  1989. for (L2ClanMember member : _members.values())
  1990. {
  1991. if (member.isOnline() && (member.getPlayerInstance() != null))
  1992. {
  1993. skillsStatus(member.getPlayerInstance(), false);
  1994. }
  1995. }
  1996. }
  1997. _reputationScore = value;
  1998. if (_reputationScore > 100000000)
  1999. {
  2000. _reputationScore = 100000000;
  2001. }
  2002. if (_reputationScore < -100000000)
  2003. {
  2004. _reputationScore = -100000000;
  2005. }
  2006. broadcastToOnlineMembers(new PledgeShowInfoUpdate(this));
  2007. if (save)
  2008. {
  2009. updateClanScoreInDB();
  2010. }
  2011. }
  2012. public int getReputationScore()
  2013. {
  2014. return _reputationScore;
  2015. }
  2016. public void setRank(int rank)
  2017. {
  2018. _rank = rank;
  2019. }
  2020. public int getRank()
  2021. {
  2022. return _rank;
  2023. }
  2024. public int getAuctionBiddedAt()
  2025. {
  2026. return _auctionBiddedAt;
  2027. }
  2028. public void setAuctionBiddedAt(int id, boolean storeInDb)
  2029. {
  2030. _auctionBiddedAt = id;
  2031. if (storeInDb)
  2032. {
  2033. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  2034. PreparedStatement ps = con.prepareStatement("UPDATE clan_data SET auction_bid_at=? WHERE clan_id=?"))
  2035. {
  2036. ps.setInt(1, id);
  2037. ps.setInt(2, getId());
  2038. ps.execute();
  2039. }
  2040. catch (Exception e)
  2041. {
  2042. _log.log(Level.WARNING, "Could not store auction for clan: " + e.getMessage(), e);
  2043. }
  2044. }
  2045. }
  2046. /**
  2047. * @param activeChar the clan inviting player.
  2048. * @param target the invited player.
  2049. * @param pledgeType the pledge type to join.
  2050. * @return {core true} if activeChar and target meet various conditions to join a clan.
  2051. */
  2052. public boolean checkClanJoinCondition(L2PcInstance activeChar, L2PcInstance target, int pledgeType)
  2053. {
  2054. if (activeChar == null)
  2055. {
  2056. return false;
  2057. }
  2058. if (!activeChar.hasClanPrivilege(ClanPrivilege.CL_JOIN_CLAN))
  2059. {
  2060. activeChar.sendPacket(SystemMessageId.YOU_ARE_NOT_AUTHORIZED_TO_DO_THAT);
  2061. return false;
  2062. }
  2063. if (target == null)
  2064. {
  2065. activeChar.sendPacket(SystemMessageId.YOU_HAVE_INVITED_THE_WRONG_TARGET);
  2066. return false;
  2067. }
  2068. if (activeChar.getObjectId() == target.getObjectId())
  2069. {
  2070. activeChar.sendPacket(SystemMessageId.CANNOT_INVITE_YOURSELF);
  2071. return false;
  2072. }
  2073. if (getCharPenaltyExpiryTime() > System.currentTimeMillis())
  2074. {
  2075. activeChar.sendPacket(SystemMessageId.YOU_MUST_WAIT_BEFORE_ACCEPTING_A_NEW_MEMBER);
  2076. return false;
  2077. }
  2078. if (target.getClanId() != 0)
  2079. {
  2080. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_WORKING_WITH_ANOTHER_CLAN);
  2081. sm.addString(target.getName());
  2082. activeChar.sendPacket(sm);
  2083. return false;
  2084. }
  2085. if (target.getClanJoinExpiryTime() > System.currentTimeMillis())
  2086. {
  2087. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_MUST_WAIT_BEFORE_JOINING_ANOTHER_CLAN);
  2088. sm.addString(target.getName());
  2089. activeChar.sendPacket(sm);
  2090. return false;
  2091. }
  2092. if (((target.getLevel() > 40) || (target.getClassId().level() >= 2)) && (pledgeType == -1))
  2093. {
  2094. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_DOESNOT_MEET_REQUIREMENTS_TO_JOIN_ACADEMY);
  2095. sm.addString(target.getName());
  2096. activeChar.sendPacket(sm);
  2097. activeChar.sendPacket(SystemMessageId.ACADEMY_REQUIREMENTS);
  2098. return false;
  2099. }
  2100. if (getSubPledgeMembersCount(pledgeType) >= getMaxNrOfMembers(pledgeType))
  2101. {
  2102. if (pledgeType == 0)
  2103. {
  2104. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_CLAN_IS_FULL);
  2105. sm.addString(getName());
  2106. activeChar.sendPacket(sm);
  2107. }
  2108. else
  2109. {
  2110. activeChar.sendPacket(SystemMessageId.SUBCLAN_IS_FULL);
  2111. }
  2112. return false;
  2113. }
  2114. return true;
  2115. }
  2116. /**
  2117. * @param activeChar the clan inviting player.
  2118. * @param target the invited player.
  2119. * @return {core true} if activeChar and target meet various conditions to join a clan.
  2120. */
  2121. public boolean checkAllyJoinCondition(L2PcInstance activeChar, L2PcInstance target)
  2122. {
  2123. if (activeChar == null)
  2124. {
  2125. return false;
  2126. }
  2127. if ((activeChar.getAllyId() == 0) || !activeChar.isClanLeader() || (activeChar.getClanId() != activeChar.getAllyId()))
  2128. {
  2129. activeChar.sendPacket(SystemMessageId.FEATURE_ONLY_FOR_ALLIANCE_LEADER);
  2130. return false;
  2131. }
  2132. L2Clan leaderClan = activeChar.getClan();
  2133. if (leaderClan.getAllyPenaltyExpiryTime() > System.currentTimeMillis())
  2134. {
  2135. if (leaderClan.getAllyPenaltyType() == PENALTY_TYPE_DISMISS_CLAN)
  2136. {
  2137. activeChar.sendPacket(SystemMessageId.CANT_INVITE_CLAN_WITHIN_1_DAY);
  2138. return false;
  2139. }
  2140. }
  2141. if (target == null)
  2142. {
  2143. activeChar.sendPacket(SystemMessageId.YOU_HAVE_INVITED_THE_WRONG_TARGET);
  2144. return false;
  2145. }
  2146. if (activeChar.getObjectId() == target.getObjectId())
  2147. {
  2148. activeChar.sendPacket(SystemMessageId.CANNOT_INVITE_YOURSELF);
  2149. return false;
  2150. }
  2151. if (target.getClan() == null)
  2152. {
  2153. activeChar.sendPacket(SystemMessageId.TARGET_MUST_BE_IN_CLAN);
  2154. return false;
  2155. }
  2156. if (!target.isClanLeader())
  2157. {
  2158. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_IS_NOT_A_CLAN_LEADER);
  2159. sm.addString(target.getName());
  2160. activeChar.sendPacket(sm);
  2161. sm = null;
  2162. return false;
  2163. }
  2164. L2Clan targetClan = target.getClan();
  2165. if (target.getAllyId() != 0)
  2166. {
  2167. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_CLAN_ALREADY_MEMBER_OF_S2_ALLIANCE);
  2168. sm.addString(targetClan.getName());
  2169. sm.addString(targetClan.getAllyName());
  2170. activeChar.sendPacket(sm);
  2171. sm = null;
  2172. return false;
  2173. }
  2174. if (targetClan.getAllyPenaltyExpiryTime() > System.currentTimeMillis())
  2175. {
  2176. if (targetClan.getAllyPenaltyType() == PENALTY_TYPE_CLAN_LEAVED)
  2177. {
  2178. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_CANT_ENTER_ALLIANCE_WITHIN_1_DAY);
  2179. sm.addString(target.getClan().getName());
  2180. sm.addString(target.getClan().getAllyName());
  2181. activeChar.sendPacket(sm);
  2182. sm = null;
  2183. return false;
  2184. }
  2185. if (targetClan.getAllyPenaltyType() == PENALTY_TYPE_CLAN_DISMISSED)
  2186. {
  2187. activeChar.sendPacket(SystemMessageId.CANT_ENTER_ALLIANCE_WITHIN_1_DAY);
  2188. return false;
  2189. }
  2190. }
  2191. if (activeChar.isInsideZone(ZoneId.SIEGE) && target.isInsideZone(ZoneId.SIEGE))
  2192. {
  2193. activeChar.sendPacket(SystemMessageId.OPPOSING_CLAN_IS_PARTICIPATING_IN_SIEGE);
  2194. return false;
  2195. }
  2196. if (leaderClan.isAtWarWith(targetClan.getId()))
  2197. {
  2198. activeChar.sendPacket(SystemMessageId.MAY_NOT_ALLY_CLAN_BATTLE);
  2199. return false;
  2200. }
  2201. if (ClanTable.getInstance().getClanAllies(activeChar.getAllyId()).size() >= Config.ALT_MAX_NUM_OF_CLANS_IN_ALLY)
  2202. {
  2203. activeChar.sendPacket(SystemMessageId.YOU_HAVE_EXCEEDED_THE_LIMIT);
  2204. return false;
  2205. }
  2206. return true;
  2207. }
  2208. public long getAllyPenaltyExpiryTime()
  2209. {
  2210. return _allyPenaltyExpiryTime;
  2211. }
  2212. public int getAllyPenaltyType()
  2213. {
  2214. return _allyPenaltyType;
  2215. }
  2216. public void setAllyPenaltyExpiryTime(long expiryTime, int penaltyType)
  2217. {
  2218. _allyPenaltyExpiryTime = expiryTime;
  2219. _allyPenaltyType = penaltyType;
  2220. }
  2221. public long getCharPenaltyExpiryTime()
  2222. {
  2223. return _charPenaltyExpiryTime;
  2224. }
  2225. public void setCharPenaltyExpiryTime(long time)
  2226. {
  2227. _charPenaltyExpiryTime = time;
  2228. }
  2229. public long getDissolvingExpiryTime()
  2230. {
  2231. return _dissolvingExpiryTime;
  2232. }
  2233. public void setDissolvingExpiryTime(long time)
  2234. {
  2235. _dissolvingExpiryTime = time;
  2236. }
  2237. public void createAlly(L2PcInstance player, String allyName)
  2238. {
  2239. if (null == player)
  2240. {
  2241. return;
  2242. }
  2243. if (Config.DEBUG)
  2244. {
  2245. _log.fine(player.getObjectId() + "(" + player.getName() + ") requested ally creation from ");
  2246. }
  2247. if (!player.isClanLeader())
  2248. {
  2249. player.sendPacket(SystemMessageId.ONLY_CLAN_LEADER_CREATE_ALLIANCE);
  2250. return;
  2251. }
  2252. if (getAllyId() != 0)
  2253. {
  2254. player.sendPacket(SystemMessageId.ALREADY_JOINED_ALLIANCE);
  2255. return;
  2256. }
  2257. if (getLevel() < 5)
  2258. {
  2259. player.sendPacket(SystemMessageId.TO_CREATE_AN_ALLY_YOU_CLAN_MUST_BE_LEVEL_5_OR_HIGHER);
  2260. return;
  2261. }
  2262. if (getAllyPenaltyExpiryTime() > System.currentTimeMillis())
  2263. {
  2264. if (getAllyPenaltyType() == L2Clan.PENALTY_TYPE_DISSOLVE_ALLY)
  2265. {
  2266. player.sendPacket(SystemMessageId.CANT_CREATE_ALLIANCE_10_DAYS_DISOLUTION);
  2267. return;
  2268. }
  2269. }
  2270. if (getDissolvingExpiryTime() > System.currentTimeMillis())
  2271. {
  2272. player.sendPacket(SystemMessageId.YOU_MAY_NOT_CREATE_ALLY_WHILE_DISSOLVING);
  2273. return;
  2274. }
  2275. if (!Util.isAlphaNumeric(allyName))
  2276. {
  2277. player.sendPacket(SystemMessageId.INCORRECT_ALLIANCE_NAME);
  2278. return;
  2279. }
  2280. if ((allyName.length() > 16) || (allyName.length() < 2))
  2281. {
  2282. player.sendPacket(SystemMessageId.INCORRECT_ALLIANCE_NAME_LENGTH);
  2283. return;
  2284. }
  2285. if (ClanTable.getInstance().isAllyExists(allyName))
  2286. {
  2287. player.sendPacket(SystemMessageId.ALLIANCE_ALREADY_EXISTS);
  2288. return;
  2289. }
  2290. setAllyId(getId());
  2291. setAllyName(allyName.trim());
  2292. setAllyPenaltyExpiryTime(0, 0);
  2293. updateClanInDB();
  2294. player.sendPacket(new UserInfo(player));
  2295. player.sendPacket(new ExBrExtraUserInfo(player));
  2296. // TODO: Need correct message id
  2297. player.sendMessage("Alliance " + allyName + " has been created.");
  2298. // notify CB server about the change
  2299. CommunityServerThread.getInstance().sendPacket(new WorldInfo(null, this, WorldInfo.TYPE_UPDATE_CLAN_DATA));
  2300. }
  2301. public void dissolveAlly(L2PcInstance player)
  2302. {
  2303. if (getAllyId() == 0)
  2304. {
  2305. player.sendPacket(SystemMessageId.NO_CURRENT_ALLIANCES);
  2306. return;
  2307. }
  2308. if (!player.isClanLeader() || (getId() != getAllyId()))
  2309. {
  2310. player.sendPacket(SystemMessageId.FEATURE_ONLY_FOR_ALLIANCE_LEADER);
  2311. return;
  2312. }
  2313. if (player.isInsideZone(ZoneId.SIEGE))
  2314. {
  2315. player.sendPacket(SystemMessageId.CANNOT_DISSOLVE_ALLY_WHILE_IN_SIEGE);
  2316. return;
  2317. }
  2318. broadcastToOnlineAllyMembers(SystemMessage.getSystemMessage(SystemMessageId.ALLIANCE_DISOLVED));
  2319. long currentTime = System.currentTimeMillis();
  2320. for (L2Clan clan : ClanTable.getInstance().getClanAllies(getAllyId()))
  2321. {
  2322. if (clan.getId() != getId())
  2323. {
  2324. clan.setAllyId(0);
  2325. clan.setAllyName(null);
  2326. clan.setAllyPenaltyExpiryTime(0, 0);
  2327. clan.updateClanInDB();
  2328. // notify CB server about the change
  2329. CommunityServerThread.getInstance().sendPacket(new WorldInfo(null, clan, WorldInfo.TYPE_UPDATE_CLAN_DATA));
  2330. }
  2331. }
  2332. setAllyId(0);
  2333. setAllyName(null);
  2334. changeAllyCrest(0, false);
  2335. setAllyPenaltyExpiryTime(currentTime + (Config.ALT_CREATE_ALLY_DAYS_WHEN_DISSOLVED * 86400000L), L2Clan.PENALTY_TYPE_DISSOLVE_ALLY); // 24*60*60*1000 = 86400000
  2336. updateClanInDB();
  2337. // The clan leader should take the XP penalty of a full death.
  2338. player.deathPenalty(false, false, false);
  2339. // notify CB server about the change
  2340. CommunityServerThread.getInstance().sendPacket(new WorldInfo(null, this, WorldInfo.TYPE_UPDATE_CLAN_DATA));
  2341. }
  2342. public boolean levelUpClan(L2PcInstance player)
  2343. {
  2344. if (!player.isClanLeader())
  2345. {
  2346. player.sendPacket(SystemMessageId.YOU_ARE_NOT_AUTHORIZED_TO_DO_THAT);
  2347. return false;
  2348. }
  2349. if (System.currentTimeMillis() < getDissolvingExpiryTime())
  2350. {
  2351. player.sendPacket(SystemMessageId.CANNOT_RISE_LEVEL_WHILE_DISSOLUTION_IN_PROGRESS);
  2352. return false;
  2353. }
  2354. boolean increaseClanLevel = false;
  2355. switch (getLevel())
  2356. {
  2357. case 0:
  2358. {
  2359. // Upgrade to 1
  2360. if ((player.getSp() >= 20000) && (player.getAdena() >= 650000))
  2361. {
  2362. if (player.reduceAdena("ClanLvl", 650000, player.getTarget(), true))
  2363. {
  2364. player.setSp(player.getSp() - 20000);
  2365. SystemMessage sp = SystemMessage.getSystemMessage(SystemMessageId.SP_DECREASED_S1);
  2366. sp.addInt(20000);
  2367. player.sendPacket(sp);
  2368. sp = null;
  2369. increaseClanLevel = true;
  2370. }
  2371. }
  2372. break;
  2373. }
  2374. case 1:
  2375. {
  2376. // Upgrade to 2
  2377. if ((player.getSp() >= 100000) && (player.getAdena() >= 2500000))
  2378. {
  2379. if (player.reduceAdena("ClanLvl", 2500000, player.getTarget(), true))
  2380. {
  2381. player.setSp(player.getSp() - 100000);
  2382. SystemMessage sp = SystemMessage.getSystemMessage(SystemMessageId.SP_DECREASED_S1);
  2383. sp.addInt(100000);
  2384. player.sendPacket(sp);
  2385. sp = null;
  2386. increaseClanLevel = true;
  2387. }
  2388. }
  2389. break;
  2390. }
  2391. case 2:
  2392. {
  2393. // Upgrade to 3
  2394. if ((player.getSp() >= 350000) && (player.getInventory().getItemByItemId(1419) != null))
  2395. {
  2396. // itemId 1419 == Blood Mark
  2397. if (player.destroyItemByItemId("ClanLvl", 1419, 1, player.getTarget(), false))
  2398. {
  2399. player.setSp(player.getSp() - 350000);
  2400. SystemMessage sp = SystemMessage.getSystemMessage(SystemMessageId.SP_DECREASED_S1);
  2401. sp.addInt(350000);
  2402. player.sendPacket(sp);
  2403. sp = null;
  2404. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_DISAPPEARED);
  2405. sm.addItemName(1419);
  2406. player.sendPacket(sm);
  2407. sm = null;
  2408. increaseClanLevel = true;
  2409. }
  2410. }
  2411. break;
  2412. }
  2413. case 3:
  2414. {
  2415. // Upgrade to 4
  2416. if ((player.getSp() >= 1000000) && (player.getInventory().getItemByItemId(3874) != null))
  2417. {
  2418. // itemId 3874 == Alliance Manifesto
  2419. if (player.destroyItemByItemId("ClanLvl", 3874, 1, player.getTarget(), false))
  2420. {
  2421. player.setSp(player.getSp() - 1000000);
  2422. SystemMessage sp = SystemMessage.getSystemMessage(SystemMessageId.SP_DECREASED_S1);
  2423. sp.addInt(1000000);
  2424. player.sendPacket(sp);
  2425. sp = null;
  2426. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_DISAPPEARED);
  2427. sm.addItemName(3874);
  2428. player.sendPacket(sm);
  2429. sm = null;
  2430. increaseClanLevel = true;
  2431. }
  2432. }
  2433. break;
  2434. }
  2435. case 4:
  2436. {
  2437. // Upgrade to 5
  2438. if ((player.getSp() >= 2500000) && (player.getInventory().getItemByItemId(3870) != null))
  2439. {
  2440. // itemId 3870 == Seal of Aspiration
  2441. if (player.destroyItemByItemId("ClanLvl", 3870, 1, player.getTarget(), false))
  2442. {
  2443. player.setSp(player.getSp() - 2500000);
  2444. SystemMessage sp = SystemMessage.getSystemMessage(SystemMessageId.SP_DECREASED_S1);
  2445. sp.addInt(2500000);
  2446. player.sendPacket(sp);
  2447. sp = null;
  2448. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_DISAPPEARED);
  2449. sm.addItemName(3870);
  2450. player.sendPacket(sm);
  2451. sm = null;
  2452. increaseClanLevel = true;
  2453. }
  2454. }
  2455. break;
  2456. }
  2457. case 5:
  2458. // Upgrade to 6
  2459. if ((getReputationScore() >= Config.CLAN_LEVEL_6_COST) && (getMembersCount() >= Config.CLAN_LEVEL_6_REQUIREMENT))
  2460. {
  2461. setReputationScore(getReputationScore() - Config.CLAN_LEVEL_6_COST, true);
  2462. SystemMessage cr = SystemMessage.getSystemMessage(SystemMessageId.S1_DEDUCTED_FROM_CLAN_REP);
  2463. cr.addInt(Config.CLAN_LEVEL_6_COST);
  2464. player.sendPacket(cr);
  2465. cr = null;
  2466. increaseClanLevel = true;
  2467. }
  2468. break;
  2469. case 6:
  2470. // Upgrade to 7
  2471. if ((getReputationScore() >= Config.CLAN_LEVEL_7_COST) && (getMembersCount() >= Config.CLAN_LEVEL_7_REQUIREMENT))
  2472. {
  2473. setReputationScore(getReputationScore() - Config.CLAN_LEVEL_7_COST, true);
  2474. SystemMessage cr = SystemMessage.getSystemMessage(SystemMessageId.S1_DEDUCTED_FROM_CLAN_REP);
  2475. cr.addInt(Config.CLAN_LEVEL_7_COST);
  2476. player.sendPacket(cr);
  2477. cr = null;
  2478. increaseClanLevel = true;
  2479. }
  2480. break;
  2481. case 7:
  2482. // Upgrade to 8
  2483. if ((getReputationScore() >= Config.CLAN_LEVEL_8_COST) && (getMembersCount() >= Config.CLAN_LEVEL_8_REQUIREMENT))
  2484. {
  2485. setReputationScore(getReputationScore() - Config.CLAN_LEVEL_8_COST, true);
  2486. SystemMessage cr = SystemMessage.getSystemMessage(SystemMessageId.S1_DEDUCTED_FROM_CLAN_REP);
  2487. cr.addInt(Config.CLAN_LEVEL_8_COST);
  2488. player.sendPacket(cr);
  2489. cr = null;
  2490. increaseClanLevel = true;
  2491. }
  2492. break;
  2493. case 8:
  2494. // Upgrade to 9
  2495. if ((getReputationScore() >= Config.CLAN_LEVEL_9_COST) && (player.getInventory().getItemByItemId(9910) != null) && (getMembersCount() >= Config.CLAN_LEVEL_9_REQUIREMENT))
  2496. {
  2497. // itemId 9910 == Blood Oath
  2498. if (player.destroyItemByItemId("ClanLvl", 9910, 150, player.getTarget(), false))
  2499. {
  2500. setReputationScore(getReputationScore() - Config.CLAN_LEVEL_9_COST, true);
  2501. SystemMessage cr = SystemMessage.getSystemMessage(SystemMessageId.S1_DEDUCTED_FROM_CLAN_REP);
  2502. cr.addInt(Config.CLAN_LEVEL_9_COST);
  2503. player.sendPacket(cr);
  2504. cr = null;
  2505. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S2_S1_DISAPPEARED);
  2506. sm.addItemName(9910);
  2507. sm.addLong(150);
  2508. player.sendPacket(sm);
  2509. increaseClanLevel = true;
  2510. }
  2511. }
  2512. break;
  2513. case 9:
  2514. // Upgrade to 10
  2515. if ((getReputationScore() >= Config.CLAN_LEVEL_10_COST) && (player.getInventory().getItemByItemId(9911) != null) && (getMembersCount() >= Config.CLAN_LEVEL_10_REQUIREMENT))
  2516. {
  2517. // itemId 9911 == Blood Alliance
  2518. if (player.destroyItemByItemId("ClanLvl", 9911, 5, player.getTarget(), false))
  2519. {
  2520. setReputationScore(getReputationScore() - Config.CLAN_LEVEL_10_COST, true);
  2521. SystemMessage cr = SystemMessage.getSystemMessage(SystemMessageId.S1_DEDUCTED_FROM_CLAN_REP);
  2522. cr.addInt(Config.CLAN_LEVEL_10_COST);
  2523. player.sendPacket(cr);
  2524. cr = null;
  2525. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S2_S1_DISAPPEARED);
  2526. sm.addItemName(9911);
  2527. sm.addLong(5);
  2528. player.sendPacket(sm);
  2529. increaseClanLevel = true;
  2530. }
  2531. }
  2532. break;
  2533. case 10:
  2534. // Upgrade to 11
  2535. boolean hasTerritory = false;
  2536. for (Territory terr : TerritoryWarManager.getInstance().getAllTerritories())
  2537. {
  2538. if (terr.getOwnerClan().getId() == getId())
  2539. {
  2540. hasTerritory = true;
  2541. break;
  2542. }
  2543. }
  2544. if (hasTerritory && (getReputationScore() >= Config.CLAN_LEVEL_11_COST) && (getMembersCount() >= Config.CLAN_LEVEL_11_REQUIREMENT))
  2545. {
  2546. setReputationScore(getReputationScore() - Config.CLAN_LEVEL_11_COST, true);
  2547. SystemMessage cr = SystemMessage.getSystemMessage(SystemMessageId.S1_DEDUCTED_FROM_CLAN_REP);
  2548. cr.addInt(Config.CLAN_LEVEL_11_COST);
  2549. player.sendPacket(cr);
  2550. cr = null;
  2551. increaseClanLevel = true;
  2552. }
  2553. break;
  2554. default:
  2555. return false;
  2556. }
  2557. if (!increaseClanLevel)
  2558. {
  2559. player.sendPacket(SystemMessageId.FAILED_TO_INCREASE_CLAN_LEVEL);
  2560. return false;
  2561. }
  2562. // the player should know that he has less sp now :p
  2563. StatusUpdate su = new StatusUpdate(player);
  2564. su.addAttribute(StatusUpdate.SP, player.getSp());
  2565. player.sendPacket(su);
  2566. ItemList il = new ItemList(player, false);
  2567. player.sendPacket(il);
  2568. changeLevel(getLevel() + 1);
  2569. // Notify to scripts
  2570. EventDispatcher.getInstance().notifyEventAsync(new OnPlayerClanLvlUp(player, this));
  2571. return true;
  2572. }
  2573. public void changeLevel(int level)
  2574. {
  2575. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  2576. PreparedStatement ps = con.prepareStatement("UPDATE clan_data SET clan_level = ? WHERE clan_id = ?"))
  2577. {
  2578. ps.setInt(1, level);
  2579. ps.setInt(2, getId());
  2580. ps.execute();
  2581. }
  2582. catch (Exception e)
  2583. {
  2584. _log.log(Level.WARNING, "could not increase clan level:" + e.getMessage(), e);
  2585. }
  2586. setLevel(level);
  2587. if (getLeader().isOnline())
  2588. {
  2589. L2PcInstance leader = getLeader().getPlayerInstance();
  2590. if (level > 4)
  2591. {
  2592. SiegeManager.getInstance().addSiegeSkills(leader);
  2593. leader.sendPacket(SystemMessageId.CLAN_CAN_ACCUMULATE_CLAN_REPUTATION_POINTS);
  2594. }
  2595. else if (level < 5)
  2596. {
  2597. SiegeManager.getInstance().removeSiegeSkills(leader);
  2598. }
  2599. }
  2600. // notify all the members about it
  2601. broadcastToOnlineMembers(SystemMessage.getSystemMessage(SystemMessageId.CLAN_LEVEL_INCREASED));
  2602. broadcastToOnlineMembers(new PledgeShowInfoUpdate(this));
  2603. /*
  2604. * Micht : - use PledgeShowInfoUpdate instead of PledgeStatusChanged to update clan level ingame - remove broadcastClanStatus() to avoid members duplication
  2605. */
  2606. // clan.broadcastToOnlineMembers(new PledgeStatusChanged(clan));
  2607. // clan.broadcastClanStatus();
  2608. // notify CB server about the change
  2609. CommunityServerThread.getInstance().sendPacket(new WorldInfo(null, this, WorldInfo.TYPE_UPDATE_CLAN_DATA));
  2610. }
  2611. /**
  2612. * Change the clan crest. If crest id is 0, crest is removed. New crest id is saved to database.
  2613. * @param crestId if 0, crest is removed, else new crest id is set and saved to database
  2614. */
  2615. public void changeClanCrest(int crestId)
  2616. {
  2617. if (getCrestId() != 0)
  2618. {
  2619. CrestTable.getInstance().removeCrest(getCrestId());
  2620. }
  2621. setCrestId(crestId);
  2622. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  2623. PreparedStatement ps = con.prepareStatement("UPDATE clan_data SET crest_id = ? WHERE clan_id = ?"))
  2624. {
  2625. ps.setInt(1, crestId);
  2626. ps.setInt(2, getId());
  2627. ps.executeUpdate();
  2628. }
  2629. catch (SQLException e)
  2630. {
  2631. _log.log(Level.WARNING, "Could not update crest for clan " + getName() + " [" + getId() + "] : " + e.getMessage(), e);
  2632. }
  2633. for (L2PcInstance member : getOnlineMembers(0))
  2634. {
  2635. member.broadcastUserInfo();
  2636. }
  2637. }
  2638. /**
  2639. * Change the ally crest. If crest id is 0, crest is removed. New crest id is saved to database.
  2640. * @param crestId if 0, crest is removed, else new crest id is set and saved to database
  2641. * @param onlyThisClan
  2642. */
  2643. public void changeAllyCrest(int crestId, boolean onlyThisClan)
  2644. {
  2645. String sqlStatement = "UPDATE clan_data SET ally_crest_id = ? WHERE clan_id = ?";
  2646. int allyId = getId();
  2647. if (!onlyThisClan)
  2648. {
  2649. if (getAllyCrestId() != 0)
  2650. {
  2651. CrestTable.getInstance().removeCrest(getAllyCrestId());
  2652. }
  2653. sqlStatement = "UPDATE clan_data SET ally_crest_id = ? WHERE ally_id = ?";
  2654. allyId = getAllyId();
  2655. }
  2656. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  2657. PreparedStatement ps = con.prepareStatement(sqlStatement))
  2658. {
  2659. ps.setInt(1, crestId);
  2660. ps.setInt(2, allyId);
  2661. ps.executeUpdate();
  2662. }
  2663. catch (SQLException e)
  2664. {
  2665. _log.log(Level.WARNING, "Could not update ally crest for ally/clan id " + allyId + " : " + e.getMessage(), e);
  2666. }
  2667. if (onlyThisClan)
  2668. {
  2669. setAllyCrestId(crestId);
  2670. for (L2PcInstance member : getOnlineMembers(0))
  2671. {
  2672. member.broadcastUserInfo();
  2673. }
  2674. }
  2675. else
  2676. {
  2677. for (L2Clan clan : ClanTable.getInstance().getClanAllies(getAllyId()))
  2678. {
  2679. clan.setAllyCrestId(crestId);
  2680. for (L2PcInstance member : clan.getOnlineMembers(0))
  2681. {
  2682. member.broadcastUserInfo();
  2683. }
  2684. }
  2685. }
  2686. }
  2687. /**
  2688. * Change the large crest. If crest id is 0, crest is removed. New crest id is saved to database.
  2689. * @param crestId if 0, crest is removed, else new crest id is set and saved to database
  2690. */
  2691. public void changeLargeCrest(int crestId)
  2692. {
  2693. if (getCrestLargeId() != 0)
  2694. {
  2695. CrestTable.getInstance().removeCrest(getCrestLargeId());
  2696. }
  2697. setCrestLargeId(crestId);
  2698. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  2699. PreparedStatement ps = con.prepareStatement("UPDATE clan_data SET crest_large_id = ? WHERE clan_id = ?"))
  2700. {
  2701. ps.setInt(1, crestId);
  2702. ps.setInt(2, getId());
  2703. ps.executeUpdate();
  2704. }
  2705. catch (SQLException e)
  2706. {
  2707. _log.log(Level.WARNING, "Could not update large crest for clan " + getName() + " [" + getId() + "] : " + e.getMessage(), e);
  2708. }
  2709. for (L2PcInstance member : getOnlineMembers(0))
  2710. {
  2711. member.broadcastUserInfo();
  2712. }
  2713. }
  2714. /**
  2715. * Check if this clan can learn the skill for the given skill ID, level.
  2716. * @param skillId
  2717. * @param skillLevel
  2718. * @return {@code true} if skill can be learned.
  2719. */
  2720. public boolean isLearnableSubSkill(int skillId, int skillLevel)
  2721. {
  2722. Skill current = _subPledgeSkills.get(skillId);
  2723. // is next level?
  2724. if ((current != null) && ((current.getLevel() + 1) == skillLevel))
  2725. {
  2726. return true;
  2727. }
  2728. // is first level?
  2729. if ((current == null) && (skillLevel == 1))
  2730. {
  2731. return true;
  2732. }
  2733. // other sub-pledges
  2734. for (SubPledge subunit : _subPledges.values())
  2735. {
  2736. // disable academy
  2737. if (subunit.getId() == -1)
  2738. {
  2739. continue;
  2740. }
  2741. current = subunit.getSkill(skillId);
  2742. // is next level?
  2743. if ((current != null) && ((current.getLevel() + 1) == skillLevel))
  2744. {
  2745. return true;
  2746. }
  2747. // is first level?
  2748. if ((current == null) && (skillLevel == 1))
  2749. {
  2750. return true;
  2751. }
  2752. }
  2753. return false;
  2754. }
  2755. public boolean isLearnableSubPledgeSkill(Skill skill, int subType)
  2756. {
  2757. // academy
  2758. if (subType == -1)
  2759. {
  2760. return false;
  2761. }
  2762. int id = skill.getId();
  2763. Skill current;
  2764. if (subType == 0)
  2765. {
  2766. current = _subPledgeSkills.get(id);
  2767. }
  2768. else
  2769. {
  2770. current = _subPledges.get(subType).getSkill(id);
  2771. }
  2772. // is next level?
  2773. if ((current != null) && ((current.getLevel() + 1) == skill.getLevel()))
  2774. {
  2775. return true;
  2776. }
  2777. // is first level?
  2778. if ((current == null) && (skill.getLevel() == 1))
  2779. {
  2780. return true;
  2781. }
  2782. return false;
  2783. }
  2784. public SubPledgeSkill[] getAllSubSkills()
  2785. {
  2786. FastList<SubPledgeSkill> list = FastList.newInstance();
  2787. for (Skill skill : _subPledgeSkills.values())
  2788. {
  2789. list.add(new SubPledgeSkill(0, skill.getId(), skill.getLevel()));
  2790. }
  2791. for (SubPledge subunit : _subPledges.values())
  2792. {
  2793. for (Skill skill : subunit.getSkills())
  2794. {
  2795. list.add(new SubPledgeSkill(subunit.getId(), skill.getId(), skill.getLevel()));
  2796. }
  2797. }
  2798. SubPledgeSkill[] result = list.toArray(new SubPledgeSkill[list.size()]);
  2799. FastList.recycle(list);
  2800. return result;
  2801. }
  2802. public void setNewLeaderId(int objectId, boolean storeInDb)
  2803. {
  2804. _newLeaderId = objectId;
  2805. if (storeInDb)
  2806. {
  2807. updateClanInDB();
  2808. }
  2809. }
  2810. public int getNewLeaderId()
  2811. {
  2812. return _newLeaderId;
  2813. }
  2814. public L2PcInstance getNewLeader()
  2815. {
  2816. return L2World.getInstance().getPlayer(_newLeaderId);
  2817. }
  2818. public String getNewLeaderName()
  2819. {
  2820. return CharNameTable.getInstance().getNameById(_newLeaderId);
  2821. }
  2822. public int getSiegeKills()
  2823. {
  2824. return _siegeKills.get();
  2825. }
  2826. public int getSiegeDeaths()
  2827. {
  2828. return _siegeDeaths.get();
  2829. }
  2830. public int addSiegeKill()
  2831. {
  2832. return _siegeKills.incrementAndGet();
  2833. }
  2834. public int addSiegeDeath()
  2835. {
  2836. return _siegeDeaths.incrementAndGet();
  2837. }
  2838. public void clearSiegeKills()
  2839. {
  2840. _siegeKills.set(0);
  2841. }
  2842. public void clearSiegeDeaths()
  2843. {
  2844. _siegeDeaths.set(0);
  2845. }
  2846. }