Castle.java 40 KB

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