Castle.java 42 KB

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