Castle.java 41 KB

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