Castle.java 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. /*
  2. * Copyright (C) 2004-2013 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.entity;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.ArrayList;
  24. import java.util.Calendar;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import javolution.util.FastMap;
  30. import com.l2jserver.Config;
  31. import com.l2jserver.L2DatabaseFactory;
  32. import com.l2jserver.gameserver.CastleUpdater;
  33. import com.l2jserver.gameserver.ThreadPoolManager;
  34. import com.l2jserver.gameserver.datatables.ClanTable;
  35. import com.l2jserver.gameserver.datatables.DoorTable;
  36. import com.l2jserver.gameserver.datatables.ManorData;
  37. import com.l2jserver.gameserver.datatables.SkillTable;
  38. import com.l2jserver.gameserver.datatables.SkillTreesData;
  39. import com.l2jserver.gameserver.instancemanager.CastleManager;
  40. import com.l2jserver.gameserver.instancemanager.CastleManorManager;
  41. import com.l2jserver.gameserver.instancemanager.CastleManorManager.CropProcure;
  42. import com.l2jserver.gameserver.instancemanager.CastleManorManager.SeedProduction;
  43. import com.l2jserver.gameserver.instancemanager.FortManager;
  44. import com.l2jserver.gameserver.instancemanager.SiegeManager;
  45. import com.l2jserver.gameserver.instancemanager.TerritoryWarManager;
  46. import com.l2jserver.gameserver.instancemanager.TerritoryWarManager.Territory;
  47. import com.l2jserver.gameserver.instancemanager.ZoneManager;
  48. import com.l2jserver.gameserver.model.L2Clan;
  49. import com.l2jserver.gameserver.model.L2Object;
  50. import com.l2jserver.gameserver.model.L2SkillLearn;
  51. import com.l2jserver.gameserver.model.TowerSpawn;
  52. import com.l2jserver.gameserver.model.actor.instance.L2ArtefactInstance;
  53. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  54. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  55. import com.l2jserver.gameserver.model.itemcontainer.PcInventory;
  56. import com.l2jserver.gameserver.model.skills.L2Skill;
  57. import com.l2jserver.gameserver.model.zone.type.L2CastleZone;
  58. import com.l2jserver.gameserver.model.zone.type.L2ResidenceTeleportZone;
  59. import com.l2jserver.gameserver.model.zone.type.L2SiegeZone;
  60. import com.l2jserver.gameserver.network.serverpackets.PlaySound;
  61. import com.l2jserver.gameserver.network.serverpackets.PledgeShowInfoUpdate;
  62. import gnu.trove.map.hash.TIntIntHashMap;
  63. public class Castle
  64. {
  65. protected static final Logger _log = Logger.getLogger(Castle.class.getName());
  66. private static final String CASTLE_MANOR_DELETE_PRODUCTION = "DELETE FROM castle_manor_production WHERE castle_id=?;";
  67. private static final String CASTLE_MANOR_DELETE_PRODUCTION_PERIOD = "DELETE FROM castle_manor_production WHERE castle_id=? AND period=?;";
  68. private static final String CASTLE_MANOR_DELETE_PROCURE = "DELETE FROM castle_manor_procure WHERE castle_id=?;";
  69. private static final String CASTLE_MANOR_DELETE_PROCURE_PERIOD = "DELETE FROM castle_manor_procure WHERE castle_id=? AND period=?;";
  70. private static final String CASTLE_UPDATE_CROP = "UPDATE castle_manor_procure SET can_buy=? WHERE crop_id=? AND castle_id=? AND period=?";
  71. private static final String CASTLE_UPDATE_SEED = "UPDATE castle_manor_production SET can_produce=? WHERE seed_id=? AND castle_id=? AND period=?";
  72. private int _castleId = 0;
  73. private final List<L2DoorInstance> _doors = new ArrayList<>();
  74. private String _name = "";
  75. private int _ownerId = 0;
  76. private Siege _siege = null;
  77. private Calendar _siegeDate;
  78. private boolean _isTimeRegistrationOver = true; // true if Castle Lords set the time, or 24h is elapsed after the siege
  79. private Calendar _siegeTimeRegistrationEndDate; // last siege end date + 1 day
  80. private int _taxPercent = 0;
  81. private double _taxRate = 0;
  82. private long _treasury = 0;
  83. private boolean _showNpcCrest = false;
  84. private L2SiegeZone _zone = null;
  85. private L2CastleZone _castleZone = null;
  86. private L2ResidenceTeleportZone _teleZone;
  87. private L2Clan _formerOwner = null;
  88. private final List<L2ArtefactInstance> _artefacts = new ArrayList<>(1);
  89. private final TIntIntHashMap _engrave = new TIntIntHashMap(1);
  90. private final Map<Integer, CastleFunction> _function;
  91. private final List<L2Skill> _residentialSkills = new ArrayList<>();
  92. private int _ticketBuyCount = 0;
  93. private List<CropProcure> _procure = new ArrayList<>();
  94. private List<SeedProduction> _production = new ArrayList<>();
  95. private List<CropProcure> _procureNext = new ArrayList<>();
  96. private List<SeedProduction> _productionNext = new ArrayList<>();
  97. private boolean _isNextPeriodApproved = false;
  98. /** Castle Functions */
  99. public static final int FUNC_TELEPORT = 1;
  100. public static final int FUNC_RESTORE_HP = 2;
  101. public static final int FUNC_RESTORE_MP = 3;
  102. public static final int FUNC_RESTORE_EXP = 4;
  103. public static final int FUNC_SUPPORT = 5;
  104. public class CastleFunction
  105. {
  106. private final int _type;
  107. private int _lvl;
  108. protected int _fee;
  109. protected int _tempFee;
  110. private final long _rate;
  111. private long _endDate;
  112. protected boolean _inDebt;
  113. public boolean _cwh;
  114. public CastleFunction(int type, int lvl, int lease, int tempLease, long rate, long time, boolean cwh)
  115. {
  116. _type = type;
  117. _lvl = lvl;
  118. _fee = lease;
  119. _tempFee = tempLease;
  120. _rate = rate;
  121. _endDate = time;
  122. initializeTask(cwh);
  123. }
  124. public int getType()
  125. {
  126. return _type;
  127. }
  128. public int getLvl()
  129. {
  130. return _lvl;
  131. }
  132. public int getLease()
  133. {
  134. return _fee;
  135. }
  136. public long getRate()
  137. {
  138. return _rate;
  139. }
  140. public long getEndTime()
  141. {
  142. return _endDate;
  143. }
  144. public void setLvl(int lvl)
  145. {
  146. _lvl = lvl;
  147. }
  148. public void setLease(int lease)
  149. {
  150. _fee = lease;
  151. }
  152. public void setEndTime(long time)
  153. {
  154. _endDate = time;
  155. }
  156. private void initializeTask(boolean cwh)
  157. {
  158. if (getOwnerId() <= 0)
  159. {
  160. return;
  161. }
  162. long currentTime = System.currentTimeMillis();
  163. if (_endDate > currentTime)
  164. {
  165. ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(cwh), _endDate - currentTime);
  166. }
  167. else
  168. {
  169. ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(cwh), 0);
  170. }
  171. }
  172. private class FunctionTask implements Runnable
  173. {
  174. public FunctionTask(boolean cwh)
  175. {
  176. _cwh = cwh;
  177. }
  178. @Override
  179. public void run()
  180. {
  181. try
  182. {
  183. if (getOwnerId() <= 0)
  184. {
  185. return;
  186. }
  187. if ((ClanTable.getInstance().getClan(getOwnerId()).getWarehouse().getAdena() >= _fee) || !_cwh)
  188. {
  189. int fee = _fee;
  190. if (getEndTime() == -1)
  191. {
  192. fee = _tempFee;
  193. }
  194. setEndTime(System.currentTimeMillis() + getRate());
  195. dbSave();
  196. if (_cwh)
  197. {
  198. ClanTable.getInstance().getClan(getOwnerId()).getWarehouse().destroyItemByItemId("CS_function_fee", PcInventory.ADENA_ID, fee, null, null);
  199. }
  200. ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(true), getRate());
  201. }
  202. else
  203. {
  204. removeFunction(getType());
  205. }
  206. }
  207. catch (Exception e)
  208. {
  209. _log.log(Level.SEVERE, "", e);
  210. }
  211. }
  212. }
  213. public void dbSave()
  214. {
  215. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  216. PreparedStatement ps = con.prepareStatement("REPLACE INTO castle_functions (castle_id, type, lvl, lease, rate, endTime) VALUES (?,?,?,?,?,?)"))
  217. {
  218. ps.setInt(1, getCastleId());
  219. ps.setInt(2, getType());
  220. ps.setInt(3, getLvl());
  221. ps.setInt(4, getLease());
  222. ps.setLong(5, getRate());
  223. ps.setLong(6, getEndTime());
  224. ps.execute();
  225. }
  226. catch (Exception e)
  227. {
  228. _log.log(Level.SEVERE, "Exception: Castle.updateFunctions(int type, int lvl, int lease, long rate, long time, boolean addNew): " + e.getMessage(), e);
  229. }
  230. }
  231. }
  232. public Castle(int castleId)
  233. {
  234. _castleId = castleId;
  235. /*
  236. * if (_castleId == 7 || castleId == 9) // Goddard and Schuttgart _nbArtifact = 2;
  237. */
  238. load();
  239. _function = new FastMap<>();
  240. final List<L2SkillLearn> residentialSkills = SkillTreesData.getInstance().getAvailableResidentialSkills(castleId);
  241. for (L2SkillLearn s : residentialSkills)
  242. {
  243. final L2Skill sk = SkillTable.getInstance().getInfo(s.getSkillId(), s.getSkillLevel());
  244. if (sk != null)
  245. {
  246. _residentialSkills.add(sk);
  247. }
  248. else
  249. {
  250. _log.warning("Castle Id: " + castleId + " has a null residential skill Id: " + s.getSkillId() + " level: " + s.getSkillLevel() + "!");
  251. }
  252. }
  253. if (getOwnerId() != 0)
  254. {
  255. loadFunctions();
  256. loadDoorUpgrade();
  257. }
  258. }
  259. /**
  260. * Return function with id
  261. * @param type
  262. * @return
  263. */
  264. public CastleFunction getFunction(int type)
  265. {
  266. if (_function.containsKey(type))
  267. {
  268. return _function.get(type);
  269. }
  270. return null;
  271. }
  272. public synchronized void engrave(L2Clan clan, L2Object target)
  273. {
  274. if (!_artefacts.contains(target))
  275. {
  276. return;
  277. }
  278. _engrave.put(target.getObjectId(), clan.getClanId());
  279. if (_engrave.size() == _artefacts.size())
  280. {
  281. for (L2ArtefactInstance art : _artefacts)
  282. {
  283. if (_engrave.get(art.getObjectId()) != clan.getClanId())
  284. {
  285. return;
  286. }
  287. }
  288. _engrave.clear();
  289. setOwner(clan);
  290. }
  291. }
  292. // This method add to the treasury
  293. /**
  294. * Add amount to castle instance's treasury (warehouse).
  295. * @param amount
  296. */
  297. public void addToTreasury(long amount)
  298. {
  299. // check if owned
  300. if (getOwnerId() <= 0)
  301. {
  302. return;
  303. }
  304. if (_name.equalsIgnoreCase("Schuttgart") || _name.equalsIgnoreCase("Goddard"))
  305. {
  306. Castle rune = CastleManager.getInstance().getCastle("rune");
  307. if (rune != null)
  308. {
  309. long runeTax = (long) (amount * rune.getTaxRate());
  310. if (rune.getOwnerId() > 0)
  311. {
  312. rune.addToTreasury(runeTax);
  313. }
  314. amount -= runeTax;
  315. }
  316. }
  317. if (!_name.equalsIgnoreCase("aden") && !_name.equalsIgnoreCase("Rune") && !_name.equalsIgnoreCase("Schuttgart") && !_name.equalsIgnoreCase("Goddard")) // If current castle instance is not Aden, Rune, Goddard or Schuttgart.
  318. {
  319. Castle aden = CastleManager.getInstance().getCastle("aden");
  320. if (aden != null)
  321. {
  322. long adenTax = (long) (amount * aden.getTaxRate()); // Find out what Aden gets from the current castle instance's income
  323. if (aden.getOwnerId() > 0)
  324. {
  325. aden.addToTreasury(adenTax); // Only bother to really add the tax to the treasury if not npc owned
  326. }
  327. amount -= adenTax; // Subtract Aden's income from current castle instance's income
  328. }
  329. }
  330. addToTreasuryNoTax(amount);
  331. }
  332. /**
  333. * Add amount to castle instance's treasury (warehouse), no tax paying.
  334. * @param amount
  335. * @return
  336. */
  337. public boolean addToTreasuryNoTax(long amount)
  338. {
  339. if (getOwnerId() <= 0)
  340. {
  341. return false;
  342. }
  343. if (amount < 0)
  344. {
  345. amount *= -1;
  346. if (_treasury < amount)
  347. {
  348. return false;
  349. }
  350. _treasury -= amount;
  351. }
  352. else
  353. {
  354. if ((_treasury + amount) > PcInventory.MAX_ADENA)
  355. {
  356. _treasury = PcInventory.MAX_ADENA;
  357. }
  358. else
  359. {
  360. _treasury += amount;
  361. }
  362. }
  363. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  364. PreparedStatement ps = con.prepareStatement("UPDATE castle SET treasury = ? WHERE id = ?"))
  365. {
  366. ps.setLong(1, getTreasury());
  367. ps.setInt(2, getCastleId());
  368. ps.execute();
  369. }
  370. catch (Exception e)
  371. {
  372. _log.log(Level.WARNING, e.getMessage(), e);
  373. }
  374. return true;
  375. }
  376. /**
  377. * Move non clan members off castle area and to nearest town.
  378. */
  379. public void banishForeigners()
  380. {
  381. getCastleZone().banishForeigners(getOwnerId());
  382. }
  383. /**
  384. * Return true if object is inside the zone
  385. * @param x
  386. * @param y
  387. * @param z
  388. * @return
  389. */
  390. public boolean checkIfInZone(int x, int y, int z)
  391. {
  392. return getZone().isInsideZone(x, y, z);
  393. }
  394. public L2SiegeZone getZone()
  395. {
  396. if (_zone == null)
  397. {
  398. for (L2SiegeZone zone : ZoneManager.getInstance().getAllZones(L2SiegeZone.class))
  399. {
  400. if (zone.getSiegeObjectId() == getCastleId())
  401. {
  402. _zone = zone;
  403. break;
  404. }
  405. }
  406. }
  407. return _zone;
  408. }
  409. public L2CastleZone getCastleZone()
  410. {
  411. if (_castleZone == null)
  412. {
  413. for (L2CastleZone zone : ZoneManager.getInstance().getAllZones(L2CastleZone.class))
  414. {
  415. if (zone.getCastleId() == getCastleId())
  416. {
  417. _castleZone = zone;
  418. break;
  419. }
  420. }
  421. }
  422. return _castleZone;
  423. }
  424. public L2ResidenceTeleportZone getTeleZone()
  425. {
  426. if (_teleZone == null)
  427. {
  428. for (L2ResidenceTeleportZone zone : ZoneManager.getInstance().getAllZones(L2ResidenceTeleportZone.class))
  429. {
  430. if (zone.getResidenceId() == getCastleId())
  431. {
  432. _teleZone = zone;
  433. break;
  434. }
  435. }
  436. }
  437. return _teleZone;
  438. }
  439. public void oustAllPlayers()
  440. {
  441. getTeleZone().oustAllPlayers();
  442. }
  443. /**
  444. * Get the objects distance to this castle
  445. * @param obj
  446. * @return
  447. */
  448. public double getDistance(L2Object obj)
  449. {
  450. return getZone().getDistanceToZone(obj);
  451. }
  452. public void closeDoor(L2PcInstance activeChar, int doorId)
  453. {
  454. openCloseDoor(activeChar, doorId, false);
  455. }
  456. public void openDoor(L2PcInstance activeChar, int doorId)
  457. {
  458. openCloseDoor(activeChar, doorId, true);
  459. }
  460. public void openCloseDoor(L2PcInstance activeChar, int doorId, boolean open)
  461. {
  462. if (activeChar.getClanId() != getOwnerId())
  463. {
  464. return;
  465. }
  466. L2DoorInstance door = getDoor(doorId);
  467. if (door != null)
  468. {
  469. if (open)
  470. {
  471. door.openMe();
  472. }
  473. else
  474. {
  475. door.closeMe();
  476. }
  477. }
  478. }
  479. // This method is used to begin removing all castle upgrades
  480. public void removeUpgrade()
  481. {
  482. removeDoorUpgrade();
  483. removeTrapUpgrade();
  484. for (Integer fc : _function.keySet())
  485. {
  486. removeFunction(fc);
  487. }
  488. _function.clear();
  489. }
  490. // This method updates the castle tax rate
  491. public void setOwner(L2Clan clan)
  492. {
  493. // Remove old owner
  494. if ((getOwnerId() > 0) && ((clan == null) || (clan.getClanId() != getOwnerId())))
  495. {
  496. L2Clan oldOwner = ClanTable.getInstance().getClan(getOwnerId()); // Try to find clan instance
  497. if (oldOwner != null)
  498. {
  499. if (_formerOwner == null)
  500. {
  501. _formerOwner = oldOwner;
  502. if (Config.REMOVE_CASTLE_CIRCLETS)
  503. {
  504. CastleManager.getInstance().removeCirclet(_formerOwner, getCastleId());
  505. }
  506. }
  507. try
  508. {
  509. L2PcInstance oldleader = oldOwner.getLeader().getPlayerInstance();
  510. if (oldleader != null)
  511. {
  512. if (oldleader.getMountType() == 2)
  513. {
  514. oldleader.dismount();
  515. }
  516. }
  517. }
  518. catch (Exception e)
  519. {
  520. _log.log(Level.WARNING, "Exception in setOwner: " + e.getMessage(), e);
  521. }
  522. oldOwner.setCastleId(0); // Unset has castle flag for old owner
  523. for (L2PcInstance member : oldOwner.getOnlineMembers(0))
  524. {
  525. removeResidentialSkills(member);
  526. member.sendSkillList();
  527. }
  528. }
  529. }
  530. updateOwnerInDB(clan); // Update in database
  531. setShowNpcCrest(false);
  532. // if clan have fortress, remove it
  533. if ((clan != null) && (clan.getFortId() > 0))
  534. {
  535. FortManager.getInstance().getFortByOwner(clan).removeOwner(true);
  536. }
  537. if (getSiege().getIsInProgress())
  538. {
  539. getSiege().midVictory(); // Mid victory phase of siege
  540. }
  541. TerritoryWarManager.getInstance().getTerritory(_castleId).setOwnerClan(clan);
  542. if (clan != null)
  543. {
  544. for (L2PcInstance member : clan.getOnlineMembers(0))
  545. {
  546. giveResidentialSkills(member);
  547. member.sendSkillList();
  548. }
  549. }
  550. }
  551. public void removeOwner(L2Clan clan)
  552. {
  553. if (clan != null)
  554. {
  555. _formerOwner = clan;
  556. if (Config.REMOVE_CASTLE_CIRCLETS)
  557. {
  558. CastleManager.getInstance().removeCirclet(_formerOwner, getCastleId());
  559. }
  560. for (L2PcInstance member : clan.getOnlineMembers(0))
  561. {
  562. removeResidentialSkills(member);
  563. member.sendSkillList();
  564. }
  565. clan.setCastleId(0);
  566. clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
  567. }
  568. updateOwnerInDB(null);
  569. if (getSiege().getIsInProgress())
  570. {
  571. getSiege().midVictory();
  572. }
  573. for (Integer fc : _function.keySet())
  574. {
  575. removeFunction(fc);
  576. }
  577. _function.clear();
  578. }
  579. public void setTaxPercent(int taxPercent)
  580. {
  581. _taxPercent = taxPercent;
  582. _taxRate = _taxPercent / 100.0;
  583. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  584. PreparedStatement ps = con.prepareStatement("UPDATE castle SET taxPercent = ? WHERE id = ?"))
  585. {
  586. ps.setInt(1, taxPercent);
  587. ps.setInt(2, getCastleId());
  588. ps.execute();
  589. }
  590. catch (Exception e)
  591. {
  592. _log.log(Level.WARNING, e.getMessage(), e);
  593. }
  594. }
  595. /**
  596. * Respawn all doors on castle grounds.
  597. */
  598. public void spawnDoor()
  599. {
  600. spawnDoor(false);
  601. }
  602. /**
  603. * Respawn all doors on castle grounds<BR>
  604. * <BR>
  605. * @param isDoorWeak
  606. */
  607. public void spawnDoor(boolean isDoorWeak)
  608. {
  609. for (L2DoorInstance door : _doors)
  610. {
  611. if (door.isDead())
  612. {
  613. door.doRevive();
  614. door.setCurrentHp((isDoorWeak) ? (door.getMaxHp() / 2) : (door.getMaxHp()));
  615. }
  616. if (door.getOpen())
  617. {
  618. door.closeMe();
  619. }
  620. }
  621. }
  622. // This method loads castle
  623. private void load()
  624. {
  625. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  626. PreparedStatement ps1 = con.prepareStatement("SELECT * FROM castle WHERE id = ?");
  627. PreparedStatement ps2 = con.prepareStatement("SELECT clan_id FROM clan_data WHERE hasCastle = ?"))
  628. {
  629. ps1.setInt(1, getCastleId());
  630. try (ResultSet rs = ps1.executeQuery())
  631. {
  632. while (rs.next())
  633. {
  634. _name = rs.getString("name");
  635. // _OwnerId = rs.getInt("ownerId");
  636. _siegeDate = Calendar.getInstance();
  637. _siegeDate.setTimeInMillis(rs.getLong("siegeDate"));
  638. _siegeTimeRegistrationEndDate = Calendar.getInstance();
  639. _siegeTimeRegistrationEndDate.setTimeInMillis(rs.getLong("regTimeEnd"));
  640. _isTimeRegistrationOver = rs.getBoolean("regTimeOver");
  641. _taxPercent = rs.getInt("taxPercent");
  642. _treasury = rs.getLong("treasury");
  643. _showNpcCrest = rs.getBoolean("showNpcCrest");
  644. _ticketBuyCount = rs.getInt("ticketBuyCount");
  645. }
  646. }
  647. _taxRate = _taxPercent / 100.0;
  648. ps2.setInt(1, getCastleId());
  649. try (ResultSet rs = ps2.executeQuery())
  650. {
  651. while (rs.next())
  652. {
  653. _ownerId = rs.getInt("clan_id");
  654. }
  655. }
  656. if (getOwnerId() > 0)
  657. {
  658. L2Clan clan = ClanTable.getInstance().getClan(getOwnerId()); // Try to find clan instance
  659. ThreadPoolManager.getInstance().scheduleGeneral(new CastleUpdater(clan, 1), 3600000); // Schedule owner tasks to start running
  660. }
  661. }
  662. catch (Exception e)
  663. {
  664. _log.log(Level.WARNING, "Exception: loadCastleData(): " + e.getMessage(), e);
  665. }
  666. }
  667. /** Load All Functions */
  668. private void loadFunctions()
  669. {
  670. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  671. PreparedStatement ps = con.prepareStatement("SELECT * FROM castle_functions WHERE castle_id = ?"))
  672. {
  673. ps.setInt(1, getCastleId());
  674. try (ResultSet rs = ps.executeQuery())
  675. {
  676. while (rs.next())
  677. {
  678. _function.put(rs.getInt("type"), new CastleFunction(rs.getInt("type"), rs.getInt("lvl"), rs.getInt("lease"), 0, rs.getLong("rate"), rs.getLong("endTime"), true));
  679. }
  680. }
  681. }
  682. catch (Exception e)
  683. {
  684. _log.log(Level.SEVERE, "Exception: Castle.loadFunctions(): " + e.getMessage(), e);
  685. }
  686. }
  687. /**
  688. * Remove function In List and in DB
  689. * @param functionType
  690. */
  691. public void removeFunction(int functionType)
  692. {
  693. _function.remove(functionType);
  694. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  695. PreparedStatement ps = con.prepareStatement("DELETE FROM castle_functions WHERE castle_id=? AND type=?"))
  696. {
  697. ps.setInt(1, getCastleId());
  698. ps.setInt(2, functionType);
  699. ps.execute();
  700. }
  701. catch (Exception e)
  702. {
  703. _log.log(Level.SEVERE, "Exception: Castle.removeFunctions(int functionType): " + e.getMessage(), e);
  704. }
  705. }
  706. public boolean updateFunctions(L2PcInstance player, int type, int lvl, int lease, long rate, boolean addNew)
  707. {
  708. if (player == null)
  709. {
  710. return false;
  711. }
  712. if (lease > 0)
  713. {
  714. if (!player.destroyItemByItemId("Consume", PcInventory.ADENA_ID, lease, null, true))
  715. {
  716. return false;
  717. }
  718. }
  719. if (addNew)
  720. {
  721. _function.put(type, new CastleFunction(type, lvl, lease, 0, rate, 0, false));
  722. }
  723. else
  724. {
  725. if ((lvl == 0) && (lease == 0))
  726. {
  727. removeFunction(type);
  728. }
  729. else
  730. {
  731. int diffLease = lease - _function.get(type).getLease();
  732. if (diffLease > 0)
  733. {
  734. _function.remove(type);
  735. _function.put(type, new CastleFunction(type, lvl, lease, 0, rate, -1, false));
  736. }
  737. else
  738. {
  739. _function.get(type).setLease(lease);
  740. _function.get(type).setLvl(lvl);
  741. _function.get(type).dbSave();
  742. }
  743. }
  744. }
  745. return true;
  746. }
  747. public void activateInstance()
  748. {
  749. loadDoor();
  750. }
  751. // This method loads castle door data from database
  752. private void loadDoor()
  753. {
  754. for (L2DoorInstance door : DoorTable.getInstance().getDoors())
  755. {
  756. if ((door.getCastle() != null) && (door.getCastle().getCastleId() == getCastleId()))
  757. {
  758. _doors.add(door);
  759. }
  760. }
  761. }
  762. // This method loads castle door upgrade data from database
  763. private void loadDoorUpgrade()
  764. {
  765. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  766. PreparedStatement ps = con.prepareStatement("SELECT * FROM castle_doorupgrade WHERE castleId=?"))
  767. {
  768. ps.setInt(1, getCastleId());
  769. try (ResultSet rs = ps.executeQuery())
  770. {
  771. while (rs.next())
  772. {
  773. setDoorUpgrade(rs.getInt("doorId"), rs.getInt("ratio"), false);
  774. }
  775. }
  776. }
  777. catch (Exception e)
  778. {
  779. _log.log(Level.WARNING, "Exception: loadCastleDoorUpgrade(): " + e.getMessage(), e);
  780. }
  781. }
  782. private void removeDoorUpgrade()
  783. {
  784. for (L2DoorInstance door : _doors)
  785. {
  786. door.getStat().setUpgradeHpRatio(1);
  787. door.setCurrentHp(door.getCurrentHp());
  788. }
  789. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  790. PreparedStatement ps = con.prepareStatement("DELETE FROM castle_doorupgrade WHERE castleId=?"))
  791. {
  792. ps.setInt(1, getCastleId());
  793. ps.execute();
  794. }
  795. catch (Exception e)
  796. {
  797. _log.log(Level.WARNING, "Exception: removeDoorUpgrade(): " + e.getMessage(), e);
  798. }
  799. }
  800. public void setDoorUpgrade(int doorId, int ratio, boolean save)
  801. {
  802. final L2DoorInstance door = (getDoors().isEmpty()) ? DoorTable.getInstance().getDoor(doorId) : getDoor(doorId);
  803. if (door == null)
  804. {
  805. return;
  806. }
  807. door.getStat().setUpgradeHpRatio(ratio);
  808. door.setCurrentHp(door.getMaxHp());
  809. if (save)
  810. {
  811. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  812. PreparedStatement ps = con.prepareStatement("REPLACE INTO castle_doorupgrade (doorId, ratio, castleId) values (?,?,?)"))
  813. {
  814. ps.setInt(1, doorId);
  815. ps.setInt(2, ratio);
  816. ps.setInt(3, getCastleId());
  817. ps.execute();
  818. }
  819. catch (Exception e)
  820. {
  821. _log.log(Level.WARNING, "Exception: setDoorUpgrade(int doorId, int ratio, int castleId): " + e.getMessage(), e);
  822. }
  823. }
  824. }
  825. private void updateOwnerInDB(L2Clan clan)
  826. {
  827. if (clan != null)
  828. {
  829. _ownerId = clan.getClanId(); // Update owner id property
  830. }
  831. else
  832. {
  833. _ownerId = 0; // Remove owner
  834. resetManor();
  835. }
  836. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  837. {
  838. // NEED TO REMOVE HAS CASTLE FLAG FROM CLAN_DATA
  839. // SHOULD BE CHECKED FROM CASTLE TABLE
  840. PreparedStatement statement = con.prepareStatement("UPDATE clan_data SET hasCastle = 0 WHERE hasCastle = ?");
  841. statement.setInt(1, getCastleId());
  842. statement.execute();
  843. statement.close();
  844. statement = con.prepareStatement("UPDATE clan_data SET hasCastle = ? WHERE clan_id = ?");
  845. statement.setInt(1, getCastleId());
  846. statement.setInt(2, getOwnerId());
  847. statement.execute();
  848. statement.close();
  849. // Announce to clan members
  850. if (clan != null)
  851. {
  852. clan.setCastleId(getCastleId()); // Set has castle flag for new owner
  853. clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
  854. clan.broadcastToOnlineMembers(new PlaySound(1, "Siege_Victory", 0, 0, 0, 0, 0));
  855. ThreadPoolManager.getInstance().scheduleGeneral(new CastleUpdater(clan, 1), 3600000); // Schedule owner tasks to start running
  856. }
  857. }
  858. catch (Exception e)
  859. {
  860. _log.log(Level.WARNING, "Exception: updateOwnerInDB(L2Clan clan): " + e.getMessage(), e);
  861. }
  862. }
  863. public final int getCastleId()
  864. {
  865. return _castleId;
  866. }
  867. public final L2DoorInstance getDoor(int doorId)
  868. {
  869. if (doorId <= 0)
  870. {
  871. return null;
  872. }
  873. for (L2DoorInstance door : getDoors())
  874. {
  875. if (door.getDoorId() == doorId)
  876. {
  877. return door;
  878. }
  879. }
  880. return null;
  881. }
  882. public final List<L2DoorInstance> getDoors()
  883. {
  884. return _doors;
  885. }
  886. public final String getName()
  887. {
  888. return _name;
  889. }
  890. public final int getOwnerId()
  891. {
  892. return _ownerId;
  893. }
  894. public final Siege getSiege()
  895. {
  896. if (_siege == null)
  897. {
  898. _siege = new Siege(this);
  899. }
  900. return _siege;
  901. }
  902. public final Calendar getSiegeDate()
  903. {
  904. return _siegeDate;
  905. }
  906. public boolean getIsTimeRegistrationOver()
  907. {
  908. return _isTimeRegistrationOver;
  909. }
  910. public void setIsTimeRegistrationOver(boolean val)
  911. {
  912. _isTimeRegistrationOver = val;
  913. }
  914. public Calendar getTimeRegistrationOverDate()
  915. {
  916. if (_siegeTimeRegistrationEndDate == null)
  917. {
  918. _siegeTimeRegistrationEndDate = Calendar.getInstance();
  919. }
  920. return _siegeTimeRegistrationEndDate;
  921. }
  922. public final int getTaxPercent()
  923. {
  924. return _taxPercent;
  925. }
  926. public final double getTaxRate()
  927. {
  928. return _taxRate;
  929. }
  930. public final long getTreasury()
  931. {
  932. return _treasury;
  933. }
  934. public final boolean getShowNpcCrest()
  935. {
  936. return _showNpcCrest;
  937. }
  938. public final void setShowNpcCrest(boolean showNpcCrest)
  939. {
  940. if (_showNpcCrest != showNpcCrest)
  941. {
  942. _showNpcCrest = showNpcCrest;
  943. updateShowNpcCrest();
  944. }
  945. }
  946. public List<SeedProduction> getSeedProduction(int period)
  947. {
  948. return (period == CastleManorManager.PERIOD_CURRENT ? _production : _productionNext);
  949. }
  950. public List<CropProcure> getCropProcure(int period)
  951. {
  952. return (period == CastleManorManager.PERIOD_CURRENT ? _procure : _procureNext);
  953. }
  954. public void setSeedProduction(List<SeedProduction> seed, int period)
  955. {
  956. if (period == CastleManorManager.PERIOD_CURRENT)
  957. {
  958. _production = seed;
  959. }
  960. else
  961. {
  962. _productionNext = seed;
  963. }
  964. }
  965. public void setCropProcure(List<CropProcure> crop, int period)
  966. {
  967. if (period == CastleManorManager.PERIOD_CURRENT)
  968. {
  969. _procure = crop;
  970. }
  971. else
  972. {
  973. _procureNext = crop;
  974. }
  975. }
  976. public SeedProduction getSeed(int seedId, int period)
  977. {
  978. for (SeedProduction seed : getSeedProduction(period))
  979. {
  980. if (seed.getId() == seedId)
  981. {
  982. return seed;
  983. }
  984. }
  985. return null;
  986. }
  987. public CropProcure getCrop(int cropId, int period)
  988. {
  989. for (CropProcure crop : getCropProcure(period))
  990. {
  991. if (crop.getId() == cropId)
  992. {
  993. return crop;
  994. }
  995. }
  996. return null;
  997. }
  998. public long getManorCost(int period)
  999. {
  1000. List<CropProcure> procure;
  1001. List<SeedProduction> production;
  1002. if (period == CastleManorManager.PERIOD_CURRENT)
  1003. {
  1004. procure = _procure;
  1005. production = _production;
  1006. }
  1007. else
  1008. {
  1009. procure = _procureNext;
  1010. production = _productionNext;
  1011. }
  1012. long total = 0;
  1013. if (production != null)
  1014. {
  1015. for (SeedProduction seed : production)
  1016. {
  1017. total += ManorData.getInstance().getSeedBuyPrice(seed.getId()) * seed.getStartProduce();
  1018. }
  1019. }
  1020. if (procure != null)
  1021. {
  1022. for (CropProcure crop : procure)
  1023. {
  1024. total += crop.getPrice() * crop.getStartAmount();
  1025. }
  1026. }
  1027. return total;
  1028. }
  1029. // save manor production data
  1030. public void saveSeedData()
  1031. {
  1032. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  1033. {
  1034. try (PreparedStatement ps1 = con.prepareStatement(CASTLE_MANOR_DELETE_PRODUCTION))
  1035. {
  1036. ps1.setInt(1, getCastleId());
  1037. ps1.execute();
  1038. }
  1039. if (_production != null)
  1040. {
  1041. int count = 0;
  1042. StringBuilder query = new StringBuilder();
  1043. query.append("INSERT INTO castle_manor_production VALUES ");
  1044. String values[] = new String[_production.size()];
  1045. for (SeedProduction s : _production)
  1046. {
  1047. values[count++] = "(" + getCastleId() + "," + s.getId() + "," + s.getCanProduce() + "," + s.getStartProduce() + "," + s.getPrice() + "," + CastleManorManager.PERIOD_CURRENT + ")";
  1048. }
  1049. if (values.length > 0)
  1050. {
  1051. query.append(values[0]);
  1052. for (int i = 1; i < values.length; i++)
  1053. {
  1054. query.append(',');
  1055. query.append(values[i]);
  1056. }
  1057. try (PreparedStatement ps2 = con.prepareStatement(query.toString()))
  1058. {
  1059. ps2.execute();
  1060. }
  1061. }
  1062. }
  1063. if (_productionNext != null)
  1064. {
  1065. int count = 0;
  1066. String query = "INSERT INTO castle_manor_production VALUES ";
  1067. String values[] = new String[_productionNext.size()];
  1068. for (SeedProduction s : _productionNext)
  1069. {
  1070. values[count++] = "(" + getCastleId() + "," + s.getId() + "," + s.getCanProduce() + "," + s.getStartProduce() + "," + s.getPrice() + "," + CastleManorManager.PERIOD_NEXT + ")";
  1071. }
  1072. if (values.length > 0)
  1073. {
  1074. query += values[0];
  1075. for (int i = 1; i < values.length; i++)
  1076. {
  1077. query += "," + values[i];
  1078. }
  1079. try (PreparedStatement ps3 = con.prepareStatement(query))
  1080. {
  1081. ps3.execute();
  1082. }
  1083. }
  1084. }
  1085. }
  1086. catch (Exception e)
  1087. {
  1088. _log.info("Error adding seed production data for castle " + getName() + ": " + e.getMessage());
  1089. }
  1090. }
  1091. // save manor production data for specified period
  1092. public void saveSeedData(int period)
  1093. {
  1094. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  1095. {
  1096. PreparedStatement statement = con.prepareStatement(CASTLE_MANOR_DELETE_PRODUCTION_PERIOD);
  1097. statement.setInt(1, getCastleId());
  1098. statement.setInt(2, period);
  1099. statement.execute();
  1100. statement.close();
  1101. List<SeedProduction> prod = null;
  1102. prod = getSeedProduction(period);
  1103. if (prod != null)
  1104. {
  1105. int count = 0;
  1106. StringBuilder query = new StringBuilder();
  1107. query.append("INSERT INTO castle_manor_production VALUES ");
  1108. String values[] = new String[prod.size()];
  1109. for (SeedProduction s : prod)
  1110. {
  1111. values[count++] = "(" + getCastleId() + "," + s.getId() + "," + s.getCanProduce() + "," + s.getStartProduce() + "," + s.getPrice() + "," + period + ")";
  1112. }
  1113. if (values.length > 0)
  1114. {
  1115. query.append(values[0]);
  1116. for (int i = 1; i < values.length; i++)
  1117. {
  1118. query.append(',').append(values[i]);
  1119. }
  1120. statement = con.prepareStatement(query.toString());
  1121. statement.execute();
  1122. statement.close();
  1123. }
  1124. }
  1125. }
  1126. catch (Exception e)
  1127. {
  1128. _log.info("Error adding seed production data for castle " + getName() + ": " + e.getMessage());
  1129. }
  1130. }
  1131. // save crop procure data
  1132. public void saveCropData()
  1133. {
  1134. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  1135. {
  1136. try (PreparedStatement ps1 = con.prepareStatement(CASTLE_MANOR_DELETE_PROCURE))
  1137. {
  1138. ps1.setInt(1, getCastleId());
  1139. ps1.execute();
  1140. }
  1141. if (!_procure.isEmpty())
  1142. {
  1143. int count = 0;
  1144. StringBuilder query = new StringBuilder();
  1145. query.append("INSERT INTO castle_manor_procure VALUES ");
  1146. String values[] = new String[_procure.size()];
  1147. for (CropProcure cp : _procure)
  1148. {
  1149. values[count++] = "(" + getCastleId() + "," + cp.getId() + "," + cp.getAmount() + "," + cp.getStartAmount() + "," + cp.getPrice() + "," + cp.getReward() + "," + CastleManorManager.PERIOD_CURRENT + ")";
  1150. }
  1151. if (values.length > 0)
  1152. {
  1153. query.append(values[0]);
  1154. for (int i = 1; i < values.length; i++)
  1155. {
  1156. query.append(',');
  1157. query.append(values[i]);
  1158. }
  1159. try (PreparedStatement ps2 = con.prepareStatement(query.toString()))
  1160. {
  1161. ps2.execute();
  1162. }
  1163. }
  1164. }
  1165. if (!_procureNext.isEmpty())
  1166. {
  1167. int count = 0;
  1168. String query = "INSERT INTO castle_manor_procure VALUES ";
  1169. String values[] = new String[_procureNext.size()];
  1170. for (CropProcure cp : _procureNext)
  1171. {
  1172. values[count++] = "(" + getCastleId() + "," + cp.getId() + "," + cp.getAmount() + "," + cp.getStartAmount() + "," + cp.getPrice() + "," + cp.getReward() + "," + CastleManorManager.PERIOD_NEXT + ")";
  1173. }
  1174. if (values.length > 0)
  1175. {
  1176. query += values[0];
  1177. for (int i = 1; i < values.length; i++)
  1178. {
  1179. query += "," + values[i];
  1180. }
  1181. try (PreparedStatement ps3 = con.prepareStatement(query))
  1182. {
  1183. ps3.execute();
  1184. }
  1185. }
  1186. }
  1187. }
  1188. catch (Exception e)
  1189. {
  1190. _log.info("Error adding crop data for castle " + getName() + ": " + e.getMessage());
  1191. }
  1192. }
  1193. // save crop procure data for specified period
  1194. public void saveCropData(int period)
  1195. {
  1196. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  1197. {
  1198. PreparedStatement statement = con.prepareStatement(CASTLE_MANOR_DELETE_PROCURE_PERIOD);
  1199. statement.setInt(1, getCastleId());
  1200. statement.setInt(2, period);
  1201. statement.execute();
  1202. statement.close();
  1203. List<CropProcure> proc = null;
  1204. proc = getCropProcure(period);
  1205. if ((proc != null) && (proc.size() > 0))
  1206. {
  1207. int count = 0;
  1208. StringBuilder query = new StringBuilder();
  1209. query.append("INSERT INTO castle_manor_procure VALUES ");
  1210. String values[] = new String[proc.size()];
  1211. for (CropProcure cp : proc)
  1212. {
  1213. values[count++] = "(" + getCastleId() + "," + cp.getId() + "," + cp.getAmount() + "," + cp.getStartAmount() + "," + cp.getPrice() + "," + cp.getReward() + "," + period + ")";
  1214. }
  1215. if (values.length > 0)
  1216. {
  1217. query.append(values[0]);
  1218. for (int i = 1; i < values.length; i++)
  1219. {
  1220. query.append(',');
  1221. query.append(values[i]);
  1222. }
  1223. statement = con.prepareStatement(query.toString());
  1224. statement.execute();
  1225. statement.close();
  1226. }
  1227. }
  1228. }
  1229. catch (Exception e)
  1230. {
  1231. _log.info("Error adding crop data for castle " + getName() + ": " + e.getMessage());
  1232. }
  1233. }
  1234. public void updateCrop(int cropId, long amount, int period)
  1235. {
  1236. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1237. PreparedStatement ps = con.prepareStatement(CASTLE_UPDATE_CROP))
  1238. {
  1239. ps.setLong(1, amount);
  1240. ps.setInt(2, cropId);
  1241. ps.setInt(3, getCastleId());
  1242. ps.setInt(4, period);
  1243. ps.execute();
  1244. }
  1245. catch (Exception e)
  1246. {
  1247. _log.info("Error adding crop data for castle " + getName() + ": " + e.getMessage());
  1248. }
  1249. }
  1250. public void updateSeed(int seedId, long amount, int period)
  1251. {
  1252. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1253. PreparedStatement ps = con.prepareStatement(CASTLE_UPDATE_SEED))
  1254. {
  1255. ps.setLong(1, amount);
  1256. ps.setInt(2, seedId);
  1257. ps.setInt(3, getCastleId());
  1258. ps.setInt(4, period);
  1259. ps.execute();
  1260. }
  1261. catch (Exception e)
  1262. {
  1263. _log.info("Error adding seed production data for castle " + getName() + ": " + e.getMessage());
  1264. }
  1265. }
  1266. public boolean isNextPeriodApproved()
  1267. {
  1268. return _isNextPeriodApproved;
  1269. }
  1270. public void setNextPeriodApproved(boolean val)
  1271. {
  1272. _isNextPeriodApproved = val;
  1273. }
  1274. public void updateClansReputation()
  1275. {
  1276. if (_formerOwner != null)
  1277. {
  1278. if (_formerOwner != ClanTable.getInstance().getClan(getOwnerId()))
  1279. {
  1280. int maxreward = Math.max(0, _formerOwner.getReputationScore());
  1281. _formerOwner.takeReputationScore(Config.LOOSE_CASTLE_POINTS, true);
  1282. L2Clan owner = ClanTable.getInstance().getClan(getOwnerId());
  1283. if (owner != null)
  1284. {
  1285. owner.addReputationScore(Math.min(Config.TAKE_CASTLE_POINTS, maxreward), true);
  1286. }
  1287. }
  1288. else
  1289. {
  1290. _formerOwner.addReputationScore(Config.CASTLE_DEFENDED_POINTS, true);
  1291. }
  1292. }
  1293. else
  1294. {
  1295. L2Clan owner = ClanTable.getInstance().getClan(getOwnerId());
  1296. if (owner != null)
  1297. {
  1298. owner.addReputationScore(Config.TAKE_CASTLE_POINTS, true);
  1299. }
  1300. }
  1301. }
  1302. public void updateShowNpcCrest()
  1303. {
  1304. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1305. PreparedStatement ps = con.prepareStatement("UPDATE castle SET showNpcCrest = ? WHERE id = ?"))
  1306. {
  1307. ps.setString(1, String.valueOf(getShowNpcCrest()));
  1308. ps.setInt(2, getCastleId());
  1309. ps.execute();
  1310. }
  1311. catch (Exception e)
  1312. {
  1313. _log.info("Error saving showNpcCrest for castle " + getName() + ": " + e.getMessage());
  1314. }
  1315. }
  1316. public List<L2Skill> getResidentialSkills()
  1317. {
  1318. return _residentialSkills;
  1319. }
  1320. public void giveResidentialSkills(L2PcInstance player)
  1321. {
  1322. for (L2Skill sk : _residentialSkills)
  1323. {
  1324. player.addSkill(sk, false);
  1325. }
  1326. Territory territory = TerritoryWarManager.getInstance().getTerritory(getCastleId());
  1327. if ((territory != null) && territory.getOwnedWardIds().contains(getCastleId() + 80))
  1328. {
  1329. for (int wardId : territory.getOwnedWardIds())
  1330. {
  1331. final List<L2SkillLearn> territorySkills = SkillTreesData.getInstance().getAvailableResidentialSkills(wardId);
  1332. for (L2SkillLearn s : territorySkills)
  1333. {
  1334. final L2Skill sk = SkillTable.getInstance().getInfo(s.getSkillId(), s.getSkillLevel());
  1335. if (sk != null)
  1336. {
  1337. player.addSkill(sk, false);
  1338. }
  1339. else
  1340. {
  1341. _log.warning("Trying to add a null skill for Territory Ward Id: " + wardId + ", skill Id: " + s.getSkillId() + " level: " + s.getSkillLevel() + "!");
  1342. }
  1343. }
  1344. }
  1345. }
  1346. }
  1347. public void removeResidentialSkills(L2PcInstance player)
  1348. {
  1349. for (L2Skill sk : _residentialSkills)
  1350. {
  1351. player.removeSkill(sk, false, true);
  1352. }
  1353. if (TerritoryWarManager.getInstance().getTerritory(getCastleId()) != null)
  1354. {
  1355. for (int wardId : TerritoryWarManager.getInstance().getTerritory(getCastleId()).getOwnedWardIds())
  1356. {
  1357. final List<L2SkillLearn> territorySkills = SkillTreesData.getInstance().getAvailableResidentialSkills(wardId);
  1358. for (L2SkillLearn s : territorySkills)
  1359. {
  1360. final L2Skill sk = SkillTable.getInstance().getInfo(s.getSkillId(), s.getSkillLevel());
  1361. if (sk != null)
  1362. {
  1363. player.removeSkill(sk, false, true);
  1364. }
  1365. else
  1366. {
  1367. _log.warning("Trying to remove a null skill for Territory Ward Id: " + wardId + ", skill Id: " + s.getSkillId() + " level: " + s.getSkillLevel() + "!");
  1368. }
  1369. }
  1370. }
  1371. }
  1372. }
  1373. /**
  1374. * Register Artefact to castle
  1375. * @param artefact
  1376. */
  1377. public void registerArtefact(L2ArtefactInstance artefact)
  1378. {
  1379. _artefacts.add(artefact);
  1380. }
  1381. public List<L2ArtefactInstance> getArtefacts()
  1382. {
  1383. return _artefacts;
  1384. }
  1385. public void resetManor()
  1386. {
  1387. setCropProcure(new ArrayList<CropProcure>(), CastleManorManager.PERIOD_CURRENT);
  1388. setCropProcure(new ArrayList<CropProcure>(), CastleManorManager.PERIOD_NEXT);
  1389. setSeedProduction(new ArrayList<SeedProduction>(), CastleManorManager.PERIOD_CURRENT);
  1390. setSeedProduction(new ArrayList<SeedProduction>(), CastleManorManager.PERIOD_NEXT);
  1391. if (Config.ALT_MANOR_SAVE_ALL_ACTIONS)
  1392. {
  1393. saveCropData();
  1394. saveSeedData();
  1395. }
  1396. }
  1397. /**
  1398. * @return the tickets exchanged for this castle
  1399. */
  1400. public int getTicketBuyCount()
  1401. {
  1402. return _ticketBuyCount;
  1403. }
  1404. /**
  1405. * Set the exchanged tickets count.<br>
  1406. * Performs database update.
  1407. * @param count the ticket count to set
  1408. */
  1409. public void setTicketBuyCount(int count)
  1410. {
  1411. _ticketBuyCount = count;
  1412. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1413. PreparedStatement statement = con.prepareStatement("UPDATE castle SET ticketBuyCount = ? WHERE id = ?"))
  1414. {
  1415. statement.setInt(1, _ticketBuyCount);
  1416. statement.setInt(2, _castleId);
  1417. statement.execute();
  1418. }
  1419. catch (Exception e)
  1420. {
  1421. _log.log(Level.WARNING, e.getMessage(), e);
  1422. }
  1423. }
  1424. public int getTrapUpgradeLevel(int towerIndex)
  1425. {
  1426. final TowerSpawn spawn = SiegeManager.getInstance().getFlameTowers(_castleId).get(towerIndex);
  1427. return (spawn != null) ? spawn.getUpgradeLevel() : 0;
  1428. }
  1429. public void setTrapUpgrade(int towerIndex, int level, boolean save)
  1430. {
  1431. if (save)
  1432. {
  1433. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1434. PreparedStatement ps = con.prepareStatement("REPLACE INTO castle_trapupgrade (castleId, towerIndex, level) values (?,?,?)"))
  1435. {
  1436. ps.setInt(1, getCastleId());
  1437. ps.setInt(2, towerIndex);
  1438. ps.setInt(3, level);
  1439. ps.execute();
  1440. }
  1441. catch (Exception e)
  1442. {
  1443. _log.log(Level.WARNING, "Exception: setTrapUpgradeLevel(int towerIndex, int level, int castleId): " + e.getMessage(), e);
  1444. }
  1445. }
  1446. final TowerSpawn spawn = SiegeManager.getInstance().getFlameTowers(_castleId).get(towerIndex);
  1447. if (spawn != null)
  1448. {
  1449. spawn.setUpgradeLevel(level);
  1450. }
  1451. }
  1452. private void removeTrapUpgrade()
  1453. {
  1454. for (TowerSpawn ts : SiegeManager.getInstance().getFlameTowers(_castleId))
  1455. {
  1456. ts.setUpgradeLevel(0);
  1457. }
  1458. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1459. PreparedStatement ps = con.prepareStatement("DELETE FROM castle_trapupgrade WHERE castleId=?"))
  1460. {
  1461. ps.setInt(1, getCastleId());
  1462. ps.execute();
  1463. }
  1464. catch (Exception e)
  1465. {
  1466. _log.log(Level.WARNING, "Exception: removeDoorUpgrade(): " + e.getMessage(), e);
  1467. }
  1468. }
  1469. @Override
  1470. public String toString()
  1471. {
  1472. return _name + "(" + _castleId + ")";
  1473. }
  1474. }